使用redis组件如下,至于为什么使用3.9版本,是因为4.0开始商业了,限制了次数

ServiceStack.Common" version="3.9.70"
ServiceStack.Redis" version="3.9.71"
ServiceStack.Text" version="3.9.71"

接口

  1. public interface ICache
  2. {
  3. #region Key-Value
  4. /// <summary>
  5. /// 读取缓存
  6. /// </summary>
  7. /// <param name="cacheKey">键</param>
  8. /// <returns></returns>
  9. T Read<T>(string cacheKey, long dbId = ) where T : class;
  10. /// <summary>
  11. /// 写入缓存
  12. /// </summary>
  13. /// <param name="value">对象数据</param>
  14. /// <param name="cacheKey">键</param>
  15. void Write<T>(string cacheKey, T value, long dbId = ) where T : class;
  16. /// <summary>
  17. /// 写入缓存
  18. /// </summary>
  19. /// <param name="value">对象数据</param>
  20. /// <param name="cacheKey">键</param>
  21. /// <param name="expireTime">到期时间</param>
  22. void Write<T>(string cacheKey, T value, DateTime expireTime, long dbId = ) where T : class;
  23. /// <summary>
  24. /// 写入缓存
  25. /// </summary>
  26. /// <param name="value">对象数据</param>
  27. /// <param name="cacheKey">键</param>
  28. /// <param name="expireTime">到期时间</param>
  29. void Write<T>(string cacheKey, T value, TimeSpan timeSpan, long dbId = ) where T : class;
  30. /// <summary>
  31. /// 移除指定数据缓存
  32. /// </summary>
  33. /// <param name="cacheKey">键</param>
  34. void Remove(string cacheKey, long dbId = );
  35. /// <summary>
  36. /// 移除全部缓存
  37. /// </summary>
  38. void RemoveAll(long dbId = );
  39. #endregion
  40. }

配置类

  1. public sealed class RedisConfigInfo : ConfigurationSection
  2. {
  3. /// <summary>
  4. /// 获取配置信息
  5. /// </summary>
  6. /// <returns></returns>
  7. public static RedisConfigInfo GetConfig()
  8. {
  9. return GetConfig("redisconfig");
  10. }
  11. /// <summary>
  12. /// 获取配置信息
  13. /// </summary>
  14. /// <param name="sectionName">xml节点名称</param>
  15. /// <returns></returns>
  16. public static RedisConfigInfo GetConfig(string sectionName)
  17. {
  18. RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection(sectionName);
  19. if (section == null)
  20. throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
  21. return section;
  22. }
  23. /// <summary>
  24. /// 可写的Redis链接地址
  25. /// </summary>
  26. [ConfigurationProperty("WriteServerList", IsRequired = false)]
  27. public string WriteServerList
  28. {
  29. get
  30. {
  31. return (string)base["WriteServerList"];
  32. }
  33. set
  34. {
  35. base["WriteServerList"] = value;
  36. }
  37. }
  38. /// <summary>
  39. /// 可读的Redis链接地址
  40. /// </summary>
  41. [ConfigurationProperty("ReadServerList", IsRequired = false)]
  42. public string ReadServerList
  43. {
  44. get
  45. {
  46. return (string)base["ReadServerList"];
  47. }
  48. set
  49. {
  50. base["ReadServerList"] = value;
  51. }
  52. }
  53. /// <summary>
  54. /// 最大写链接数
  55. /// </summary>
  56. [ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = )]
  57. public int MaxWritePoolSize
  58. {
  59. get
  60. {
  61. int _maxWritePoolSize = (int)base["MaxWritePoolSize"];
  62. return _maxWritePoolSize > ? _maxWritePoolSize : ;
  63. }
  64. set
  65. {
  66. base["MaxWritePoolSize"] = value;
  67. }
  68. }
  69. /// <summary>
  70. /// 最大读链接数
  71. /// </summary>
  72. [ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = )]
  73. public int MaxReadPoolSize
  74. {
  75. get
  76. {
  77. int _maxReadPoolSize = (int)base["MaxReadPoolSize"];
  78. return _maxReadPoolSize > ? _maxReadPoolSize : ;
  79. }
  80. set
  81. {
  82. base["MaxReadPoolSize"] = value;
  83. }
  84. }
  85. /// <summary>
  86. /// 自动重启
  87. /// </summary>
  88. [ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)]
  89. public bool AutoStart
  90. {
  91. get
  92. {
  93. return (bool)base["AutoStart"];
  94. }
  95. set
  96. {
  97. base["AutoStart"] = value;
  98. }
  99. }
  100. /// <summary>
  101. /// 本地缓存到期时间,单位:秒
  102. /// </summary>
  103. [ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = )]
  104. public int LocalCacheTime
  105. {
  106. get
  107. {
  108. return (int)base["LocalCacheTime"];
  109. }
  110. set
  111. {
  112. base["LocalCacheTime"] = value;
  113. }
  114. }
  115. /// <summary>
  116. /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
  117. /// </summary>
  118. [ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)]
  119. public bool RecordeLog
  120. {
  121. get
  122. {
  123. return (bool)base["RecordeLog"];
  124. }
  125. set
  126. {
  127. base["RecordeLog"] = value;
  128. }
  129. }
  130. /// <summary>
  131. /// 默认开始db
  132. /// </summary>
  133. [ConfigurationProperty("DefaultDb", IsRequired = false)]
  134. public long DefaultDb
  135. {
  136. get
  137. {
  138. return (long)base["DefaultDb"];
  139. }
  140. set
  141. {
  142. base["DefaultDb"] = value;
  143. }
  144. }
  145.  
  146. }

处理类

  1. public class RedisCache
  2. {
  3. #region -- 连接信息 --
  4. /// <summary>
  5. /// redis配置文件信息
  6. /// </summary>
  7. private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();
  8. /// <summary>
  9. /// 创建链接池管理对象
  10. /// </summary>
  11. private static PooledRedisClientManager CreateManager(long dbId)
  12. {
  13. string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
  14. string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");
  15.  
  16. return new PooledRedisClientManager(readServerList, writeServerList,
  17. new RedisClientManagerConfig
  18. {
  19. MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
  20. MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
  21. AutoStart = redisConfigInfo.AutoStart,
  22. DefaultDb = dbId
  23. });
  24. }
  25. /// <summary>
  26. /// 字串转数组
  27. /// </summary>
  28. /// <param name="strSource">字串</param>
  29. /// <param name="split">分隔符</param>
  30. /// <returns></returns>
  31. private static string[] SplitString(string strSource, string split)
  32. {
  33. return strSource.Split(split.ToArray());
  34. }
  35. /// <summary>
  36. /// 获取redis客户端根据库ID号
  37. /// </summary>
  38. /// <param name="dbId">redis库Id</param>
  39. /// <returns></returns>
  40. private static PooledRedisClientManager GetClientManager(long dbId)
  41. {
  42. return CreateManager(dbId);
  43. }
  44.  
  45. #endregion
  46.  
  47. #region -- Item --
  48. /// <summary>
  49. /// 设置单体
  50. /// </summary>
  51. /// <typeparam name="T">值类型</typeparam>
  52. /// <param name="key">键值</param>
  53. /// <param name="t">值</param>
  54. /// <param name="dbId">库Id</param>
  55. /// <returns></returns>
  56. public static bool Set<T>(string key, T t, long dbId = )
  57. {
  58. var clientManager = GetClientManager(dbId);
  59. IRedisClient redis = clientManager.GetClient();
  60. var res = redis.Set<T>(key, t);
  61. clientManager.DisposeClient((RedisNativeClient)redis);
  62. redis.Dispose();
  63. clientManager.Dispose();
  64. return res;
  65. }
  66. /// <summary>
  67. /// 设置单体
  68. /// </summary>
  69. /// <typeparam name="T">值类型</typeparam>
  70. /// <param name="key">键值</param>
  71. /// <param name="t">值</param>
  72. /// <param name="timeSpan">保存时间</param>
  73. /// <param name="dbId">库Id</param>
  74. /// <returns></returns>
  75. public static bool Set<T>(string key, T t, TimeSpan timeSpan, long dbId = )
  76. {
  77. var clientManager = GetClientManager(dbId);
  78. IRedisClient redis = clientManager.GetClient();
  79. var res = redis.Set<T>(key, t, timeSpan);
  80. clientManager.DisposeClient((RedisNativeClient)redis);
  81. redis.Dispose();
  82. clientManager.Dispose();
  83. return res;
  84. }
  85. /// <summary>
  86. /// 设置单体
  87. /// </summary>
  88. /// <typeparam name="T">值类型</typeparam>
  89. /// <param name="key">键值</param>
  90. /// <param name="t">值</param>
  91. /// <param name="dateTime">过期时间</param>
  92. /// <returns></returns>
  93. public static bool Set<T>(string key, T t, DateTime dateTime, long dbId = )
  94. {
  95. var clientManager = GetClientManager(dbId);
  96. IRedisClient redis = clientManager.GetClient();
  97. var res = redis.Set<T>(key, t, dateTime);
  98. clientManager.DisposeClient((RedisNativeClient)redis);
  99. redis.Dispose();
  100. clientManager.Dispose();
  101. return res;
  102. }
  103.  
  104. /// <summary>
  105. /// 获取单体
  106. /// </summary>
  107. /// <typeparam name="T">值类型</typeparam>
  108. /// <param name="key">键值</param>
  109. /// <returns></returns>
  110. public static T Get<T>(string key, long dbId = ) where T : class
  111. {
  112. var clientManager = GetClientManager(dbId);
  113. IRedisClient redis = clientManager.GetClient();
  114. var res = redis.Get<T>(key);
  115. clientManager.DisposeClient((RedisNativeClient)redis);
  116. redis.Dispose();
  117. clientManager.Dispose();
  118. return res;
  119. }
  120. /// <summary>
  121. /// 移除单体
  122. /// </summary>
  123. /// <param name="key">键值</param>
  124. public static bool Remove(string key, long dbId = )
  125. {
  126. var clientManager = GetClientManager(dbId);
  127. IRedisClient redis = clientManager.GetClient();
  128. var res = redis.Remove(key);
  129. clientManager.DisposeClient((RedisNativeClient)redis);
  130. redis.Dispose();
  131. clientManager.Dispose();
  132. return res;
  133. }
  134. /// <summary>
  135. /// 清空所有缓存
  136. /// </summary>
  137. public static void RemoveAll(long dbId = )
  138. {
  139. var clientManager = GetClientManager(dbId);
  140. IRedisClient redis = clientManager.GetClient();
  141. redis.FlushDb();
  142. clientManager.DisposeClient((RedisNativeClient)redis);
  143. redis.Dispose();
  144. clientManager.Dispose();
  145. }
  146. #endregion
  147.  
  148. #region -- List --
  149. /// <summary>
  150. /// 添加列表
  151. /// </summary>
  152. /// <typeparam name="T">类型</typeparam>
  153. /// <param name="key">键值</param>
  154. /// <param name="t">值</param>
  155. /// <param name="dbId">库</param>
  156. public static void List_Add<T>(string key, T t, long dbId = )
  157. {
  158. using (IRedisClient redis = CreateManager(dbId).GetClient())
  159. {
  160. var redisTypedClient = redis.As<T>();
  161. redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t);
  162. }
  163. }
  164. /// <summary>
  165. /// 移除列表某个值
  166. /// </summary>
  167. /// <typeparam name="T">类型</typeparam>
  168. /// <param name="key">键值</param>
  169. /// <param name="t">值</param>
  170. /// <param name="dbId">库</param>
  171. /// <returns></returns>
  172. public static bool List_Remove<T>(string key, T t, long dbId = )
  173. {
  174. using (IRedisClient redis = CreateManager(dbId).GetClient())
  175. {
  176. var redisTypedClient = redis.As<T>();
  177. return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > ;
  178. }
  179. }
  180. /// <summary>
  181. /// 移除列表所有值
  182. /// </summary>
  183. /// <typeparam name="T">类型</typeparam>
  184. /// <param name="key">键值</param>
  185. /// <param name="dbId">库Id</param>
  186. public static void List_RemoveAll<T>(string key, long dbId = )
  187. {
  188. using (IRedisClient redis = CreateManager(dbId).GetClient())
  189. {
  190. var redisTypedClient = redis.As<T>();
  191. redisTypedClient.Lists[key].RemoveAll();
  192. }
  193. }
  194. /// <summary>
  195. /// 获取列表数据条数
  196. /// </summary>
  197. /// <param name="key"></param>
  198. /// <param name="dbId"></param>
  199. /// <returns></returns>
  200. public static long List_Count(string key, long dbId = )
  201. {
  202. using (IRedisClient redis = CreateManager(dbId).GetClient())
  203. {
  204. return redis.GetListCount(key);
  205. }
  206. }
  207. /// <summary>
  208. /// 获取指定条数列表数据
  209. /// </summary>
  210. /// <typeparam name="T">类型</typeparam>
  211. /// <param name="key">键值</param>
  212. /// <param name="start">开始编号</param>
  213. /// <param name="count">条数</param>
  214. /// <param name="dbId">库</param>
  215. /// <returns></returns>
  216. public static List<T> List_GetRange<T>(string key, int start, int count, long dbId = )
  217. {
  218. using (IRedisClient redis = CreateManager(dbId).GetClient())
  219. {
  220. var c = redis.As<T>();
  221. return c.Lists[key].GetRange(start, start + count - );
  222. }
  223. }
  224. /// <summary>
  225. /// 获取列表所有数据
  226. /// </summary>
  227. /// <typeparam name="T">类型</typeparam>
  228. /// <param name="key">键值</param>
  229. /// <param name="dbId">库数据</param>
  230. /// <returns></returns>
  231. public static List<T> List_GetList<T>(string key, long dbId = )
  232. {
  233. using (IRedisClient redis = CreateManager(dbId).GetClient())
  234. {
  235. var c = redis.As<T>();
  236. return c.Lists[key].GetRange(, c.Lists[key].Count);
  237. }
  238. }
  239. /// <summary>
  240. /// 获取列表分页数据
  241. /// </summary>
  242. /// <typeparam name="T">类型</typeparam>
  243. /// <param name="key">键值</param>
  244. /// <param name="pageIndex">页码</param>
  245. /// <param name="pageSize">每页条数</param>
  246. /// <param name="dbId">库</param>
  247. /// <returns></returns>
  248. public static List<T> List_GetList<T>(string key, int pageIndex, int pageSize, long dbId = )
  249. {
  250. int start = pageSize * (pageIndex - );
  251. return List_GetRange<T>(key, start, pageSize, dbId);
  252. }
  253. #endregion
  254.  
  255. #region -- Set --
  256. /// <summary>
  257. /// 添加集合
  258. /// </summary>
  259. /// <typeparam name="T">类型</typeparam>
  260. /// <param name="key">键值</param>
  261. /// <param name="t">数值</param>
  262. /// <param name="dbId">库</param>
  263. public static void Set_Add<T>(string key, T t, long dbId = )
  264. {
  265. using (IRedisClient redis = CreateManager(dbId).GetClient())
  266. {
  267. var redisTypedClient = redis.As<T>();
  268. redisTypedClient.Sets[key].Add(t);
  269. }
  270. }
  271. /// <summary>
  272. /// 集合是否包含指定数据
  273. /// </summary>
  274. /// <typeparam name="T">类型</typeparam>
  275. /// <param name="key">键值</param>
  276. /// <param name="t">数值</param>
  277. /// <param name="dbId">库</param>
  278. /// <returns></returns>
  279. public static bool Set_Contains<T>(string key, T t, long dbId = )
  280. {
  281. using (IRedisClient redis = CreateManager(dbId).GetClient())
  282. {
  283. var redisTypedClient = redis.As<T>();
  284. return redisTypedClient.Sets[key].Contains(t);
  285. }
  286. }
  287. /// <summary>
  288. /// 移除集合某个值
  289. /// </summary>
  290. /// <typeparam name="T">类型</typeparam>
  291. /// <param name="key">键值</param>
  292. /// <param name="t">数值</param>
  293. /// <param name="dbId">库</param>
  294. /// <returns></returns>
  295. public static bool Set_Remove<T>(string key, T t, long dbId = )
  296. {
  297. using (IRedisClient redis = CreateManager(dbId).GetClient())
  298. {
  299. var redisTypedClient = redis.As<T>();
  300. return redisTypedClient.Sets[key].Remove(t);
  301. }
  302. }
  303. #endregion
  304.  
  305. #region -- Hash --
  306. /// <summary>
  307. /// 判断某个数据是否已经被缓存
  308. /// </summary>
  309. /// <typeparam name="T">类型</typeparam>
  310. /// <param name="key">hashID</param>
  311. /// <param name="dataKey">键值</param>
  312. /// <param name="dbId">库</param>
  313. /// <returns></returns>
  314. public static bool Hash_Exist<T>(string key, string dataKey, long dbId = )
  315. {
  316. using (IRedisClient redis = CreateManager(dbId).GetClient())
  317. {
  318. return redis.HashContainsEntry(key, dataKey);
  319. }
  320. }
  321.  
  322. /// <summary>
  323. /// 存储数据到hash表
  324. /// </summary>
  325. /// <typeparam name="T">类型</typeparam>
  326. /// <param name="key">hashID</param>
  327. /// <param name="dataKey">键值</param>
  328. /// <param name="t">数值</param>
  329. /// <param name="dbId">库</param>
  330. /// <returns></returns>
  331. public static bool Hash_Set<T>(string key, string dataKey, T t, long dbId = )
  332. {
  333. using (IRedisClient redis = CreateManager(dbId).GetClient())
  334. {
  335. string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
  336. return redis.SetEntryInHash(key, dataKey, value);
  337. }
  338. }
  339. /// <summary>
  340. /// 移除hash中的某值
  341. /// </summary>
  342. /// <param name="key">hashID</param>
  343. /// <param name="dataKey">键值</param>
  344. /// <param name="dbId">库</param>
  345. /// <returns></returns>
  346. public static bool Hash_Remove(string key, string dataKey, long dbId = )
  347. {
  348. using (IRedisClient redis = CreateManager(dbId).GetClient())
  349. {
  350. return redis.RemoveEntryFromHash(key, dataKey);
  351. }
  352. }
  353. /// <summary>
  354. /// 移除整个hash
  355. /// </summary>
  356. /// <param name="key">hashID</param>
  357. /// <param name="dbId">库</param>
  358. /// <returns></returns>
  359. public static bool Hash_Remove(string key, long dbId = )
  360. {
  361. using (IRedisClient redis = CreateManager(dbId).GetClient())
  362. {
  363. return redis.Remove(key);
  364. }
  365. }
  366. /// <summary>
  367. /// 从hash表获取数据
  368. /// </summary>
  369. /// <typeparam name="T">类型</typeparam>
  370. /// <param name="key">hashID</param>
  371. /// <param name="dataKey">键值</param>
  372. /// <param name="dbId">库</param>
  373. /// <returns></returns>
  374. public static T Hash_Get<T>(string key, string dataKey, long dbId = )
  375. {
  376. using (IRedisClient redis = CreateManager(dbId).GetClient())
  377. {
  378. string value = redis.GetValueFromHash(key, dataKey);
  379. return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(value);
  380. }
  381. }
  382. /// <summary>
  383. /// 获取整个hash的数据
  384. /// </summary>
  385. /// <typeparam name="T">类型</typeparam>
  386. /// <param name="key">hashID</param>
  387. /// <param name="dbId">库</param>
  388. /// <returns></returns>
  389. public static List<T> Hash_GetAll<T>(string key, long dbId = )
  390. {
  391. using (IRedisClient redis = CreateManager(dbId).GetClient())
  392. {
  393. var list = redis.GetHashValues(key);
  394. if (list != null && list.Count > )
  395. {
  396. List<T> result = new List<T>();
  397. foreach (var item in list)
  398. {
  399. var value = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
  400. result.Add(value);
  401. }
  402. return result;
  403. }
  404. return null;
  405. }
  406. }
  407. #endregion
  408.  
  409. #region -- SortedSet --
  410. /// <summary>
  411. /// 添加数据到 SortedSet
  412. /// </summary>
  413. /// <typeparam name="T">类型</typeparam>
  414. /// <param name="key">集合id</param>
  415. /// <param name="t">数值</param>
  416. /// <param name="score">排序码</param>
  417. /// <param name="dbId">库</param>
  418. public static bool SortedSet_Add<T>(string key, T t, double score, long dbId = )
  419. {
  420. using (IRedisClient redis = CreateManager(dbId).GetClient())
  421. {
  422. string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
  423. return redis.AddItemToSortedSet(key, value, score);
  424. }
  425. }
  426. /// <summary>
  427. /// 移除数据从SortedSet
  428. /// </summary>
  429. /// <typeparam name="T">类型</typeparam>
  430. /// <param name="key">集合id</param>
  431. /// <param name="t">数值</param>
  432. /// <param name="dbId">库</param>
  433. /// <returns></returns>
  434. public static bool SortedSet_Remove<T>(string key, T t, long dbId = )
  435. {
  436. using (IRedisClient redis = CreateManager(dbId).GetClient())
  437. {
  438. string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
  439. return redis.RemoveItemFromSortedSet(key, value);
  440. }
  441. }
  442. /// <summary>
  443. /// 修剪SortedSet
  444. /// </summary>
  445. /// <param name="key">键值</param>
  446. /// <param name="size">保留的条数</param>
  447. /// <param name="dbId">库</param>
  448. /// <returns></returns>
  449. public static long SortedSet_Trim(string key, int size, long dbId = )
  450. {
  451. using (IRedisClient redis = CreateManager(dbId).GetClient())
  452. {
  453. return redis.RemoveRangeFromSortedSet(key, size, );
  454. }
  455. }
  456. /// <summary>
  457. /// 获取SortedSet的长度
  458. /// </summary>
  459. /// <param name="key">键值</param>
  460. /// <param name="dbId">库</param>
  461. /// <returns></returns>
  462. public static long SortedSet_Count(string key, long dbId = )
  463. {
  464. using (IRedisClient redis = CreateManager(dbId).GetClient())
  465. {
  466. return redis.GetSortedSetCount(key);
  467. }
  468. }
  469.  
  470. /// <summary>
  471. /// 获取SortedSet的分页数据
  472. /// </summary>
  473. /// <typeparam name="T">类型</typeparam>
  474. /// <param name="key">键值</param>
  475. /// <param name="pageIndex">页码</param>
  476. /// <param name="pageSize">每页条数</param>
  477. /// <param name="dbId">库</param>
  478. /// <returns></returns>
  479. public static List<T> SortedSet_GetList<T>(string key, int pageIndex, int pageSize, long dbId = )
  480. {
  481. using (IRedisClient redis = CreateManager(dbId).GetClient())
  482. {
  483. var list = redis.GetRangeFromSortedSet(key, (pageIndex - ) * pageSize, pageIndex * pageSize - );
  484. if (list != null && list.Count > )
  485. {
  486. List<T> result = new List<T>();
  487. foreach (var item in list)
  488. {
  489. var data = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
  490. result.Add(data);
  491. }
  492. return result;
  493. }
  494. }
  495. return null;
  496. }
  497.  
  498. /// <summary>
  499. /// 获取SortedSet的全部数据
  500. /// </summary>
  501. /// <typeparam name="T">类</typeparam>
  502. /// <param name="key">键值</param>
  503. /// <param name="dbId">库</param>
  504. /// <returns></returns>
  505. public static List<T> SortedSet_GetListALL<T>(string key, long dbId = )
  506. {
  507. using (IRedisClient redis = CreateManager(dbId).GetClient())
  508. {
  509. var list = redis.GetRangeFromSortedSet(key, , );
  510. if (list != null && list.Count > )
  511. {
  512. List<T> result = new List<T>();
  513. foreach (var item in list)
  514. {
  515. var data = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
  516. result.Add(data);
  517. }
  518. return result;
  519. }
  520. }
  521. return null;
  522. }
  523. #endregion
  524.  
  525. #region 公用方法
  526. /// <summary>
  527. /// 设置缓存过期
  528. /// </summary>
  529. /// <param name="key">键值</param>
  530. /// <param name="datetime">过期时间</param>
  531. /// <param name="dbId">库</param>
  532. public static void SetExpire(string key, DateTime datetime, long dbId = )
  533. {
  534. using (IRedisClient redis = CreateManager(dbId).GetClient())
  535. {
  536. redis.ExpireEntryAt(key, datetime);
  537. }
  538. }
  539. #endregion
  540. }

接口实现

  1. public class CacheByRedis : ICache
  2. {
  3. #region Key-Value
  4. /// <summary>
  5. /// 读取缓存
  6. /// </summary>
  7. /// <param name="cacheKey">键</param>
  8. /// <returns></returns>
  9. public T Read<T>(string cacheKey, long dbId = ) where T : class
  10. {
  11. return RedisCache.Get<T>(RedisPrev + cacheKey, dbId);
  12. }
  13. /// <summary>
  14. /// 写入缓存
  15. /// </summary>
  16. /// <param name="value">对象数据</param>
  17. /// <param name="cacheKey">键</param>
  18. public void Write<T>(string cacheKey, T value, long dbId = ) where T : class
  19. {
  20. RedisCache.Set(RedisPrev + cacheKey, value, dbId);
  21. }
  22. /// <summary>
  23. /// 写入缓存
  24. /// </summary>
  25. /// <param name="value">对象数据</param>
  26. /// <param name="cacheKey">键</param>
  27. /// <param name="expireTime">到期时间</param>
  28. public void Write<T>(string cacheKey, T value, DateTime expireTime, long dbId = ) where T : class
  29. {
  30. RedisCache.Set(RedisPrev + cacheKey, value, expireTime, dbId);
  31. }
  32. /// <summary>
  33. /// 写入缓存
  34. /// </summary>
  35. /// <param name="value">对象数据</param>
  36. /// <param name="cacheKey">键</param>
  37. /// <param name="TimeSpan">缓存时间</param>
  38. public void Write<T>(string cacheKey, T value, TimeSpan timeSpan, long dbId = ) where T : class
  39. {
  40. RedisCache.Set(RedisPrev + cacheKey, value, timeSpan, dbId);
  41. }
  42. /// <summary>
  43. /// 移除指定数据缓存
  44. /// </summary>
  45. /// <param name="cacheKey">键</param>
  46. public void Remove(string cacheKey, long dbId = )
  47. {
  48. RedisCache.Remove(RedisPrev + cacheKey, dbId);
  49. }
  50. /// <summary>
  51. /// 移除全部缓存
  52. /// </summary>
  53. public void RemoveAll(long dbId = )
  54. {
  55. RedisCache.RemoveAll(dbId);
  56. }
  57.  
  58. /// <summary>
  59. /// 缓存前缀
  60. /// </summary>
  61. private static string _RedisPrev = "";
  62. private string RedisPrev
  63. {
  64. get
  65. {
  66. if (_RedisPrev.Length == )
  67. _RedisPrev = ConfigurationManager.AppSettings["RedisPrev"].ToString();
  68. return _RedisPrev;
  69. }
  70. }
  71. #endregion
  72. }

然后再弄个小工厂妥妥的

  1. public static ICache CaChe()
  2. {
  3. return new CacheByRedis();
  4. }

代码到此结束,使用的话自己慢慢玩

c# RedisHelper的更多相关文章

  1. Basic Tutorials of Redis(9) -First Edition RedisHelper

    After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...

  2. C# Azure 存储-分布式缓存Redis工具类 RedisHelper

    using System; using System.Collections.Generic; using Newtonsoft.Json; using StackExchange.Redis; na ...

  3. Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例

    本文目录 1. 摘要 2. Redis配置 3. RedisHelper 4.使用实例 5. 总结 1.  摘要 由于內存存取速度远高于磁盘读取的特性,为了程序效率提高性能,通常会把常用的不常变动的数 ...

  4. [C#] 使用 StackExchange.Redis 封装属于自己的 RedisHelper

    使用 StackExchange.Redis 封装属于自己的 RedisHelper 目录 核心类 ConnectionMultiplexer 字符串(String) 哈希(Hash) 列表(List ...

  5. RedisHelper帮助类

    using Newtonsoft.Json; using RedLockNet.SERedis; using RedLockNet.SERedis.Configuration; using Stack ...

  6. RedisHelper in C#

    自己写了一个RedisHelper,现贴出来,希望各位大神能够指正和优化. using System; using StackExchange.Redis; using System.Configur ...

  7. 使用 StackExchange.Redis 封装属于自己的 RedisHelper

    目录 核心类 ConnectionMultiplexer 字符串(String) 哈希(Hash) 列表(List) 有序集合(sorted set) Key 操作 发布订阅 其他 简介 目前 .NE ...

  8. RedisHelper (C#)

    <add key="RedisServers" value="172.20.2.90:9379,password=Aa+123456789" /> ...

  9. RedisHelper Redis帮助类

    using StackExchange.Redis; using System; using System.Collections.Generic; using System.IO; using Sy ...

  10. Redis:RedisHelper(5)

    /// <summary> /// Redis 助手 /// </summary> public class RedisHelper { /// <summary> ...

随机推荐

  1. 【16.52%】【codeforces 733C】Epidemic in Monstropolis

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. 获取全局上下文(getApplicationContext)_创建Shared Preference工具类_实现自动登录

    获取全局上下文(getApplicationContext)_创建Shared Preference工具类_实现自动登录 ===========================获取全局上下文(getA ...

  3. General-Purpose Operating System Protection Profile

    1 Protection Profile Introduction   This document defines the security functionality expected to be ...

  4. .net remoting 使用事件

    原文:.net remoting 使用事件 在RPC如果需要使用事件,相对是比较难的.本文告诉大家如何在 .net remoting 使用事件. 目录 使用 Channel 序列化 开发建议 修复异常 ...

  5. 回归(regression)的理解(regressor,回归子)

    1. 基本概念 回归(regression)是监督学习(given {(xi,yi)})的一个重要分类.回归用于预测输入变量(自变量,Xi)与输出变量(因变量,Yi) 之间的关系,特定是当输入变量的值 ...

  6. java学习笔记(9)——网络

    计算机网络: 最简单的网络由两台计算机组成 计算机A ---协议---> 网络 ---协议---> 计算机B---->端口1---->A软件 192.168.0.118     ...

  7. Android framework召回(3)binder使用和IBinder BpRefbase IInterface INTERFACE 之间的关系

    status_t AudioSystem::setStreamVolumeIndex(audio_stream_type_t stream, int index, audio_devices_t de ...

  8. 分布式高级(十三)Docker Container之间的数据共享

    sudo docker run -it -v /usr/lib:/usr/lib/dbdata --name dbcontainer-192.168.1.184 ubuntu:14.04 sudo d ...

  9. 计算机的组成 —— PCI(PCIE)、PCB

    1. PCI PCI 是 Peripheral Component Interconnect(外设部件互连标准)的缩写,它是目前个人电脑中使用最为广泛的接口,几乎所有的主板产品上都带有这种插槽. PC ...

  10. 张汝京:CIDM模式进可攻、退可守,建议尝试

    飞象网讯(路金娣/文)大约30多年前一些美国.日本和欧洲的IDM半导体工厂把多余的产能出来做代工服务,因为代工的公司不会与客户竞争,所以专业代工的模式成为半导体市场的新宠.那么,究竟国内半导体行业更加 ...