| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- using Newtonsoft.Json;
- using ServiceStack.Redis;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace CarLocalMeter
- {
- /// <summary>
- /// 这个只是做个参考,我们只用前面两个
- /// </summary>
- public class RedisOption
- {
- #region 数据缓存,实际上我们只需要这个里面的3个接口即可
- /// <summary>
- /// 存储数据
- /// </summary>
- /// <param name="key">key值</param>
- /// <param name="value">value值</param>
- /// <param name="timeout">过期时间</param>
- /// <returns></returns>
- 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<object>(key, value);
- else
- client.Set<object>(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;
- }
- }
- /// <summary>
- /// 传入的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];
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="key"></param>
- /// <returns></returns>
- public T getRedisObj<T>(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<string>(key);
- if (string.IsNullOrWhiteSpace(configData)) return t;
- return JsonConvert.DeserializeObject<T>(configData.Trim());
- }
- }
- catch (Exception ex)
- {
- //_logger.LogError($"========Redis写入失败:{ex.Message}===========");
- throw new Exception("数据获取失败:" + ex.Message);
- }
- }
- public bool delRedisObj<T>(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 数据类型对于大部分使用者来说,是实现队列服务的最经济,最简单的方式,取出后将自动删除
- /// <summary>
- /// EnqueueItemOnList是先入先出,比如写入的是1,2,3读取的时候就是1,2,3
- /// 读取完成后会自动删除
- /// </summary>
- /// <param name="key"></param>
- /// <param name="qList"></param>
- /// <returns></returns>
- public bool setEnqueue(string key, List<string> 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;
- }
- }
- /// <summary>
- /// PushItemToList是先入后出,比如写入了 1,2,3读取的时候是按3,2,1读取出来的
- /// 读取完成后会自动删除
- /// </summary>
- /// <param name="key"></param>
- /// <param name="qList"></param>
- /// <returns></returns>
- public bool setPushItem(string key, List<string> 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;
- }
- }
- /// <summary>
- /// 读取队列中所有的数据:上面EnqueueItemOnList跟PushItemToList写入的数据
- /// 当然我们很多时候可能是读取1个,然后使用完后再进行读取,那么用不带s的
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public string getListDb(string key)
- {
- using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
- {
- if (client == null) return "";
- return client.DequeueItemFromList(key) ?? "";
- }
- }
- /// <summary>
- /// 读取队列中所有的数据:上面EnqueueItemOnList跟PushItemToList写入的数据
- /// 当然我们很多时候可能是读取1个,然后使用完后再进行读取,那么用不带s的
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- public IEnumerable<string> 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 类型的无序集合,主要应用在一些需要求交集、并集、补集这样的场景
- /// <summary>
- /// Set集合中key值可以是相同的,value值不允许重复
- /// </summary>
- /// <param name="key"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- 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<string> 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<string,double> 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<string, string> getRedisHash(string hashId)
- {
- using (RedisClient client = new RedisClient(AppConfigCache.redisIp, AppConfigCache.redisPort)) //还可以接密码跟长度,这里没设置
- {
- if (client == null) return null;
- return client.GetAllEntriesFromHash(hashId);
- }
- }
- #endregion
- }
- }
|