ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

.NET6 WebApi开发练习—Redis缓存

2022-07-23 22:03:52  阅读:190  来源: 互联网

标签:WebApi return string Redis fieldKey dict key NET6 public


Redis用的是我电脑本地安装的来做测试,这篇随笔不涉及Redis安装啦。

涉及的Nuget包:StackExchange.Redis

 1、先写了一个简单的接口文件,ICache;用来学习更多中缓存方法,代码如下:

 public interface ICache
    {
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="expireTime"></param>
        /// <returns></returns>
        bool SetCache<T>(string key, T value, DateTime? expireTime = null);
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        T GetCache<T>(string key);
        /// <summary>
        /// 根据键精准删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool RemoveCache(string key);
        /// <summary>
        /// 模糊删除,左侧匹配,右侧模糊
        /// </summary>
        /// <param name="keywords"></param>
        /// <returns></returns>
        Task RemoveKeysLeftLike(string keywords);
        /// <summary>
        /// 获取自增id
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        long GetIncr(string key);
        /// <summary>
        /// 获取自增id
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="expiresTime">过期时间</param>
        /// <returns></returns>
        long GetIncr(string key, TimeSpan expiresTime);
        #region Hash
        int SetHashFieldCache<T>(string key, string fieldKey, T fieldValue);
        int SetHashFieldCache<T>(string key, Dictionary<string, T> dict);
        T GetHashFieldCache<T>(string key, string fieldKey);
        Dictionary<string, T> GetHashFieldCache<T>(string key, Dictionary<string, T> dict);
        Dictionary<string, T> GetHashCache<T>(string key);
        List<T> GetHashToListCache<T>(string key);
        bool RemoveHashFieldCache(string key, string fieldKey);
        Dictionary<string, bool> RemoveHashFieldCache(string key, Dictionary<string, bool> dict);
        #endregion
    }

 

2、在appsetting.json文件中配置Redis连接配置。

"Redis": {
    "host": "127.0.0.1",
    "port": 6379,
    "password": "12345",
    "maxWritePoolSize": 60,
    "maxReadPoolSize": 60,
    "autoStart": true,
    "localCacheTime": 36000,
    "recordeLog": false,
    "preName": "flower_"
  },

 

3、创建RedisCache.cs,实现ICache接口文件,代码如下:

public class RedisCache : ICache
    {
        private StackExchange.Redis.IDatabase cache;

        private ConnectionMultiplexer connection;

        private readonly IConfiguration _configuration;

        public RedisCache(IConfiguration configuration)
        {
            //把appsetting.json中配置的Redis连接配置注入进来,连接Redis
            _configuration = configuration;
            string redisHost = _configuration["Redis:host"];
            int redisPort = int.Parse(_configuration["Redis:port"]);

            var configurationOptions = new ConfigurationOptions
            {
                EndPoints =
                    {
                        { redisHost, redisPort }
                    },
                KeepAlive = 180,
                Password = _configuration["Redis:password"],
                AllowAdmin = true
            };
            connection = ConnectionMultiplexer.Connect(configurationOptions);
            cache = connection.GetDatabase();
        }

        public bool SetCache<T>(string key, T value, DateTime? expireTime = null)
        {
            try
            {
                var jsonOption = new JsonSerializerSettings()
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                };
                string strValue = JsonConvert.SerializeObject(value, jsonOption);
                if (string.IsNullOrEmpty(strValue))
                {
                    return false;
                }
                if (expireTime == null)
                {
                    return cache.StringSet(InitKey(key), strValue);
                }
                else
                {
                    return cache.StringSet(InitKey(key), strValue, (expireTime.Value - DateTime.Now));
                }
            }
            catch (Exception ex)
            {
                //LogHelper.Error(ex);
            }
            return false;
        }

        public bool RemoveCache(string key)
        {
            return cache.KeyDelete(InitKey(key));
        }

        public T GetCache<T>(string key)
        {
            var t = default(T);
            try
            {
                var value = cache.StringGet(InitKey(key));
                if (string.IsNullOrEmpty(value))
                {
                    return t;
                }
                t = JsonConvert.DeserializeObject<T>(value);
            }
            catch (Exception ex)
            {
                //LogHelper.Error(ex);
            }
            return t;
        }

        public long GetIncr(string key)
        {
            try
            {
                return cache.StringIncrement(InitKey(key));
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
        public long GetIncr(string key, TimeSpan expiresTime)
        {
            try
            {
                var qty = cache.StringIncrement(InitKey(key));
                if (qty == 1)
                {
                    //设置过期时间
                    cache.KeyExpire(key, expiresTime);
                }
                return qty;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        #region Hash
        public int SetHashFieldCache<T>(string key, string fieldKey, T fieldValue)
        {
            return SetHashFieldCache<T>(InitKey(key), new Dictionary<string, T> { { fieldKey, fieldValue } });
        }

        public int SetHashFieldCache<T>(string key, Dictionary<string, T> dict)
        {
            int count = 0;
            var jsonOption = new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            };
            foreach (string fieldKey in dict.Keys)
            {
                string fieldValue = JsonConvert.SerializeObject(dict[fieldKey], jsonOption);
                count += cache.HashSet(InitKey(key), fieldKey, fieldValue) ? 1 : 0;
            }
            return count;
        }

        public T GetHashFieldCache<T>(string key, string fieldKey)
        {
            var dict = GetHashFieldCache<T>(InitKey(key), new Dictionary<string, T> { { fieldKey, default(T) } });
            return dict[fieldKey];
        }

        public Dictionary<string, T> GetHashFieldCache<T>(string key, Dictionary<string, T> dict)
        {
            foreach (string fieldKey in dict.Keys)
            {
                string fieldValue = cache.HashGet(InitKey(key), fieldKey);
                dict[fieldKey] = JsonConvert.DeserializeObject<T>(fieldValue);
            }
            return dict;
        }

        public Dictionary<string, T> GetHashCache<T>(string key)
        {
            Dictionary<string, T> dict = new Dictionary<string, T>();
            var hashFields = cache.HashGetAll(InitKey(key));
            foreach (HashEntry field in hashFields)
            {
                dict[field.Name] = JsonConvert.DeserializeObject<T>(field.Value);
            }
            return dict;
        }

        public List<T> GetHashToListCache<T>(string key)
        {
            List<T> list = new List<T>();
            var hashFields = cache.HashGetAll(InitKey(key));
            foreach (HashEntry field in hashFields)
            {
                list.Add(JsonConvert.DeserializeObject<T>(field.Value));
            }
            return list;
        }

        public bool RemoveHashFieldCache(string key, string fieldKey)
        {
            Dictionary<string, bool> dict = new Dictionary<string, bool> { { fieldKey, false } };
            dict = RemoveHashFieldCache(InitKey(key), dict);
            return dict[fieldKey];
        }

        public Dictionary<string, bool> RemoveHashFieldCache(string key, Dictionary<string, bool> dict)
        {
            foreach (string fieldKey in dict.Keys)
            {
                dict[fieldKey] = cache.HashDelete(InitKey(key), fieldKey);
            }
            return dict;
        }
        private string InitKey(string key)
        {
            return $"{_configuration["Redis:preName"]}{key}";
        }
        #endregion

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

        public void tet()
        {
            var pattern = "BUIK_201710*";
            var redisResult = cache.ScriptEvaluateAsync(LuaScript.Prepare(
                            //Redis的keys模糊查询:
                            " local res = redis.call('KEYS', @keypattern) " +
                            " return res "), new { @keypattern = pattern }).Result;

            if (!redisResult.IsNull)
            {
                cache.KeyDelete((RedisKey[])redisResult);  //删除一组key
            }
        }

        public async Task RemoveKeysLeftLike(string keywords)
        {
            var redisResult = await cache.ScriptEvaluateAsync(LuaScript.Prepare(
                           //Redis的keys模糊查询:
                           " local res = redis.call('KEYS', @keywords) " +
                           " return res "), new { @keywords = $"{InitKey(keywords)}*" });

            if (!redisResult.IsNull)
            {
                cache.KeyDelete((RedisKey[])redisResult);  //删除一组key
            }
        }

    }

 

4、在program.cs文件中,将Redis配置注入好

//注入Redis
builder.Services.AddSingleton<ICache,RedisCache> ();

 

5、用法就是上篇随笔记的内容,通过构造函数注入来使用。

//存入Redis中
_redisCache.SetCache<string>($"verificationcode_{verificationCodeId}", strValidateCode, DateTime.Now.AddMinutes(5));

 

运行测试代码之后,通过控制台正常查到记录的数据了,算是使用成功了。

 

标签:WebApi,return,string,Redis,fieldKey,dict,key,NET6,public
来源: https://www.cnblogs.com/itzh/p/16513120.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有