net core 配置Redis Cache
参考文章地址:https://dotnetcoretutorials.com/2017/01/06/using-redis-cache-net-core/
具体步骤:
1 Install-Package Microsoft.Extensions.Caching.Redis
2‘
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
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; } } }