ASP.NET CORE CACHE的使用(含MemoryCache,Redis)

原文: ASP.NET CORE CACHE的使用(含MemoryCache,Redis)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处连接和本声明。
本文连接: https://blog.csdn.net/warrior21st/article/details/62884629

依赖命名空间:html

Microsoft.AspNetCore.Mvc;//测试调用时redis

Microsoft.Extensions.Caching.Memory;缓存

Microsoft.Extensions.Caching.Redis;工具

StackExchange.Redis;
测试

Newtonsoft.Json;this


定义通用工具类 :CacheUntityspa

    public class CacheUntity
    {
        private static ICacheHelper _cache = new RedisCacheHelper();//默认使用Redis


        private static bool isInited = false;
        public static void Init(ICacheHelper cache)
        {
            if (isInited)
                return;
            _cache.Dispose();
            _cache = cache;
            isInited = true;
        }


        public static bool Exists(string key)
        {
            return _cache.Exists(key);
        }


        public static T GetCache<T>(string key) where T : class
        {
            return _cache.GetCache<T>(key);
        }


        public static void SetCache(string key, object value)
        {
            _cache.SetCache(key, value);
        }


        public static void SetCache(string key, object value, DateTimeOffset expiressAbsoulte)
        {
            _cache.SetCache(key, value, expiressAbsoulte);
        }


        //public void SetCache(string key, object value, double expirationMinute)
        //{


        //}


        public static void RemoveCache(string key)
        {
            _cache.RemoveCache(key);
        }


    }
.net


定义统一缓存操做接口:ICacheHelperhtm

public interface ICacheHelper
    {
        bool Exists(string key);


        T GetCache<T>(string key) where T : class;


        void SetCache(string key, object value);


        void SetCache(string key, object value, DateTimeOffset expiressAbsoulte);//设置绝对时间过时


        //void SetCache(string key, object value, double expirationMinute);  //设置滑动过时, 因redis暂未找到自带的滑动过时类的API,暂无需实现该接口


        void RemoveCache(string key);


        void Dispose();
    }
blog


定义RedisCache帮助类:RedisCacheHelper 

public class RedisCacheHelper : ICacheHelper
    {
        public RedisCacheHelper(/*RedisCacheOptions options, int database = 0*/)//这里能够作成依赖注入,但没打算作成通用类库,因此直接把链接信息直接写在帮助类里
        {
            RedisCacheOptions options = new RedisCacheOptions();
            options.Configuration = "127.0.0.1:6379";
            options.InstanceName = "test";
            int database = 0;
            _connection = ConnectionMultiplexer.Connect(options.Configuration);
            _cache = _connection.GetDatabase(database);
            _instanceName = options.InstanceName;
        }


        private IDatabase _cache;


        private ConnectionMultiplexer _connection;


        private readonly string _instanceName;


        private string GetKeyForRedis(string key)
        {
            return _instanceName + key;
        }


        public bool Exists(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));


            return _cache.KeyExists(GetKeyForRedis(key));
        }


        public T GetCache<T>(string key) where T : class
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));


            var value = _cache.StringGet(GetKeyForRedis(key));
            if (!value.HasValue)
                return default(T);


            return JsonConvert.DeserializeObject<T>(value);
        }


        public void SetCache(string key, object value)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));
            if (value == null)
                throw new ArgumentNullException(nameof(value));


            if (Exists(GetKeyForRedis(key)))
                RemoveCache(GetKeyForRedis(key));


            _cache.StringSet(GetKeyForRedis(key), JsonConvert.SerializeObject(value));
        }


        public void SetCache(string key, object value, DateTimeOffset expiressAbsoulte)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));
            if (value == null)
                throw new ArgumentNullException(nameof(value));


            if (Exists(GetKeyForRedis(key)))
                RemoveCache(GetKeyForRedis(key));


            _cache.StringSet(GetKeyForRedis(key), JsonConvert.SerializeObject(value), expiressAbsoulte - DateTime.Now);
        }


        //public void SetCache(string key, object value, double expirationMinute)
        //{
        //    if (Exists(GetKeyForRedis(key)))
        //        RemoveCache(GetKeyForRedis(key));


        //    DateTime now = DateTime.Now;
        //    TimeSpan ts = now.AddMinutes(expirationMinute) - now;
        //    _cache.StringSet(GetKeyForRedis(key), JsonConvert.SerializeObject(value), ts);
        //}


        public void RemoveCache(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));


            _cache.KeyDelete(GetKeyForRedis(key));
        }


        public void Dispose()
        {
            if (_connection != null)
                _connection.Dispose();
            GC.SuppressFinalize(this);
        }
    }


定义MemoryCache帮助类:MemoryCacheHelper 

public class MemoryCacheHelper : ICacheHelper
    {
        public MemoryCacheHelper(/*MemoryCacheOptions options*/)//这里能够作成依赖注入,但没打算作成通用类库,因此直接把选项直接封在帮助类里边
        {
            //this._cache = new MemoryCache(options);
            this._cache = new MemoryCache(new MemoryCacheOptions());
        }


        private IMemoryCache _cache;


        public bool Exists(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));


            object v = null;
            return this._cache.TryGetValue<object>(key, out v);
        }


        public T GetCache<T>(string key) where T : class
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));


            T v = null;
            this._cache.TryGetValue<T>(key, out v);


            return v;
        }


        public void SetCache(string key, object value)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));
            if (value == null)
                throw new ArgumentNullException(nameof(value));


            object v = null;
            if (this._cache.TryGetValue(key, out v))
                this._cache.Remove(key);
            this._cache.Set<object>(key, value);
        }


        public void SetCache(string key, object value, double expirationMinute)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));
            if (value == null)
                throw new ArgumentNullException(nameof(value));


            object v = null;
            if (this._cache.TryGetValue(key, out v))
                this._cache.Remove(key);
            DateTime now = DateTime.Now;
            TimeSpan ts = now.AddMinutes(expirationMinute) - now;
            this._cache.Set<object>(key, value, ts);
        }


        public void SetCache(string key, object value, DateTimeOffset expirationTime)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));
            if (value == null)
                throw new ArgumentNullException(nameof(value));


            object v = null;
            if (this._cache.TryGetValue(key, out v))
                this._cache.Remove(key);


            this._cache.Set<object>(key, value, expirationTime);
        }


        public void RemoveCache(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                throw new ArgumentNullException(nameof(key));


            this._cache.Remove(key);
        }


        public void Dispose()
        {
            if (_cache != null)
                _cache.Dispose();
            GC.SuppressFinalize(this);
        }
    }


调用:

        [HttpGet]         public string TestCache()         {             CacheUntity.SetCache("test", "RedisCache works!");             string res = CacheUntity.GetCache<string>("test");             res += Environment.NewLine;             CacheUntity.Init(new MemoryCacheHelper());             CacheUntity.SetCache("test", "MemoryCache works!");             res += CacheUntity.GetCache<string>("test");             return res;         }

相关文章
相关标签/搜索