zoukankan      html  css  js  c++  java
  • .NET Core使用缓存

    1.MemoryCache 服务器端缓存
    添加服务:services.AddMemoryCache();
    使用:注入IMemoryCache _memoryCache;

    var cityList = new List<City>();
                if (!_memoryCache.TryGetValue("cities", out test))
                {
                    cityList = _cityServices.GetAll();
                    var cacheEntryOptions = new MemoryCacheEntryOptions()
                        .SetSlidingExpiration(TimeSpan.FromMinutes(5))
                        .SetAbsoluteExpiration(TimeSpan.FromDays(1));
    
                    //缓存清除之后调用回调
                    //cacheEntryOptions.RegisterPostEvictionCallback();
    
                    _memoryCache.Set("cities",cityList);
                }
                return cityList;

    2.CacheTagHelper自己查
    3.使用Redis;NuGet安装Microsoft.Extensions.Caching.Redis
    StartUp.cs注册服务:

    services.AddDistributedRedisCache(options=> {
                    options.Configuration = "localhost";
                    options.InstanceName = "redis-for-albums";
                });

    4.使用,注入IDistributedCache distributedCache,代码

    var cityList = new List<City>();
                var cityListBytes = _distributedCache.Get("cities");
                if (cityListBytes == null)
                {
                    cityList = _cityServices.GetAll();
                    var cityString = JsonConvert.SerializeObject(cityList);
                    byte[] cityEncode = Encoding.UTF8.GetBytes(cityString);
                    var cacheEntryOptions = new MemoryCacheEntryOptions()
                        .SetSlidingExpiration(TimeSpan.FromMinutes(5));
                    _distributedCache.Set("cities", cityEncode);
                }
                else
                {
                    byte[] cityEncode = _distributedCache.Get("cities");
                    var cityString = Encoding.UTF8.GetString(cityEncode);
                    cityList = JsonConvert.DeserializeObject<List<City>>(cityString);
                }
                return cityList;

    5.ResponseCache,

  • 相关阅读:
    在游戏中充分利用可编程的GPU
    坐标变换
    深入理解GPU Architecture(上)
    RV870和GT300的一些猜测
    深入理解Intel Core Microarchitecture
    CGDC见闻
    hdu 1517 K(2~9)倍博弈
    hdu 2177 威佐夫博弈+输出使你胜的你第1次取石子后剩下的两堆石子的数量
    坚持住
    真正体会到一个ac的快感
  • 原文地址:https://www.cnblogs.com/xsj1989/p/13856537.html
Copyright © 2011-2022 走看看