• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Redis的增删改查c#keyvalue类型和hashmap类型

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AIMS.RedisMng
{
    public class RedisContext : IRedisContext 
    {
        private readonly static string REDIS_CONN = "127.0.0.1:6379";
        private readonly static string REDIS_IP = "127.0.0.1";
        private readonly static int REDIS_PORT = 6379;


        private ConnectionMultiplexer redis = null;
        //private StackExchange redis = null;
        private IDatabase database = null;
        private IServer server = null;
        private int mydb = 0;
        
        public RedisContext()
        {
            //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;

            }
        }
        //==========================================================================
        #region key value 操作
        /// <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);
        }

        /// <summary>
        /// 看key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyIsExist(string key)
        {
            return database.KeyExists(key);
           
        }

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

        /// <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 = default(TimeSpan?))
        {
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(val);
            return database.StringSet(key, json, exp);
        }

        /// <summary>
        /// 异常记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public 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, TimeSpan? exp = default(TimeSpan?))
        {
            return database.StringSet(key, val, exp);
        }
        #endregion
        //===========================================================================================
        #region hash map 操作

        /// <summary>
        /// 移除字段
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public bool HashDelete(string redisKey, string hashField)
        {
            return database.HashDelete(redisKey, hashField);
            //return redis.HashDelete(redisKey, hashField);
        }
        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public bool HashExists(string redisKey, string hashField)
        {
            return database.HashExists(redisKey, hashField);
            //return redis.KeyExists(redisKey, hashField);
        }
        /// <summary>
        /// 获取值通过key和fieldredisKey
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public RedisValue HashGet(string redisKey, string hashField)
        {
            return database.HashGet(redisKey, hashField).ToString();
            //return redis.HashGet(redisKey, hashField).ToString();
        }

        /// <summary>
        /// 获取值通过key和field
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public T HashGet<T>(string redisKey, string hashField)
        {
           
            return JsonConvert.DeserializeObject<T>(database.HashGet(redisKey, hashField));
        }

        /// <summary>
        /// 获取值通过key
        /// </summary>
        /// <param name="redisKey"></param>
        /// <returns></returns>
        public IEnumerable<RedisValue> HashKeys(string redisKey)
        {
            return database.HashValues(redisKey);
            //return database.HashLength(redisKey);
        }

        /// <summary>
        /// 设定值
        /// </summary>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool HashSet(string redisKey, string hashField, string value)
        {
            return Convert.ToBoolean( database.HashIncrement(redisKey, hashField,Convert.ToInt64(value)));
            //return redis.HashIncrement(redisKey, hashField, value);
        }

        /// <summary>
        /// 设定值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="redisKey"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool HashSet<T>(string redisKey, string hashField, T value)
        {
            string val = JsonConvert.SerializeObject(value);
           return database .HashSet(redisKey, hashField, val);
        }


        #endregion  
        //=======================================================================================
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#格式规范C#格式规范发布时间:2022-07-13
下一篇:
C#函数参数默认值发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap