RedisCache.cs 16 KB

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