net core 配置Redis Cache
参考文章地址: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 }
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;
}
}
}

浙公网安备 33010602011771号