using Newtonsoft.Json; using ServiceStack.Redis; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace CarLocalMeter { /// /// 这个只是做个参考,我们只用前面两个 /// public class RedisOption { #region 数据缓存,实际上我们只需要这个里面的3个接口即可 /// /// 存储数据 /// /// key值 /// value值 /// 过期时间 /// public bool setRedis(string key, object value, int timeout = 0) { try { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) return false; if (client.ContainsKey(key)) client.Replace(key, value); else client.Set(key, value); if (timeout > 0) client.ExpireEntryIn(key, TimeSpan.FromSeconds(timeout)); //也可以用DateTime的设置,这个随意 //client.ExpireEntryAt(key,DateTime.Now.AddSeconds(timeout)); return true; } } catch (Exception ex) { //_logger.LogError($"========Redis写入失败:{ex.Message}==========="); return false; } } /// /// 传入的T必须是能够正常转换未对应对格式的数据, /// 比如Hashtable或者Dictionary《string,string>存入的数据格式一个是类似"{'a':'1','b':'2'}" /// 如果是string或者是int之类的则 "xxx"即可 /// 如果是Hashtable则读取数据方式:foreach (var mc in mm.Keys) mm[mc]; /// 如果是Dictionary则读取数据方式:foreach (var mc in mm.Keys) mm[mc]; /// /// /// /// public T getRedisObj(string key) { try { T t = default(T); using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) { if (client == null) return t; string configData = client.Get(key); if (string.IsNullOrWhiteSpace(configData)) return t; return JsonConvert.DeserializeObject(configData.Trim()); } } catch (Exception ex) { //_logger.LogError($"========Redis写入失败:{ex.Message}==========="); throw new Exception("数据获取失败:" + ex.Message); } } public bool delRedisObj(string key) { try { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) { if (client == null) return false; client.Del(key); return true; } } catch (Exception ex) { throw new Exception("数据删除失败:" + ex.Message); } } #endregion #region redis 的 list 数据类型对于大部分使用者来说,是实现队列服务的最经济,最简单的方式,取出后将自动删除 /// /// EnqueueItemOnList是先入先出,比如写入的是1,2,3读取的时候就是1,2,3 /// 读取完成后会自动删除 /// /// /// /// public bool setEnqueue(string key, List qList) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) return false; foreach (string t in qList) { client.EnqueueItemOnList(key, t); //入队 } return true; } } /// /// PushItemToList是先入后出,比如写入了 1,2,3读取的时候是按3,2,1读取出来的 /// 读取完成后会自动删除 /// /// /// /// public bool setPushItem(string key, List qList) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) return false; foreach (string t in qList) { client.PushItemToList(key, t); //入栈 } return true; } } /// /// 读取队列中所有的数据:上面EnqueueItemOnList跟PushItemToList写入的数据 /// 当然我们很多时候可能是读取1个,然后使用完后再进行读取,那么用不带s的 /// /// /// public string getListDb(string key) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) return ""; return client.DequeueItemFromList(key) ?? ""; } } /// /// 读取队列中所有的数据:上面EnqueueItemOnList跟PushItemToList写入的数据 /// 当然我们很多时候可能是读取1个,然后使用完后再进行读取,那么用不带s的 /// /// /// public IEnumerable getListDbs(string key) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) yield break; for (int i = 0; i < client.GetListCount(key); i++) { yield return client.DequeueItemFromList(key) ?? ""; } } } #endregion #region Redis 的 Set 是 string 类型的无序集合,主要应用在一些需要求交集、并集、补集这样的场景 /// /// Set集合中key值可以是相同的,value值不允许重复 /// /// /// /// public bool setRedisItem(string key, string value) { try { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) return false; client.AddItemToSet(key,value); return true; } #region 利用集合可求出两集合相同的value的值 /* #region Set操作 client.AddItemToSet("QQ用户1", "好友A"); client.AddItemToSet("QQ用户1", "好友B"); client.AddItemToSet("QQ用户2", "好友B"); client.AddItemToSet("QQ用户2", "好友F"); var setunion = client.GetIntersectFromSets("QQ用户1", "QQ用户2"); foreach (var item in setunion) Console.WriteLine($"QQ用户1和QQ用户2的共同好友为:{item}"); //实际上GetIntersectFromSets取得的是HashSet,所以可以直接操作2个HashSet来取交集、并集等 //*/ #endregion } catch (Exception ex) { //_logger.LogError($"========Redis写入失败:{ex.Message}==========="); return false; } } public HashSet getRedisItem(string key) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { return client.GetIntersectFromSets(key); } } public bool setRedisItemSort(string key, string value,double db) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) return false; client.AddItemToSortedSet(key, value, db); return true; } } public IDictionary getRedisItemSort(string key) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { return client.GetRangeWithScoresFromSortedSet(key, 1, 3); } } public bool setRedisHash(string hashId, string key, string value) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) return false; client.SetEntryInHash(hashId, key, value); return true; } } public Dictionary getRedisHash(string hashId) { using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置 { if (client == null) return null; return client.GetAllEntriesFromHash(hashId); } } #endregion } }