net core 操作Redis

Posted zldqpm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了net core 操作Redis相关的知识,希望对你有一定的参考价值。

appsettings.json

"WebConfig": 
    "Redis": 
      "Connection": "127.0.1:6379,allowAdmin=true,password=cmkn,defaultdatabase=5,abortConnect=false",
      "InstanceName": "cmkn_Redis_Session_",
      "ApplicationName": "session_application_name",
      "PersistKeys": "DataProtection-Keys",
      "CookieName": "RedisSession","redisIP": "127.0.1",
      "redisPort": "6379",
      "redistime": "0.06:00:00"
    ,
    "SessionTimeOut": "30" 
  

 

Redis操作类

/// <summary>
    /// Redis操作类
    /// </summary>
    public class RedisHelper
    
        public static TimeSpan ts;
        private static ConnectionMultiplexer redis = null;
        private static IDatabase database = null;
        private static IServer server = null;
        private int mydb = 0;

        /// <summary>
        /// redis连接
        /// </summary>
        public static string REDIS_CONN;

        /// <summary>
        /// redisIP
        /// </summary>
        public static string REDIS_IP;

        /// <summary>
        /// redis端口
        /// </summary>
        public static int REDIS_PORT;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="db"></param>
        public RedisHelper(int db)
        
            mydb = db;
            if (redis == null)
            
                redis = ConnectionMultiplexer.Connect(REDIS_CONN);
                database = redis.GetDatabase(mydb);
                server = redis.GetServer(REDIS_IP, REDIS_PORT);
                redis.ErrorMessage += Redis_ErrorMessage;
            
        


        /// <summary>
        /// 异常记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Redis_ErrorMessage(object sender, RedisErrorEventArgs e)
        
            //LogHelper.WriteLog("Redis", new Exception(e.Message));
        

        /// <summary>
        /// 通过key获取value
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string StringGet(string key)
        
            return database.StringGet(key);
        

        /// <summary>
        /// 新增key value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public bool StringSet(string key, string val)
        
            return StringSet(key, val, ts);
        

        /// <summary>
        /// 新增key value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public bool StringSet(string key, string val, TimeSpan exp)
        
            return database.StringSet(key, val, exp);
        

        /// <summary>
        /// 新增key value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public bool ObjectSet(string key, object val)
        
            return ObjectSet(key, val, ts);
        

        /// <summary>
        /// 新增key value
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="exp"></param>
        /// <returns></returns>
        public bool ObjectSet(string key, object val, TimeSpan exp)
        
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(val);
            return database.StringSet(key, json, exp);
        

        /// <summary>
        /// 获取key
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public IEnumerable<RedisKey> LikeKeys(string pattern = "*")
        
            return server.Keys(database: mydb, pattern: pattern);
        

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public Dictionary<string, string> GetLikeKeyValue(string pattern = "*")
        
            IEnumerable<RedisKey> list = LikeKeys(pattern);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (list != null && list.Count() > 0)
            
                foreach (var item in list)
                
                    dic.Add(item, StringGet(item));
                
            
            return dic;
        

        /// <summary>
        /// 删除key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyDelete(string key)
        
            return database.KeyDelete(key);
        
    

Startup.cs 给操作类赋值

 RedisHelper.REDIS_CONN = Configuration["WebConfig:Redis:Connection"];
 RedisHelper.REDIS_IP = Configuration["WebConfig:Redis:redisIP"];
 if (!int.TryParse(Configuration["WebConfig:Redis:redisPort"], out RedisHelper.REDIS_PORT)) RedisHelper.REDIS_PORT = 6379;

 

以上是关于net core 操作Redis的主要内容,如果未能解决你的问题,请参考以下文章

谈谈在.NET Core中使用Redis和Memcached的序列化问题

Redis优化:启动警告问题的解决(必须做的操作)

redis geo操作

net core体系-web应用程序-4asp.net core2.0 项目实战-5项目数据库操作封装操作

.NET Core 使用ODP.NET Core连接操作Oracle数据库

redis(在.net core中)