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 //======================================================================================= } }