zoukankan      html  css  js  c++  java
  • 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 }
    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;
    		}
    	}
    }
    

      

  • 相关阅读:
    Pieczęć(模拟)
    【并查集】关押罪犯
    火车进栈
    独木舟上的旅行
    哈尔滨理工大学第八届程序设计团队赛K题
    [数学、递推]Everything Is Generated In Equal Probability
    [构造]triples I
    2019牛客第三场
    [DP]销售
    [哈夫曼树]猜球球
  • 原文地址:https://www.cnblogs.com/yanwuming/p/9613747.html
Copyright © 2011-2022 走看看