Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

1071 řádky
33 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Newtonsoft.Json;
  6. using StackExchange.Redis;
  7. namespace Learun.Cache.Redis
  8. {
  9. /// <summary>
  10. /// 版 本 Learun-ADMS V7.0.3 数字化智慧校园
  11. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  12. /// 创建人:框架开发组
  13. /// 日 期:2017.03.03
  14. /// 描 述:redis操作方法
  15. /// </summary>
  16. public class RedisCache
  17. {
  18. private int DbNum { get; set; }
  19. /// <summary>
  20. /// Redis连接
  21. /// </summary>
  22. private readonly ConnectionMultiplexer _conn;
  23. /// <summary>
  24. /// 设置自定义键
  25. /// </summary>
  26. public string CustomKey;
  27. #region 构造函数
  28. public RedisCache(int dbNum = 0)
  29. : this(dbNum, null)
  30. {
  31. }
  32. public RedisCache(int dbNum, string readWriteHosts)
  33. {
  34. DbNum = dbNum;
  35. _conn = string.IsNullOrWhiteSpace(readWriteHosts) ?
  36. RedisConnectionHelp.Instance :
  37. RedisConnectionHelp.GetConnectionMultiplexer(readWriteHosts);
  38. }
  39. #endregion 构造函数
  40. #region String
  41. #region 同步方法
  42. /// <summary>
  43. /// 保存单个key value
  44. /// </summary>
  45. /// <param name="key">Redis Key</param>
  46. /// <param name="value">保存的值</param>
  47. /// <param name="expiry">过期时间</param>
  48. /// <returns></returns>
  49. public bool StringSet(string key, string value, TimeSpan? expiry = default(TimeSpan?))
  50. {
  51. key = AddSysCustomKey(key);
  52. return Do(db => db.StringSet(key, value, expiry));
  53. }
  54. /// <summary>
  55. /// 保存多个key value
  56. /// </summary>
  57. /// <param name="keyValues">键值对</param>
  58. /// <returns></returns>
  59. public bool StringSet(List<KeyValuePair<RedisKey, RedisValue>> keyValues)
  60. {
  61. List<KeyValuePair<RedisKey, RedisValue>> newkeyValues = keyValues.Select(p => new KeyValuePair<RedisKey, RedisValue>(AddSysCustomKey(p.Key), p.Value)).ToList();
  62. return Do(db => db.StringSet(newkeyValues.ToArray()));
  63. }
  64. /// <summary>
  65. /// 保存一个对象
  66. /// </summary>
  67. /// <typeparam name="T"></typeparam>
  68. /// <param name="key"></param>
  69. /// <param name="obj"></param>
  70. /// <param name="expiry"></param>
  71. /// <returns></returns>
  72. public bool StringSet<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
  73. {
  74. key = AddSysCustomKey(key);
  75. string json = ConvertJson(obj);
  76. return Do(db => db.StringSet(key, json, expiry));
  77. }
  78. /// <summary>
  79. /// 获取单个key的值
  80. /// </summary>
  81. /// <param name="key">Redis Key</param>
  82. /// <returns></returns>
  83. public string StringGet(string key)
  84. {
  85. key = AddSysCustomKey(key);
  86. return Do(db => db.StringGet(key));
  87. }
  88. /// <summary>
  89. /// 获取多个Key
  90. /// </summary>
  91. /// <param name="listKey">Redis Key集合</param>
  92. /// <returns></returns>
  93. public RedisValue[] StringGet(List<string> listKey)
  94. {
  95. List<string> newKeys = listKey.Select(AddSysCustomKey).ToList();
  96. return Do(db => db.StringGet(ConvertRedisKeys(newKeys)));
  97. }
  98. /// <summary>
  99. /// 获取一个key的对象
  100. /// </summary>
  101. /// <typeparam name="T"></typeparam>
  102. /// <param name="key"></param>
  103. /// <returns></returns>
  104. public T StringGet<T>(string key)
  105. {
  106. key = AddSysCustomKey(key);
  107. return Do(db => ConvertObj<T>(db.StringGet(key)));
  108. }
  109. /// <summary>
  110. /// 为数字增长val
  111. /// </summary>
  112. /// <param name="key"></param>
  113. /// <param name="val">可以为负</param>
  114. /// <returns>增长后的值</returns>
  115. public double StringIncrement(string key, double val = 1)
  116. {
  117. key = AddSysCustomKey(key);
  118. return Do(db => db.StringIncrement(key, val));
  119. }
  120. /// <summary>
  121. /// 为数字减少val
  122. /// </summary>
  123. /// <param name="key"></param>
  124. /// <param name="val">可以为负</param>
  125. /// <returns>减少后的值</returns>
  126. public double StringDecrement(string key, double val = 1)
  127. {
  128. key = AddSysCustomKey(key);
  129. return Do(db => db.StringDecrement(key, val));
  130. }
  131. #endregion 同步方法
  132. #region 异步方法
  133. /// <summary>
  134. /// 保存单个key value
  135. /// </summary>
  136. /// <param name="key">Redis Key</param>
  137. /// <param name="value">保存的值</param>
  138. /// <param name="expiry">过期时间</param>
  139. /// <returns></returns>
  140. public async Task<bool> StringSetAsync(string key, string value, TimeSpan? expiry = default(TimeSpan?))
  141. {
  142. key = AddSysCustomKey(key);
  143. return await Do(db => db.StringSetAsync(key, value, expiry));
  144. }
  145. /// <summary>
  146. /// 保存多个key value
  147. /// </summary>
  148. /// <param name="keyValues">键值对</param>
  149. /// <returns></returns>
  150. public async Task<bool> StringSetAsync(List<KeyValuePair<RedisKey, RedisValue>> keyValues)
  151. {
  152. List<KeyValuePair<RedisKey, RedisValue>> newkeyValues =
  153. keyValues.Select(p => new KeyValuePair<RedisKey, RedisValue>(AddSysCustomKey(p.Key), p.Value)).ToList();
  154. return await Do(db => db.StringSetAsync(newkeyValues.ToArray()));
  155. }
  156. /// <summary>
  157. /// 保存一个对象
  158. /// </summary>
  159. /// <typeparam name="T"></typeparam>
  160. /// <param name="key"></param>
  161. /// <param name="obj"></param>
  162. /// <param name="expiry"></param>
  163. /// <returns></returns>
  164. public async Task<bool> StringSetAsync<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
  165. {
  166. key = AddSysCustomKey(key);
  167. string json = ConvertJson(obj);
  168. return await Do(db => db.StringSetAsync(key, json, expiry));
  169. }
  170. /// <summary>
  171. /// 获取单个key的值
  172. /// </summary>
  173. /// <param name="key">Redis Key</param>
  174. /// <returns></returns>
  175. public async Task<string> StringGetAsync(string key)
  176. {
  177. key = AddSysCustomKey(key);
  178. return await Do(db => db.StringGetAsync(key));
  179. }
  180. /// <summary>
  181. /// 获取多个Key
  182. /// </summary>
  183. /// <param name="listKey">Redis Key集合</param>
  184. /// <returns></returns>
  185. public async Task<RedisValue[]> StringGetAsync(List<string> listKey)
  186. {
  187. List<string> newKeys = listKey.Select(AddSysCustomKey).ToList();
  188. return await Do(db => db.StringGetAsync(ConvertRedisKeys(newKeys)));
  189. }
  190. /// <summary>
  191. /// 获取一个key的对象
  192. /// </summary>
  193. /// <typeparam name="T"></typeparam>
  194. /// <param name="key"></param>
  195. /// <returns></returns>
  196. public async Task<T> StringGetAsync<T>(string key)
  197. {
  198. key = AddSysCustomKey(key);
  199. string result = await Do(db => db.StringGetAsync(key));
  200. return ConvertObj<T>(result);
  201. }
  202. /// <summary>
  203. /// 为数字增长val
  204. /// </summary>
  205. /// <param name="key"></param>
  206. /// <param name="val">可以为负</param>
  207. /// <returns>增长后的值</returns>
  208. public async Task<double> StringIncrementAsync(string key, double val = 1)
  209. {
  210. key = AddSysCustomKey(key);
  211. return await Do(db => db.StringIncrementAsync(key, val));
  212. }
  213. /// <summary>
  214. /// 为数字减少val
  215. /// </summary>
  216. /// <param name="key"></param>
  217. /// <param name="val">可以为负</param>
  218. /// <returns>减少后的值</returns>
  219. public async Task<double> StringDecrementAsync(string key, double val = 1)
  220. {
  221. key = AddSysCustomKey(key);
  222. return await Do(db => db.StringDecrementAsync(key, val));
  223. }
  224. #endregion 异步方法
  225. #endregion String
  226. #region Hash
  227. #region 同步方法
  228. /// <summary>
  229. /// 判断某个数据是否已经被缓存
  230. /// </summary>
  231. /// <param name="key"></param>
  232. /// <param name="dataKey"></param>
  233. /// <returns></returns>
  234. public bool HashExists(string key, string dataKey)
  235. {
  236. key = AddSysCustomKey(key);
  237. return Do(db => db.HashExists(key, dataKey));
  238. }
  239. /// <summary>
  240. /// 存储数据到hash表
  241. /// </summary>
  242. /// <typeparam name="T"></typeparam>
  243. /// <param name="key"></param>
  244. /// <param name="dataKey"></param>
  245. /// <param name="t"></param>
  246. /// <returns></returns>
  247. public bool HashSet<T>(string key, string dataKey, T t)
  248. {
  249. key = AddSysCustomKey(key);
  250. return Do(db =>
  251. {
  252. string json = ConvertJson(t);
  253. return db.HashSet(key, dataKey, json);
  254. });
  255. }
  256. /// <summary>
  257. /// 移除hash中的某值
  258. /// </summary>
  259. /// <param name="key"></param>
  260. /// <param name="dataKey"></param>
  261. /// <returns></returns>
  262. public bool HashDelete(string key, string dataKey)
  263. {
  264. key = AddSysCustomKey(key);
  265. return Do(db => db.HashDelete(key, dataKey));
  266. }
  267. /// <summary>
  268. /// 移除hash中的多个值
  269. /// </summary>
  270. /// <param name="key"></param>
  271. /// <param name="dataKeys"></param>
  272. /// <returns></returns>
  273. public long HashDelete(string key, List<RedisValue> dataKeys)
  274. {
  275. key = AddSysCustomKey(key);
  276. //List<RedisValue> dataKeys1 = new List<RedisValue>() {"1","2"};
  277. return Do(db => db.HashDelete(key, dataKeys.ToArray()));
  278. }
  279. /// <summary>
  280. /// 从hash表获取某个值
  281. /// </summary>
  282. /// <typeparam name="T"></typeparam>
  283. /// <param name="key"></param>
  284. /// <param name="dataKey"></param>
  285. /// <returns></returns>
  286. public string HashGet(string key, string dataKey)
  287. {
  288. key = AddSysCustomKey(key);
  289. return Do(db =>
  290. {
  291. string value = db.HashGet(key, dataKey);
  292. return value;
  293. });
  294. }
  295. /// <summary>
  296. /// 从hash表获取Model
  297. /// </summary>
  298. /// <typeparam name="T"></typeparam>
  299. /// <param name="key"></param>
  300. /// <param name="dataKey"></param>
  301. /// <returns></returns>
  302. public T HashGet<T>(string key, string dataKey)
  303. {
  304. key = AddSysCustomKey(key);
  305. return Do(db =>
  306. {
  307. string value = db.HashGet(key, dataKey);
  308. return ConvertObj<T>(value);
  309. });
  310. }
  311. /// <summary>
  312. /// 从hash表获取List
  313. /// </summary>
  314. /// <typeparam name="T"></typeparam>
  315. /// <param name="key"></param>
  316. /// <param name="dataKey"></param>
  317. /// <returns></returns>
  318. public IList<T> HashGetList<T>(string key)
  319. {
  320. key = AddSysCustomKey(key);
  321. return Do(db =>
  322. {
  323. RedisValue[] values = db.HashValues(key);
  324. return ConvetList<T>(values);
  325. });
  326. }
  327. /// <summary>
  328. /// 为数字增长val
  329. /// </summary>
  330. /// <param name="key"></param>
  331. /// <param name="dataKey"></param>
  332. /// <param name="val">可以为负</param>
  333. /// <returns>增长后的值</returns>
  334. public double HashIncrement(string key, string dataKey, double val = 1)
  335. {
  336. key = AddSysCustomKey(key);
  337. return Do(db => db.HashIncrement(key, dataKey, val));
  338. }
  339. /// <summary>
  340. /// 为数字减少val
  341. /// </summary>
  342. /// <param name="key"></param>
  343. /// <param name="dataKey"></param>
  344. /// <param name="val">可以为负</param>
  345. /// <returns>减少后的值</returns>
  346. public double HashDecrement(string key, string dataKey, double val = 1)
  347. {
  348. key = AddSysCustomKey(key);
  349. return Do(db => db.HashDecrement(key, dataKey, val));
  350. }
  351. /// <summary>
  352. /// 获取hashkey所有Redis key
  353. /// </summary>
  354. /// <typeparam name="T"></typeparam>
  355. /// <param name="key"></param>
  356. /// <returns></returns>
  357. public List<T> HashKeys<T>(string key)
  358. {
  359. key = AddSysCustomKey(key);
  360. return Do(db =>
  361. {
  362. RedisValue[] values = db.HashKeys(key);
  363. return ConvetList<T>(values);
  364. });
  365. }
  366. #endregion 同步方法
  367. #region 异步方法
  368. /// <summary>
  369. /// 判断某个数据是否已经被缓存
  370. /// </summary>
  371. /// <param name="key"></param>
  372. /// <param name="dataKey"></param>
  373. /// <returns></returns>
  374. public async Task<bool> HashExistsAsync(string key, string dataKey)
  375. {
  376. key = AddSysCustomKey(key);
  377. return await Do(db => db.HashExistsAsync(key, dataKey));
  378. }
  379. /// <summary>
  380. /// 存储数据到hash表
  381. /// </summary>
  382. /// <typeparam name="T"></typeparam>
  383. /// <param name="key"></param>
  384. /// <param name="dataKey"></param>
  385. /// <param name="t"></param>
  386. /// <returns></returns>
  387. public async Task<bool> HashSetAsync<T>(string key, string dataKey, T t)
  388. {
  389. key = AddSysCustomKey(key);
  390. return await Do(db =>
  391. {
  392. string json = ConvertJson(t);
  393. return db.HashSetAsync(key, dataKey, json);
  394. });
  395. }
  396. /// <summary>
  397. /// 移除hash中的某值
  398. /// </summary>
  399. /// <param name="key"></param>
  400. /// <param name="dataKey"></param>
  401. /// <returns></returns>
  402. public async Task<bool> HashDeleteAsync(string key, string dataKey)
  403. {
  404. key = AddSysCustomKey(key);
  405. return await Do(db => db.HashDeleteAsync(key, dataKey));
  406. }
  407. /// <summary>
  408. /// 移除hash中的多个值
  409. /// </summary>
  410. /// <param name="key"></param>
  411. /// <param name="dataKeys"></param>
  412. /// <returns></returns>
  413. public async Task<long> HashDeleteAsync(string key, List<RedisValue> dataKeys)
  414. {
  415. key = AddSysCustomKey(key);
  416. //List<RedisValue> dataKeys1 = new List<RedisValue>() {"1","2"};
  417. return await Do(db => db.HashDeleteAsync(key, dataKeys.ToArray()));
  418. }
  419. /// <summary>
  420. /// 从hash表获取数据
  421. /// </summary>
  422. /// <typeparam name="T"></typeparam>
  423. /// <param name="key"></param>
  424. /// <param name="dataKey"></param>
  425. /// <returns></returns>
  426. public async Task<T> HashGeAsync<T>(string key, string dataKey)
  427. {
  428. key = AddSysCustomKey(key);
  429. string value = await Do(db => db.HashGetAsync(key, dataKey));
  430. return ConvertObj<T>(value);
  431. }
  432. /// <summary>
  433. /// 为数字增长val
  434. /// </summary>
  435. /// <param name="key"></param>
  436. /// <param name="dataKey"></param>
  437. /// <param name="val">可以为负</param>
  438. /// <returns>增长后的值</returns>
  439. public async Task<double> HashIncrementAsync(string key, string dataKey, double val = 1)
  440. {
  441. key = AddSysCustomKey(key);
  442. return await Do(db => db.HashIncrementAsync(key, dataKey, val));
  443. }
  444. /// <summary>
  445. /// 为数字减少val
  446. /// </summary>
  447. /// <param name="key"></param>
  448. /// <param name="dataKey"></param>
  449. /// <param name="val">可以为负</param>
  450. /// <returns>减少后的值</returns>
  451. public async Task<double> HashDecrementAsync(string key, string dataKey, double val = 1)
  452. {
  453. key = AddSysCustomKey(key);
  454. return await Do(db => db.HashDecrementAsync(key, dataKey, val));
  455. }
  456. /// <summary>
  457. /// 获取hashkey所有Redis key
  458. /// </summary>
  459. /// <typeparam name="T"></typeparam>
  460. /// <param name="key"></param>
  461. /// <returns></returns>
  462. public async Task<List<T>> HashKeysAsync<T>(string key)
  463. {
  464. key = AddSysCustomKey(key);
  465. RedisValue[] values = await Do(db => db.HashKeysAsync(key));
  466. return ConvetList<T>(values);
  467. }
  468. #endregion 异步方法
  469. #endregion Hash
  470. #region List
  471. #region 同步方法
  472. /// <summary>
  473. /// 移除指定ListId的内部List的值
  474. /// </summary>
  475. /// <param name="key"></param>
  476. /// <param name="value"></param>
  477. public void ListRemove<T>(string key, T value)
  478. {
  479. key = AddSysCustomKey(key);
  480. Do(db => db.ListRemove(key, ConvertJson(value)));
  481. }
  482. /// <summary>
  483. /// 获取指定key的List
  484. /// </summary>
  485. /// <param name="key"></param>
  486. /// <returns></returns>
  487. public List<T> ListRange<T>(string key)
  488. {
  489. key = AddSysCustomKey(key);
  490. return Do(redis =>
  491. {
  492. var values = redis.ListRange(key);
  493. return ConvetList<T>(values);
  494. });
  495. }
  496. /// <summary>
  497. /// 入队
  498. /// </summary>
  499. /// <param name="key"></param>
  500. /// <param name="value"></param>
  501. public void ListRightPush<T>(string key, T value)
  502. {
  503. key = AddSysCustomKey(key);
  504. Do(db => db.ListRightPush(key, ConvertJson(value)));
  505. }
  506. /// <summary>
  507. /// 出队
  508. /// </summary>
  509. /// <typeparam name="T"></typeparam>
  510. /// <param name="key"></param>
  511. /// <returns></returns>
  512. public T ListRightPop<T>(string key)
  513. {
  514. key = AddSysCustomKey(key);
  515. return Do(db =>
  516. {
  517. var value = db.ListRightPop(key);
  518. return ConvertObj<T>(value);
  519. });
  520. }
  521. /// <summary>
  522. /// 入栈
  523. /// </summary>
  524. /// <typeparam name="T"></typeparam>
  525. /// <param name="key"></param>
  526. /// <param name="value"></param>
  527. public void ListLeftPush<T>(string key, T value)
  528. {
  529. key = AddSysCustomKey(key);
  530. Do(db => db.ListLeftPush(key, ConvertJson(value)));
  531. }
  532. /// <summary>
  533. /// 出栈
  534. /// </summary>
  535. /// <typeparam name="T"></typeparam>
  536. /// <param name="key"></param>
  537. /// <returns></returns>
  538. public T ListLeftPop<T>(string key)
  539. {
  540. key = AddSysCustomKey(key);
  541. return Do(db =>
  542. {
  543. var value = db.ListLeftPop(key);
  544. return ConvertObj<T>(value);
  545. });
  546. }
  547. /// <summary>
  548. /// 获取集合中的数量
  549. /// </summary>
  550. /// <param name="key"></param>
  551. /// <returns></returns>
  552. public long ListLength(string key)
  553. {
  554. key = AddSysCustomKey(key);
  555. return Do(redis => redis.ListLength(key));
  556. }
  557. #endregion 同步方法
  558. #region 异步方法
  559. /// <summary>
  560. /// 移除指定ListId的内部List的值
  561. /// </summary>
  562. /// <param name="key"></param>
  563. /// <param name="value"></param>
  564. public async Task<long> ListRemoveAsync<T>(string key, T value)
  565. {
  566. key = AddSysCustomKey(key);
  567. return await Do(db => db.ListRemoveAsync(key, ConvertJson(value)));
  568. }
  569. /// <summary>
  570. /// 获取指定key的List
  571. /// </summary>
  572. /// <param name="key"></param>
  573. /// <returns></returns>
  574. public async Task<List<T>> ListRangeAsync<T>(string key)
  575. {
  576. key = AddSysCustomKey(key);
  577. var values = await Do(redis => redis.ListRangeAsync(key));
  578. return ConvetList<T>(values);
  579. }
  580. /// <summary>
  581. /// 入队
  582. /// </summary>
  583. /// <param name="key"></param>
  584. /// <param name="value"></param>
  585. public async Task<long> ListRightPushAsync<T>(string key, T value)
  586. {
  587. key = AddSysCustomKey(key);
  588. return await Do(db => db.ListRightPushAsync(key, ConvertJson(value)));
  589. }
  590. /// <summary>
  591. /// 出队
  592. /// </summary>
  593. /// <typeparam name="T"></typeparam>
  594. /// <param name="key"></param>
  595. /// <returns></returns>
  596. public async Task<T> ListRightPopAsync<T>(string key)
  597. {
  598. key = AddSysCustomKey(key);
  599. var value = await Do(db => db.ListRightPopAsync(key));
  600. return ConvertObj<T>(value);
  601. }
  602. /// <summary>
  603. /// 入栈
  604. /// </summary>
  605. /// <typeparam name="T"></typeparam>
  606. /// <param name="key"></param>
  607. /// <param name="value"></param>
  608. public async Task<long> ListLeftPushAsync<T>(string key, T value)
  609. {
  610. key = AddSysCustomKey(key);
  611. return await Do(db => db.ListLeftPushAsync(key, ConvertJson(value)));
  612. }
  613. /// <summary>
  614. /// 出栈
  615. /// </summary>
  616. /// <typeparam name="T"></typeparam>
  617. /// <param name="key"></param>
  618. /// <returns></returns>
  619. public async Task<T> ListLeftPopAsync<T>(string key)
  620. {
  621. key = AddSysCustomKey(key);
  622. var value = await Do(db => db.ListLeftPopAsync(key));
  623. return ConvertObj<T>(value);
  624. }
  625. /// <summary>
  626. /// 获取集合中的数量
  627. /// </summary>
  628. /// <param name="key"></param>
  629. /// <returns></returns>
  630. public async Task<long> ListLengthAsync(string key)
  631. {
  632. key = AddSysCustomKey(key);
  633. return await Do(redis => redis.ListLengthAsync(key));
  634. }
  635. #endregion 异步方法
  636. #endregion List
  637. #region SortedSet 有序集合
  638. #region 同步方法
  639. /// <summary>
  640. /// 添加
  641. /// </summary>
  642. /// <param name="key"></param>
  643. /// <param name="value"></param>
  644. /// <param name="score"></param>
  645. public bool SortedSetAdd<T>(string key, T value, double score)
  646. {
  647. key = AddSysCustomKey(key);
  648. return Do(redis => redis.SortedSetAdd(key, ConvertJson<T>(value), score));
  649. }
  650. /// <summary>
  651. /// 删除
  652. /// </summary>
  653. /// <param name="key"></param>
  654. /// <param name="value"></param>
  655. public bool SortedSetRemove<T>(string key, T value)
  656. {
  657. key = AddSysCustomKey(key);
  658. return Do(redis => redis.SortedSetRemove(key, ConvertJson(value)));
  659. }
  660. /// <summary>
  661. /// 获取全部
  662. /// </summary>
  663. /// <param name="key"></param>
  664. /// <returns></returns>
  665. public List<T> SortedSetRangeByRank<T>(string key)
  666. {
  667. key = AddSysCustomKey(key);
  668. return Do(redis =>
  669. {
  670. var values = redis.SortedSetRangeByRank(key);
  671. return ConvetList<T>(values);
  672. });
  673. }
  674. /// <summary>
  675. /// 获取集合中的数量
  676. /// </summary>
  677. /// <param name="key"></param>
  678. /// <returns></returns>
  679. public long SortedSetLength(string key)
  680. {
  681. key = AddSysCustomKey(key);
  682. return Do(redis => redis.SortedSetLength(key));
  683. }
  684. #endregion 同步方法
  685. #region 异步方法
  686. /// <summary>
  687. /// 添加
  688. /// </summary>
  689. /// <param name="key"></param>
  690. /// <param name="value"></param>
  691. /// <param name="score"></param>
  692. public async Task<bool> SortedSetAddAsync<T>(string key, T value, double score)
  693. {
  694. key = AddSysCustomKey(key);
  695. return await Do(redis => redis.SortedSetAddAsync(key, ConvertJson<T>(value), score));
  696. }
  697. /// <summary>
  698. /// 删除
  699. /// </summary>
  700. /// <param name="key"></param>
  701. /// <param name="value"></param>
  702. public async Task<bool> SortedSetRemoveAsync<T>(string key, T value)
  703. {
  704. key = AddSysCustomKey(key);
  705. return await Do(redis => redis.SortedSetRemoveAsync(key, ConvertJson(value)));
  706. }
  707. /// <summary>
  708. /// 获取全部
  709. /// </summary>
  710. /// <param name="key"></param>
  711. /// <returns></returns>
  712. public async Task<List<T>> SortedSetRangeByRankAsync<T>(string key)
  713. {
  714. key = AddSysCustomKey(key);
  715. var values = await Do(redis => redis.SortedSetRangeByRankAsync(key));
  716. return ConvetList<T>(values);
  717. }
  718. /// <summary>
  719. /// 获取集合中的数量
  720. /// </summary>
  721. /// <param name="key"></param>
  722. /// <returns></returns>
  723. public async Task<long> SortedSetLengthAsync(string key)
  724. {
  725. key = AddSysCustomKey(key);
  726. return await Do(redis => redis.SortedSetLengthAsync(key));
  727. }
  728. #endregion 异步方法
  729. #endregion SortedSet 有序集合
  730. #region key
  731. /// <summary>
  732. /// 删除单个key
  733. /// </summary>
  734. /// <param name="key">redis key</param>
  735. /// <returns>是否删除成功</returns>
  736. public bool KeyDelete(string key)
  737. {
  738. key = AddSysCustomKey(key);
  739. return Do(db => db.KeyDelete(key));
  740. }
  741. /// <summary>
  742. /// 删除多个key
  743. /// </summary>
  744. /// <param name="keys">rediskey</param>
  745. /// <returns>成功删除的个数</returns>
  746. public long KeyDelete(List<string> keys)
  747. {
  748. List<string> newKeys = keys.Select(AddSysCustomKey).ToList();
  749. return Do(db => db.KeyDelete(ConvertRedisKeys(newKeys)));
  750. }
  751. /// <summary>
  752. /// 判断key是否存储
  753. /// </summary>
  754. /// <param name="key">redis key</param>
  755. /// <returns></returns>
  756. public bool KeyExists(string key)
  757. {
  758. key = AddSysCustomKey(key);
  759. return Do(db => db.KeyExists(key));
  760. }
  761. /// <summary>
  762. /// 重新命名key
  763. /// </summary>
  764. /// <param name="key">就的redis key</param>
  765. /// <param name="newKey">新的redis key</param>
  766. /// <returns></returns>
  767. public bool KeyRename(string key, string newKey)
  768. {
  769. key = AddSysCustomKey(key);
  770. return Do(db => db.KeyRename(key, newKey));
  771. }
  772. /// <summary>
  773. /// 设置Key的时间
  774. /// </summary>
  775. /// <param name="key">redis key</param>
  776. /// <param name="expiry"></param>
  777. /// <returns></returns>
  778. public bool KeyExpire(string key, TimeSpan? expiry = default(TimeSpan?))
  779. {
  780. key = AddSysCustomKey(key);
  781. return Do(db => db.KeyExpire(key, expiry));
  782. }
  783. #endregion key
  784. #region 发布订阅
  785. /// <summary>
  786. /// Redis发布订阅 订阅
  787. /// </summary>
  788. /// <param name="subChannel"></param>
  789. /// <param name="handler"></param>
  790. public void Subscribe(string subChannel, Action<RedisChannel, RedisValue> handler = null)
  791. {
  792. ISubscriber sub = _conn.GetSubscriber();
  793. sub.Subscribe(subChannel, (channel, message) =>
  794. {
  795. //接收订阅消息,处理逻辑
  796. if (handler == null)
  797. {
  798. Console.WriteLine(subChannel + " 订阅收到消息:" + message);
  799. }
  800. else
  801. {
  802. handler(channel, message);
  803. }
  804. });
  805. //写法二
  806. sub.Subscribe(subChannel, new Action<RedisChannel, RedisValue>((channel, message) =>
  807. {
  808. Console.WriteLine(subChannel + " 订阅收到消息:" + message);
  809. }));
  810. //写法三
  811. sub.Subscribe(subChannel, new Action<RedisChannel, RedisValue>(GetMessage));
  812. }
  813. /// <summary>
  814. /// 获取订阅消息(写法三)
  815. /// </summary>
  816. /// <param name="channel"></param>
  817. /// <param name="message"></param>
  818. static void GetMessage(RedisChannel channel, RedisValue message)
  819. {
  820. Console.WriteLine(channel + " 订阅收到消息:" + message);
  821. }
  822. /// <summary>
  823. /// Redis发布订阅 发布
  824. /// </summary>
  825. /// <typeparam name="T"></typeparam>
  826. /// <param name="channel"></param>
  827. /// <param name="msg"></param>
  828. /// <returns></returns>
  829. public long Publish<T>(string channel, T msg)
  830. {
  831. ISubscriber sub = _conn.GetSubscriber();
  832. return sub.Publish(channel, ConvertJson(msg));
  833. }
  834. /// <summary>
  835. /// Redis发布订阅 取消订阅
  836. /// </summary>
  837. /// <param name="channel"></param>
  838. public void Unsubscribe(string channel)
  839. {
  840. ISubscriber sub = _conn.GetSubscriber();
  841. sub.Unsubscribe(channel);
  842. }
  843. /// <summary>
  844. /// Redis发布订阅 取消全部订阅
  845. /// </summary>
  846. public void UnsubscribeAll()
  847. {
  848. ISubscriber sub = _conn.GetSubscriber();
  849. sub.UnsubscribeAll();
  850. }
  851. #endregion 发布订阅
  852. #region 其他
  853. /// <summary>
  854. /// 获取事物
  855. /// </summary>
  856. /// <returns></returns>
  857. public ITransaction CreateTransaction()
  858. {
  859. return GetDatabase().CreateTransaction();
  860. }
  861. /// <summary>
  862. /// 获取批处理(管道模式)
  863. /// </summary>
  864. /// <returns></returns>
  865. public IBatch CreateBatch()
  866. {
  867. return GetDatabase().CreateBatch();
  868. }
  869. public IDatabase GetDatabase()
  870. {
  871. return _conn.GetDatabase(DbNum);
  872. }
  873. public IServer GetServer(string hostAndPort)
  874. {
  875. return _conn.GetServer(hostAndPort);
  876. }
  877. public void FlushDatabase(int dbNum)
  878. {
  879. var list = _conn.GetEndPoints();
  880. foreach (var item in list)
  881. {
  882. _conn.GetServer(item).FlushDatabase(dbNum);
  883. }
  884. }
  885. /// <summary>
  886. /// 设置前缀
  887. /// </summary>
  888. /// <param name="customKey">自定义键</param>
  889. public void SetSysCustomKey(string customKey)
  890. {
  891. CustomKey = customKey;
  892. }
  893. #endregion 其他
  894. #region 辅助方法
  895. /// <summary>
  896. /// 添加自定义键
  897. /// </summary>
  898. /// <param name="oldKey"></param>
  899. /// <returns></returns>
  900. private string AddSysCustomKey(string oldKey)
  901. {
  902. var prefixKey = CustomKey ?? RedisConnectionHelp.SysCustomKey;
  903. return prefixKey + oldKey;
  904. }
  905. private T Do<T>(Func<IDatabase, T> func)
  906. {
  907. var database = _conn.GetDatabase(DbNum);
  908. return func(database);
  909. }
  910. /// <summary>
  911. /// 序列化
  912. /// </summary>
  913. /// <typeparam name="T">类</typeparam>
  914. /// <param name="value">值</param>
  915. /// <returns></returns>
  916. private string ConvertJson<T>(T value)
  917. {
  918. string result = value is string ? value.ToString() : JsonConvert.SerializeObject(value);
  919. return result;
  920. }
  921. /// <summary>
  922. /// 反序列化
  923. /// </summary>
  924. /// <typeparam name="T"></typeparam>
  925. /// <param name="value"></param>
  926. /// <returns></returns>
  927. private T ConvertObj<T>(RedisValue value)
  928. {
  929. if (value.IsNullOrEmpty)
  930. {
  931. return default(T);
  932. }
  933. if (typeof(T) == typeof(string))
  934. {
  935. return (T)Convert.ChangeType(value, typeof(T));
  936. }
  937. return JsonConvert.DeserializeObject<T>(value);
  938. }
  939. private List<T> ConvetList<T>(RedisValue[] values)
  940. {
  941. List<T> result = new List<T>();
  942. foreach (var item in values)
  943. {
  944. if (!item.IsNullOrEmpty)
  945. {
  946. var model = ConvertObj<T>(item);
  947. result.Add(model);
  948. }
  949. }
  950. return result;
  951. }
  952. private RedisKey[] ConvertRedisKeys(List<string> redisKeys)
  953. {
  954. return redisKeys.Select(redisKey => (RedisKey)redisKey).ToArray();
  955. }
  956. #endregion 辅助方法
  957. }
  958. }