参考文章地址:https://dotnetcoretutorials.com/2017/01/06/using-redis-cache-net-core/

 

具体步骤:

1   Install-Package Microsoft.Extensions.Caching.Redis

2‘  

 1 public void ConfigureServices(IServiceCollection services)
 2 {
 3 services.AddMvc();
 4  
 5 services.AddDistributedRedisCache(option =>
 6 {
 7 option.Configuration = "127.0.0.1";
 8 option.InstanceName = "master";
 9 });
10 }
View Code

3:

[Route("api/[controller]")]
public class HomeController : Controller
{
	private readonly IDistributedCache _distributedCache;
 
	public HomeController(IDistributedCache distributedCache)
	{
		_distributedCache = distributedCache;
	}
 
	[HttpGet]
	public async Task<string> Get()
	{
		var cacheKey = "TheTime";
		var existingTime = _distributedCache.GetString(cacheKey);
		if (!string.IsNullOrEmpty(existingTime))
		{
			return "Fetched from cache : " + existingTime;
		}
		else
		{
			existingTime = DateTime.UtcNow.ToString();
			_distributedCache.SetString(cacheKey, existingTime);
			return "Added to cache : " + existingTime;
		}
	}
}

  

相关文章:

  • 2022-03-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
猜你喜欢
  • 2021-11-04
  • 2022-12-23
  • 2021-11-07
  • 2021-06-25
  • 2021-07-22
相关资源
相似解决方案