RedisUtils.java 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. package com.ydtech.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ydtech.components.MyJedisPoolConfig;
  4. import redis.clients.jedis.*;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. /**
  9. * 基于jedis的redis操作工具类
  10. *
  11. * @author: lig
  12. * @date: 2023年03月13日 0013
  13. */
  14. public class RedisUtils {
  15. /*
  16. 除了该工具类提供的方法外,还可以在外面调用getJedis()方法,获取到jedis实例后,调用它原生的api来操作
  17. */
  18. /**
  19. * 获取jedis对象,并选择redis库。jedis默认是0号库,可传入1-16之间的数选择库存放数据
  20. * 原则上使用一个redis库存放数据,通过特定的key的命令规则来区分不同的数据就行了。
  21. *
  22. * @param index redis库号。使用可变参数的目的就是该参数可传可不传。
  23. * @return 返回jedis对象
  24. */
  25. public static Jedis getJedis(int... index) {
  26. Jedis jedis = MyJedisPoolConfig.getJedisPool().getResource();
  27. if (index != null && index.length > 0) {
  28. if (index[0] > 0 && index[0] <= 16) {
  29. jedis.select(index[0]);
  30. }
  31. }
  32. return jedis;
  33. }
  34. /*######################## key的操作 ################################*/
  35. /**
  36. * 根据pattern返回当前库中的key
  37. *
  38. * @param pattern
  39. * @return
  40. */
  41. public static Set<String> keys(String pattern) {
  42. Jedis jedis = null;
  43. Set<String> keys = null;
  44. try {
  45. jedis = getJedis();
  46. keys = jedis.keys(pattern);
  47. } finally {
  48. if (jedis != null) {
  49. jedis.close();
  50. }
  51. }
  52. return keys;
  53. }
  54. /**
  55. * 删除一个或多个key
  56. *
  57. * @param key 一个或多个key
  58. */
  59. public static Long del(String... key) {
  60. Jedis jedis = null;
  61. Long delNum = 0L;
  62. try {
  63. jedis = getJedis();
  64. delNum = jedis.del(key);
  65. } finally {
  66. if (jedis != null) {
  67. jedis.close();
  68. }
  69. }
  70. return delNum;
  71. }
  72. /**
  73. * 批量删除
  74. *
  75. * @param keyList 要删除的key的集合
  76. */
  77. public static void mdel(List<String> keyList) {
  78. Jedis jedis = getJedis();
  79. //获取pipeline
  80. Pipeline pipeline = jedis.pipelined();
  81. for (String key : keyList) {
  82. pipeline.del(key);
  83. }
  84. //执行结果同步,这样才能保证结果的正确性。实际上不执行该方法也执行了上面的命令,但是结果确不一定完全正确。
  85. //注意
  86. pipeline.sync();
  87. //关闭连接
  88. jedis.close();
  89. }
  90. /**
  91. * 判断某个key是否还存在
  92. *
  93. * @param key key
  94. * @return
  95. */
  96. public static Boolean exists(String key) {
  97. Jedis jedis = null;
  98. Boolean flag = false;
  99. try {
  100. jedis = getJedis();
  101. flag = jedis.exists(key);
  102. } finally {
  103. if (jedis != null) {
  104. jedis.close();
  105. }
  106. }
  107. return flag;
  108. }
  109. /**
  110. * 设置某个key的过期时间,单位秒
  111. *
  112. * @param key key
  113. * @param seconds 过期时间秒
  114. */
  115. public static void expire(String key, int seconds) {
  116. Jedis jedis = null;
  117. try {
  118. jedis = getJedis();
  119. jedis.expire(key, seconds);
  120. } finally {
  121. if (jedis != null) {
  122. jedis.close();
  123. }
  124. }
  125. }
  126. /**
  127. * 查看某个key还有几秒过期,-1表示永不过期 ,-2表示已过期
  128. *
  129. * @param key key
  130. * @return
  131. */
  132. public static Long timeToLive(String key) {
  133. Jedis jedis = null;
  134. Long ttl;
  135. try {
  136. jedis = getJedis();
  137. ttl = jedis.ttl(key);
  138. } finally {
  139. if (jedis != null) {
  140. jedis.close();
  141. }
  142. }
  143. return ttl;
  144. }
  145. /**
  146. * 查看某个key对应的value的类型
  147. *
  148. * @param key
  149. * @return
  150. */
  151. public static String type(String key) {
  152. Jedis jedis = null;
  153. String type = null;
  154. try {
  155. jedis = getJedis();
  156. type = jedis.type(key);
  157. } finally {
  158. if (jedis != null) {
  159. jedis.close();
  160. }
  161. }
  162. return type;
  163. }
  164. /*######################## string(字符串)的操作 ####################*/
  165. /**
  166. * 获取某个key的value,类型要对,只能value是string的才能获取
  167. *
  168. * @param key
  169. * @return
  170. */
  171. public static String get(String key) {
  172. Jedis jedis = null;
  173. String value = null;
  174. try {
  175. jedis = getJedis();
  176. value = jedis.get(key);
  177. } finally {
  178. if (jedis != null) {
  179. jedis.close();
  180. }
  181. }
  182. return value;
  183. }
  184. /**
  185. * 获取某个key的value 对象
  186. *
  187. * @param key
  188. * @return
  189. */
  190. public static Object getObject(String key) {
  191. Jedis jedis = null;
  192. String value = null;
  193. try {
  194. jedis = getJedis();
  195. value = jedis.get(key);
  196. } finally {
  197. if (jedis != null) {
  198. jedis.close();
  199. }
  200. }
  201. Object obj = JSON.parse(value);
  202. return obj;
  203. }
  204. /**
  205. * 设置某个key的value
  206. *
  207. * @param key
  208. * @param value
  209. */
  210. public static void set(String key, String value) {
  211. Jedis jedis = null;
  212. try {
  213. jedis = getJedis();
  214. jedis.set(key, value);
  215. } finally {
  216. if (jedis != null) {
  217. jedis.close();
  218. }
  219. }
  220. }
  221. /**
  222. * 设置某个key的value
  223. *
  224. * @param key
  225. * @param value
  226. */
  227. public static void setObject(String key, Object obj) {
  228. String value = JSON.toJSONString(obj);
  229. Jedis jedis = null;
  230. try {
  231. jedis = getJedis();
  232. jedis.set(key, value);
  233. } finally {
  234. if (jedis != null) {
  235. jedis.close();
  236. }
  237. }
  238. }
  239. /**
  240. * 设置某个key的value 包含过期时间
  241. *
  242. * @param key
  243. * @param value
  244. * @param seconds 过期时间
  245. */
  246. public static void set(String key, String value ,int seconds) {
  247. Jedis jedis = null;
  248. try {
  249. jedis = getJedis();
  250. jedis.set(key, value);
  251. jedis.expire(key, seconds);
  252. } finally {
  253. if (jedis != null) {
  254. jedis.close();
  255. }
  256. }
  257. }
  258. /**
  259. * 字符串后追加内容
  260. *
  261. * @param key key
  262. * @param appendContent 要追加的内容
  263. */
  264. public static void append(String key, String appendContent) {
  265. Jedis jedis = null;
  266. try {
  267. jedis = getJedis();
  268. jedis.append(key, appendContent);
  269. } finally {
  270. if (jedis != null) {
  271. jedis.close();
  272. }
  273. }
  274. }
  275. /**
  276. * 返回key的value的长度
  277. *
  278. * @param key
  279. * @return
  280. */
  281. public static Long strlen(String key) {
  282. Jedis jedis = null;
  283. Long strLen = 0L;
  284. try {
  285. jedis = getJedis();
  286. strLen = jedis.strlen(key);
  287. } finally {
  288. if (jedis != null) {
  289. jedis.close();
  290. }
  291. }
  292. return strLen;
  293. }
  294. /**
  295. * value 加1 必
  296. * 须是字符型数字
  297. *
  298. * @param key
  299. * @return 增加后的值
  300. */
  301. public static Long incr(String key) {
  302. Jedis jedis = null;
  303. Long incrResult = 0L;
  304. try {
  305. jedis = getJedis();
  306. incrResult = jedis.incr(key);
  307. } finally {
  308. if (jedis != null) {
  309. jedis.close();
  310. }
  311. }
  312. return incrResult;
  313. }
  314. /**
  315. * value 减1 必须是字符型数字
  316. *
  317. * @param key
  318. * @return
  319. */
  320. public static Long decr(String key) {
  321. Jedis jedis = null;
  322. Long decrResult = 0L;
  323. try {
  324. jedis = getJedis();
  325. decrResult = jedis.decr(key);
  326. } finally {
  327. if (jedis != null) {
  328. jedis.close();
  329. }
  330. }
  331. return decrResult;
  332. }
  333. /**
  334. * value 加increment
  335. *
  336. * @param key key
  337. * @param increment 加几
  338. * @return
  339. */
  340. public static Long incrby(String key, int increment) {
  341. Jedis jedis = null;
  342. Long incrByResult = 0L;
  343. try {
  344. jedis = getJedis();
  345. incrByResult = jedis.incrBy(key, increment);
  346. } finally {
  347. if (jedis != null) {
  348. jedis.close();
  349. }
  350. }
  351. return incrByResult;
  352. }
  353. /**
  354. * value 减increment
  355. *
  356. * @param key
  357. * @param increment
  358. * @return
  359. */
  360. public static Long decrby(String key, int increment) {
  361. Jedis jedis = null;
  362. Long decrByResult = 0L;
  363. try {
  364. jedis = getJedis();
  365. decrByResult = jedis.decrBy(key, increment);
  366. } finally {
  367. if (jedis != null) {
  368. jedis.close();
  369. }
  370. }
  371. return decrByResult;
  372. }
  373. /**
  374. * 给某个key设置过期时间和value,成功返回OK
  375. *
  376. * @param key key
  377. * @param seconds 过期时间秒
  378. * @param value 设置的值
  379. * @return
  380. */
  381. public static String setex(String key, int seconds, String value) {
  382. Jedis jedis = null;
  383. String result = null;
  384. try {
  385. jedis = getJedis();
  386. result = jedis.setex(key, seconds, value);
  387. } finally {
  388. if (jedis != null) {
  389. jedis.close();
  390. }
  391. }
  392. return result;
  393. }
  394. /*######################## list(列表)的操作 #######################*/
  395. //lpush rpush lpop rpop lrange lindex llen lset
  396. /**
  397. * 从左边向列表中添加值
  398. *
  399. * @param key key
  400. * @param str 要添加的值
  401. */
  402. public static void lpush(String key, String str) {
  403. Jedis jedis = null;
  404. try {
  405. jedis = getJedis();
  406. jedis.lpush(key, str);
  407. } finally {
  408. if (jedis != null) {
  409. jedis.close();
  410. }
  411. }
  412. }
  413. /**
  414. * 从右边向列表中添加值
  415. *
  416. * @param key key
  417. * @param str 要添加的值
  418. */
  419. public static void rpush(String key, String str) {
  420. Jedis jedis = null;
  421. try {
  422. jedis = getJedis();
  423. jedis.rpush(key, str);
  424. } finally {
  425. if (jedis != null) {
  426. jedis.close();
  427. }
  428. }
  429. }
  430. /**
  431. * 从左边取出一个列表中的值
  432. *
  433. * @param key
  434. * @return
  435. */
  436. public static String lpop(String key) {
  437. Jedis jedis = null;
  438. String lpop = null;
  439. try {
  440. jedis = getJedis();
  441. lpop = jedis.lpop(key);
  442. } finally {
  443. if (jedis != null) {
  444. jedis.close();
  445. }
  446. }
  447. return lpop;
  448. }
  449. /**
  450. * 从右边取出一个列表中的值
  451. *
  452. * @param key
  453. * @return
  454. */
  455. public static String rpop(String key) {
  456. Jedis jedis = null;
  457. String rpop = null;
  458. try {
  459. jedis = getJedis();
  460. rpop = jedis.rpop(key);
  461. } finally {
  462. if (jedis != null) {
  463. jedis.close();
  464. }
  465. }
  466. return rpop;
  467. }
  468. /**
  469. * 取出列表中指定范围内的值,0 到 -1 表示全部
  470. *
  471. * @param key
  472. * @param startIndex
  473. * @param endIndex
  474. * @return
  475. */
  476. public static List<String> lrange(String key, int startIndex, int endIndex) {
  477. Jedis jedis = null;
  478. List<String> result = null;
  479. try {
  480. jedis = getJedis();
  481. result = jedis.lrange(key, startIndex, endIndex);
  482. } finally {
  483. if (jedis != null) {
  484. jedis.close();
  485. }
  486. }
  487. return result;
  488. }
  489. /**
  490. * 返回某列表指定索引位置的值
  491. *
  492. * @param key 列表key
  493. * @param index 索引位置
  494. * @return
  495. */
  496. public static String lindex(String key, int index) {
  497. Jedis jedis = null;
  498. String lindex = null;
  499. try {
  500. jedis = getJedis();
  501. lindex = jedis.lindex(key, index);
  502. } finally {
  503. if (jedis != null) {
  504. jedis.close();
  505. }
  506. }
  507. return lindex;
  508. }
  509. /**
  510. * 返回某列表的长度
  511. *
  512. * @param key
  513. * @return
  514. */
  515. public static Long llen(String key) {
  516. Jedis jedis = null;
  517. Long llen = 0L;
  518. try {
  519. jedis = getJedis();
  520. llen = jedis.llen(key);
  521. } finally {
  522. if (jedis != null) {
  523. jedis.close();
  524. }
  525. }
  526. return llen;
  527. }
  528. /**
  529. * 给某列表指定位置设置为指定的值
  530. *
  531. * @param key
  532. * @param index
  533. * @param str
  534. */
  535. public static void lset(String key, Long index, String str) {
  536. Jedis jedis = null;
  537. try {
  538. jedis = getJedis();
  539. jedis.lset(key, index, str);
  540. } finally {
  541. if (jedis != null) {
  542. jedis.close();
  543. }
  544. }
  545. }
  546. /**
  547. * 对列表进行剪裁,保留指定闭区间的元素(索引位置也会重排)
  548. *
  549. * @param key 列表key
  550. * @param startIndex 开始索引位置
  551. * @param endIndex 结束索引位置
  552. */
  553. public static void ltrim(String key, Integer startIndex, Integer endIndex) {
  554. Jedis jedis = null;
  555. try {
  556. jedis = getJedis();
  557. jedis.ltrim(key, startIndex, endIndex);
  558. } finally {
  559. if (jedis != null) {
  560. jedis.close();
  561. }
  562. }
  563. }
  564. /**
  565. * 从列表的左边阻塞弹出一个元素
  566. *
  567. * @param key 列表的key
  568. * @param timeout 阻塞超时时间,0表示若没有元素就永久阻塞
  569. * @return
  570. */
  571. public static List<String> blpop(String key, Integer timeout) {
  572. Jedis jedis = null;
  573. List<String> valueList = null;
  574. try {
  575. jedis = getJedis();
  576. valueList = jedis.blpop(timeout, key);
  577. } finally {
  578. if (jedis != null) {
  579. jedis.close();
  580. }
  581. }
  582. return valueList;
  583. }
  584. /**
  585. * 从列表的右边阻塞弹出一个元素
  586. *
  587. * @param key 列表的key
  588. * @param timeout 阻塞超时时间,0表示若没有元素就永久阻塞
  589. * @return
  590. */
  591. public static List<String> brpop(String key, Integer timeout) {
  592. Jedis jedis = null;
  593. List<String> valueList = null;
  594. try {
  595. jedis = getJedis();
  596. valueList = jedis.brpop(timeout, key);
  597. } finally {
  598. if (jedis != null) {
  599. jedis.close();
  600. }
  601. }
  602. return valueList;
  603. }
  604. /*######################## hash(哈希表)的操作 #######################*/
  605. //hset hget hmset hmget hgetall hdel hkeys hvals hexists hincrby
  606. /**
  607. * 给某个hash表设置一个键值对
  608. *
  609. * @param key
  610. * @param field
  611. * @param value
  612. */
  613. public static void hset(String key, String field, String value) {
  614. Jedis jedis = null;
  615. try {
  616. jedis = getJedis();
  617. jedis.hset(key, field, value);
  618. } finally {
  619. if (jedis != null) {
  620. jedis.close();
  621. }
  622. }
  623. }
  624. /**
  625. * 取出某个hash表中某个field对应的value
  626. *
  627. * @param key key
  628. * @param field field
  629. * @return
  630. */
  631. public static String hget(String key, String field) {
  632. Jedis jedis = null;
  633. String hget = null;
  634. try {
  635. jedis = getJedis();
  636. hget = jedis.hget(key, field);
  637. } finally {
  638. if (jedis != null) {
  639. jedis.close();
  640. }
  641. }
  642. return hget;
  643. }
  644. /**
  645. * 某个hash表设置一个或多个键值对
  646. *
  647. * @param key
  648. * @param kvMap
  649. */
  650. public static void hmset(String key, Map<String, String> kvMap) {
  651. Jedis jedis = null;
  652. try {
  653. jedis = getJedis();
  654. jedis.hmset(key, kvMap);
  655. } finally {
  656. if (jedis != null) {
  657. jedis.close();
  658. }
  659. }
  660. }
  661. /**
  662. * 取出某个hash表中任意多个key对应的value的集合
  663. *
  664. * @param key
  665. * @param fields
  666. * @return
  667. */
  668. public static List<String> hmget(String key, String... fields) {
  669. Jedis jedis = null;
  670. List<String> hmget = null;
  671. try {
  672. jedis = getJedis();
  673. hmget = jedis.hmget(key, fields);
  674. } finally {
  675. if (jedis != null) {
  676. jedis.close();
  677. }
  678. }
  679. return hmget;
  680. }
  681. /**
  682. * 取出某个hash表中所有的键值对
  683. *
  684. * @param key
  685. * @return
  686. */
  687. public static Map<String, String> hgetall(String key) {
  688. Jedis jedis = null;
  689. Map<String, String> kvMap = null;
  690. try {
  691. jedis = getJedis();
  692. kvMap = jedis.hgetAll(key);
  693. } finally {
  694. if (jedis != null) {
  695. jedis.close();
  696. }
  697. }
  698. return kvMap;
  699. }
  700. /**
  701. * 判断某个hash表中的某个key是否存在
  702. *
  703. * @param key
  704. * @param field
  705. * @return
  706. */
  707. public static Boolean hexists(String key, String field) {
  708. Jedis jedis = null;
  709. Boolean exists = null;
  710. try {
  711. jedis = getJedis();
  712. exists = jedis.hexists(key, field);
  713. } finally {
  714. if (jedis != null) {
  715. jedis.close();
  716. }
  717. }
  718. return exists;
  719. }
  720. /**
  721. * 返回某个hash表中所有的key
  722. *
  723. * @param key
  724. * @return
  725. */
  726. public static Set<String> hkeys(String key) {
  727. Jedis jedis = null;
  728. Set<String> keys = null;
  729. try {
  730. jedis = getJedis();
  731. keys = jedis.hkeys(key);
  732. } finally {
  733. if (jedis != null) {
  734. jedis.close();
  735. }
  736. }
  737. return keys;
  738. }
  739. /**
  740. * 返回某个hash表中所有的value
  741. *
  742. * @param key
  743. * @return
  744. */
  745. public static List<String> hvals(String key) {
  746. Jedis jedis = null;
  747. List<String> hvals = null;
  748. try {
  749. jedis = getJedis();
  750. hvals = jedis.hvals(key);
  751. } finally {
  752. if (jedis != null) {
  753. jedis.close();
  754. }
  755. }
  756. return hvals;
  757. }
  758. /**
  759. * 删除某个hash表中的一个或多个键值对
  760. *
  761. * @param key
  762. * @param fields
  763. */
  764. public static void hdel(String key, String... fields) {
  765. Jedis jedis = null;
  766. try {
  767. jedis = getJedis();
  768. jedis.hdel(key, fields);
  769. } finally {
  770. if (jedis != null) {
  771. jedis.close();
  772. }
  773. }
  774. }
  775. /**
  776. * 给某个hash表中的某个field的value增加多少
  777. *
  778. * @param key hash表的key
  779. * @param field 表中的某个field
  780. * @param increment 增加多少
  781. * @return
  782. */
  783. public static Long hincrby(String key, String field, Long increment) {
  784. Jedis jedis = null;
  785. Long result = null;
  786. try {
  787. jedis = getJedis();
  788. result = jedis.hincrBy(key, field, increment);
  789. } finally {
  790. if (jedis != null) {
  791. jedis.close();
  792. }
  793. }
  794. return result;
  795. }
  796. /*######################## set(集合)的操作 ###########################*/
  797. /**
  798. * 往set集合中添加一个或多个元素
  799. *
  800. * @param key key
  801. * @param members 要添加的元素
  802. * @return 添加成功的元素个数
  803. */
  804. public static Long sadd(String key, String... members) {
  805. Jedis jedis = null;
  806. Long num = 0L;
  807. try {
  808. jedis = getJedis();
  809. num = jedis.sadd(key, members);
  810. } finally {
  811. if (jedis != null) {
  812. jedis.close();
  813. }
  814. }
  815. return num;
  816. }
  817. /**
  818. * 返回set集合中的所有元素,顺序与加入时的顺序一致
  819. *
  820. * @param key key
  821. * @return
  822. */
  823. public static Set<String> smembers(String key) {
  824. Jedis jedis = null;
  825. Set<String> members = null;
  826. try {
  827. jedis = getJedis();
  828. members = jedis.smembers(key);
  829. } finally {
  830. if (jedis != null) {
  831. jedis.close();
  832. }
  833. }
  834. return members;
  835. }
  836. /**
  837. * 判断集合中是否存在某个元素
  838. *
  839. * @param key key
  840. * @param member 某个元素
  841. * @return true存在,false不存在
  842. */
  843. public static Boolean sismember(String key, String member) {
  844. Jedis jedis = null;
  845. Boolean isMember = false;
  846. try {
  847. jedis = getJedis();
  848. isMember = jedis.sismember(key, member);
  849. } finally {
  850. if (jedis != null) {
  851. jedis.close();
  852. }
  853. }
  854. return isMember;
  855. }
  856. /**
  857. * 返回set集合的长度
  858. *
  859. * @param key key
  860. * @return
  861. */
  862. public static Long scard(String key) {
  863. Jedis jedis = null;
  864. Long len = 0L;
  865. try {
  866. jedis = getJedis();
  867. len = jedis.scard(key);
  868. } finally {
  869. if (jedis != null) {
  870. jedis.close();
  871. }
  872. }
  873. return len;
  874. }
  875. /**
  876. * 删除set集合中指定的一个或多个元素
  877. *
  878. * @param key
  879. * @param members 要删除的元素
  880. * @return 删除成功的元素个数
  881. */
  882. public static Long srem(String key, String... members) {
  883. Jedis jedis = null;
  884. Long num = 0L;
  885. try {
  886. jedis = getJedis();
  887. num = jedis.srem(key, members);
  888. } finally {
  889. if (jedis != null) {
  890. jedis.close();
  891. }
  892. }
  893. return num;
  894. }
  895. /**
  896. * 将key1中的元素key1Member移动到key2中
  897. *
  898. * @param key1 来源集合key
  899. * @param key2 目的地集合key
  900. * @param key1Member key1中的元素
  901. * @return 1成功,0失败
  902. */
  903. public static Long smove(String key1, String key2, String key1Member) {
  904. Jedis jedis = null;
  905. Long num = 0L;
  906. try {
  907. jedis = getJedis();
  908. num = jedis.smove(key1, key2, key1Member);
  909. } finally {
  910. if (jedis != null) {
  911. jedis.close();
  912. }
  913. }
  914. return num;
  915. }
  916. /**
  917. * 随机查询返回集合中的指定个数的元素(若count为负数,返回的元素可能会重复)
  918. *
  919. * @param key key
  920. * @param count 要查询返回的元素个数
  921. * @return 元素list集合
  922. */
  923. public static List<String> srandmember(String key, int count) {
  924. Jedis jedis = null;
  925. List<String> members = null;
  926. try {
  927. jedis = getJedis();
  928. members = jedis.srandmember(key, count);
  929. } finally {
  930. if (jedis != null) {
  931. jedis.close();
  932. }
  933. }
  934. return members;
  935. }
  936. /**
  937. * 从set集合中随机弹出指定个数个元素
  938. *
  939. * @param key key
  940. * @param count 要弹出的个数
  941. * @return 随机弹出的元素
  942. */
  943. public static Set<String> spop(String key, int count) {
  944. Jedis jedis = null;
  945. Set<String> members = null;
  946. try {
  947. jedis = getJedis();
  948. members = jedis.spop(key, count);
  949. } finally {
  950. if (jedis != null) {
  951. jedis.close();
  952. }
  953. }
  954. return members;
  955. }
  956. /**
  957. * 求交集,返回多个set集合相交的部分
  958. *
  959. * @param setKeys 多个set集合的key
  960. * @return 相交的元素集合
  961. */
  962. public static Set<String> sinter(String... setKeys) {
  963. Jedis jedis = null;
  964. Set<String> members = null;
  965. try {
  966. jedis = getJedis();
  967. members = jedis.sinter(setKeys);
  968. } finally {
  969. if (jedis != null) {
  970. jedis.close();
  971. }
  972. }
  973. return members;
  974. }
  975. /**
  976. * 求并集,求几个set集合的并集(因为set中不会有重复的元素,合并后的集合也不会有重复的元素)
  977. *
  978. * @param setKeys 多个set的key
  979. * @return 合并后的集合
  980. */
  981. public static Set<String> sunion(String... setKeys) {
  982. Jedis jedis = null;
  983. Set<String> members = null;
  984. try {
  985. jedis = getJedis();
  986. members = jedis.sunion(setKeys);
  987. } finally {
  988. if (jedis != null) {
  989. jedis.close();
  990. }
  991. }
  992. return members;
  993. }
  994. /**
  995. * 求差集,求几个集合之间的差集
  996. *
  997. * @param setKeys 多个set的key
  998. * @return 差集
  999. */
  1000. public static Set<String> sdiff(String... setKeys) {
  1001. Jedis jedis = null;
  1002. Set<String> members = null;
  1003. try {
  1004. jedis = getJedis();
  1005. members = jedis.sdiff(setKeys);
  1006. } finally {
  1007. if (jedis != null) {
  1008. jedis.close();
  1009. }
  1010. }
  1011. return members;
  1012. }
  1013. /*######################## zset(有序集合)的操作 #######################*/
  1014. /**
  1015. * 添加一个元素到zset
  1016. *
  1017. * @param key key
  1018. * @param score 元素的分数
  1019. * @param member 元素
  1020. * @return 成功添加的元素个数
  1021. */
  1022. public static Long zadd(String key, double score, String member) {
  1023. Jedis jedis = null;
  1024. Long num = null;
  1025. try {
  1026. jedis = getJedis();
  1027. num = jedis.zadd(key, score, member);
  1028. } finally {
  1029. if (jedis != null) {
  1030. jedis.close();
  1031. }
  1032. }
  1033. return num;
  1034. }
  1035. /**
  1036. * 添加一个或多个元素到zset
  1037. *
  1038. * @param key key
  1039. * @param memberScores 元素和分数的map集合,map的k是元素,v是分数
  1040. * @return 成功添加的元素个数
  1041. */
  1042. public static Long zadd(String key, Map<String, Double> memberScores) {
  1043. Jedis jedis = null;
  1044. Long num = null;
  1045. try {
  1046. jedis = getJedis();
  1047. num = jedis.zadd(key, memberScores);
  1048. } finally {
  1049. if (jedis != null) {
  1050. jedis.close();
  1051. }
  1052. }
  1053. return num;
  1054. }
  1055. /**
  1056. * 查询指定闭区间的元素(根据分数升序)
  1057. * (0,-1表示全部)
  1058. *
  1059. * @param key zset的key
  1060. * @param start 开始区间
  1061. * @param end 结束区间
  1062. * @return 元素集合
  1063. */
  1064. public static Set<String> zrange(String key, long start, long end) {
  1065. Jedis jedis = null;
  1066. Set<String> ascResult = null;
  1067. try {
  1068. jedis = getJedis();
  1069. ascResult = jedis.zrange(key, start, end);
  1070. } finally {
  1071. if (jedis != null) {
  1072. jedis.close();
  1073. }
  1074. }
  1075. return ascResult;
  1076. }
  1077. /**
  1078. * 查询指定闭区间的元素,带着分数
  1079. * (0,-1表示全部)
  1080. *
  1081. * @param key zset的key
  1082. * @param start 开始区间
  1083. * @param end 结束区间
  1084. * @return 元素集合 格式为:
  1085. * [{"element":"a","score":10.0,"binaryElement":"YQ=="},{"element":"b","score":20.0,"binaryElement":"Yg=="}]
  1086. */
  1087. public static Set<Tuple> zrangeWithScores(String key, long start, long end) {
  1088. Jedis jedis = null;
  1089. Set<Tuple> ascResult = null;
  1090. try {
  1091. jedis = getJedis();
  1092. ascResult = jedis.zrangeWithScores(key, start, end);
  1093. } finally {
  1094. if (jedis != null) {
  1095. jedis.close();
  1096. }
  1097. }
  1098. return ascResult;
  1099. }
  1100. /**
  1101. * 查询指定索引闭区间的元素(根据分数降序)
  1102. * (0,-1表示全部)
  1103. *
  1104. * @param key zset的key
  1105. * @param start 开始区间
  1106. * @param end 结束区间
  1107. * @return 元素集合
  1108. */
  1109. public static Set<String> zrevrange(String key, long start, long end) {
  1110. Jedis jedis = null;
  1111. Set<String> descResult = null;
  1112. try {
  1113. jedis = getJedis();
  1114. descResult = jedis.zrevrange(key, start, end);
  1115. } finally {
  1116. if (jedis != null) {
  1117. jedis.close();
  1118. }
  1119. }
  1120. return descResult;
  1121. }
  1122. /**
  1123. * 查询指定索引闭区间的元素,带着分数(根据分数降序)
  1124. * (0,-1表示全部)
  1125. *
  1126. * @param key zset的key
  1127. * @param start 开始区间
  1128. * @param end 结束区间
  1129. * @return 元素集合 格式为:
  1130. * [{"element":"b","score":20.0,"binaryElement":"Yg=="},{"element":"a","score":10.0,"binaryElement":"YQ=="}]
  1131. */
  1132. public static Set<Tuple> zrevrangeWithScores(String key, long start, long end) {
  1133. Jedis jedis = null;
  1134. Set<Tuple> descResult = null;
  1135. try {
  1136. jedis = getJedis();
  1137. descResult = jedis.zrevrangeWithScores(key, start, end);
  1138. } finally {
  1139. if (jedis != null) {
  1140. jedis.close();
  1141. }
  1142. }
  1143. return descResult;
  1144. }
  1145. /**
  1146. * 返回有序集合(zset)中的元素个数
  1147. *
  1148. * @param key key
  1149. * @return 个数
  1150. */
  1151. public static Long zcard(String key) {
  1152. Jedis jedis = null;
  1153. Long count = null;
  1154. try {
  1155. jedis = getJedis();
  1156. count = jedis.zcard(key);
  1157. } finally {
  1158. if (jedis != null) {
  1159. jedis.close();
  1160. }
  1161. }
  1162. return count;
  1163. }
  1164. /**
  1165. * 返回指定分数区间的元素个数(闭区间)
  1166. *
  1167. * @param key key
  1168. * @param startScore 开始分数
  1169. * @param endScore 结束分数
  1170. * @return 个数
  1171. */
  1172. public static Long zcount(String key, Long startScore, Long endScore) {
  1173. Jedis jedis = null;
  1174. Long count = null;
  1175. try {
  1176. jedis = getJedis();
  1177. count = jedis.zcount(key, startScore, endScore);
  1178. } finally {
  1179. if (jedis != null) {
  1180. jedis.close();
  1181. }
  1182. }
  1183. return count;
  1184. }
  1185. /**
  1186. * 返回某元素在集合中的排名(根据分数降序排列时)
  1187. *
  1188. * @param key key
  1189. * @param member 集合中的元素
  1190. * @return 排名,从0开始
  1191. */
  1192. public static Long zrevrank(String key, String member) {
  1193. Jedis jedis = null;
  1194. Long count = null;
  1195. try {
  1196. jedis = getJedis();
  1197. count = jedis.zrevrank(key, member);
  1198. } finally {
  1199. if (jedis != null) {
  1200. jedis.close();
  1201. }
  1202. }
  1203. return count;
  1204. }
  1205. /**
  1206. * 返回某元素在集合中的排名(根据分数升序排列时)
  1207. *
  1208. * @param key key
  1209. * @param member 集合中的元素
  1210. * @return 排名,从0开始
  1211. */
  1212. public static Long zrank(String key, String member) {
  1213. Jedis jedis = null;
  1214. Long count = null;
  1215. try {
  1216. jedis = getJedis();
  1217. count = jedis.zrank(key, member);
  1218. } finally {
  1219. if (jedis != null) {
  1220. jedis.close();
  1221. }
  1222. }
  1223. return count;
  1224. }
  1225. /**
  1226. * 升序查询指定分数闭区间的元素
  1227. *
  1228. * @param key key
  1229. * @param min 最小分数
  1230. * @param max 最大分数
  1231. * @return 元素集合
  1232. */
  1233. public static Set<String> zrangeByScore(String key, double min, double max) {
  1234. Jedis jedis = null;
  1235. Set<String> members = null;
  1236. try {
  1237. jedis = getJedis();
  1238. members = jedis.zrangeByScore(key, min, max);
  1239. } finally {
  1240. if (jedis != null) {
  1241. jedis.close();
  1242. }
  1243. }
  1244. return members;
  1245. }
  1246. /**
  1247. * 升序查询指定分数闭区间的元素,并指定偏移量
  1248. *
  1249. * @param key key
  1250. * @param min 最小分数
  1251. * @param max 最大分数
  1252. * @param offset 偏移量
  1253. * @param size 数量
  1254. * @return 元素集合
  1255. */
  1256. public static Set<String> zrangeByScore(String key, double min, double max, int offset, int size) {
  1257. Jedis jedis = null;
  1258. Set<String> members = null;
  1259. try {
  1260. jedis = getJedis();
  1261. members = jedis.zrangeByScore(key, min, max, offset, size);
  1262. } finally {
  1263. if (jedis != null) {
  1264. jedis.close();
  1265. }
  1266. }
  1267. return members;
  1268. }
  1269. /**
  1270. * 升序查询指定分数闭区间的元素,带着分数
  1271. *
  1272. * @param key key
  1273. * @param min 最小分数
  1274. * @param max 最大分数
  1275. * @return 元素集合
  1276. */
  1277. public static Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
  1278. Jedis jedis = null;
  1279. Set<Tuple> members = null;
  1280. try {
  1281. jedis = getJedis();
  1282. members = jedis.zrangeByScoreWithScores(key, min, max);
  1283. } finally {
  1284. if (jedis != null) {
  1285. jedis.close();
  1286. }
  1287. }
  1288. return members;
  1289. }
  1290. /**
  1291. * 升序查询指定分数闭区间的元素,并指定偏移量,带着分数
  1292. *
  1293. * @param key key
  1294. * @param min 最小分数
  1295. * @param max 最大分数
  1296. * @param offset 偏移量
  1297. * @param size 数量
  1298. * @return 元素集合
  1299. */
  1300. public static Set<Tuple> zrangeByScoreWithScores(String key, double min, double max, int offset, int size) {
  1301. Jedis jedis = null;
  1302. Set<Tuple> members = null;
  1303. try {
  1304. jedis = getJedis();
  1305. members = jedis.zrangeByScoreWithScores(key, min, max, offset, size);
  1306. } finally {
  1307. if (jedis != null) {
  1308. jedis.close();
  1309. }
  1310. }
  1311. return members;
  1312. }
  1313. /**
  1314. * 降序查询指定分数闭区间的元素
  1315. *
  1316. * @param key key
  1317. * @param max 最大分数
  1318. * @param min 最小分数
  1319. * @return 元素集合
  1320. */
  1321. public static Set<String> zrevrangebyscore(String key, double max, double min) {
  1322. Jedis jedis = null;
  1323. Set<String> members = null;
  1324. try {
  1325. jedis = getJedis();
  1326. members = jedis.zrevrangeByScore(key, max, min);
  1327. } finally {
  1328. if (jedis != null) {
  1329. jedis.close();
  1330. }
  1331. }
  1332. return members;
  1333. }
  1334. /**
  1335. * 降序查询指定分数闭区间的元素,并指定偏移量
  1336. *
  1337. * @param key key
  1338. * @param max 最大分数
  1339. * @param min 最小分数
  1340. * @return 元素集合
  1341. */
  1342. public static Set<String> zrevrangebyscore(String key, double max, double min, int offset, int size) {
  1343. Jedis jedis = null;
  1344. Set<String> members = null;
  1345. try {
  1346. jedis = getJedis();
  1347. members = jedis.zrevrangeByScore(key, max, min, offset, size);
  1348. } finally {
  1349. if (jedis != null) {
  1350. jedis.close();
  1351. }
  1352. }
  1353. return members;
  1354. }
  1355. /**
  1356. * 降序查询指定分数闭区间的元素,并带着分数
  1357. *
  1358. * @param key key
  1359. * @param min 最小分数
  1360. * @param max 最大分数
  1361. * @return 元素集合
  1362. */
  1363. public static Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min) {
  1364. Jedis jedis = null;
  1365. Set<Tuple> members = null;
  1366. try {
  1367. jedis = getJedis();
  1368. members = jedis.zrevrangeByScoreWithScores(key, max, min);
  1369. } finally {
  1370. if (jedis != null) {
  1371. jedis.close();
  1372. }
  1373. }
  1374. return members;
  1375. }
  1376. /**
  1377. * 降序查询指定分数闭区间的元素,并指定偏移量,带着分数
  1378. *
  1379. * @param key key
  1380. * @param min 最小分数
  1381. * @param max 最大分数
  1382. * @param offset 偏移量
  1383. * @param size 数量
  1384. * @return 元素集合
  1385. */
  1386. public static Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min, int offset, int size) {
  1387. Jedis jedis = null;
  1388. Set<Tuple> members = null;
  1389. try {
  1390. jedis = getJedis();
  1391. members = jedis.zrevrangeByScoreWithScores(key, min, max, offset, size);
  1392. } finally {
  1393. if (jedis != null) {
  1394. jedis.close();
  1395. }
  1396. }
  1397. return members;
  1398. }
  1399. /**
  1400. * 有序集合中指定删除一或多个元素
  1401. *
  1402. * @param key
  1403. * @param member 元素
  1404. * @return 删除成功的个数
  1405. */
  1406. public static Long zrem(String key, String... member) {
  1407. Jedis jedis = null;
  1408. Long num = null;
  1409. try {
  1410. jedis = getJedis();
  1411. num = jedis.zrem(key, member);
  1412. } finally {
  1413. if (jedis != null) {
  1414. jedis.close();
  1415. }
  1416. }
  1417. return num;
  1418. }
  1419. /**
  1420. * 分数升序排名时,删除指定索引区间的元素
  1421. *
  1422. * @param key key
  1423. * @param start 开始索引
  1424. * @param end 结束索引
  1425. * @return 删除成功的个数
  1426. */
  1427. public static Long zremrangebyrank(String key, long start, long end) {
  1428. Jedis jedis = null;
  1429. Long num = null;
  1430. try {
  1431. jedis = getJedis();
  1432. num = jedis.zremrangeByRank(key, start, end);
  1433. } finally {
  1434. if (jedis != null) {
  1435. jedis.close();
  1436. }
  1437. }
  1438. return num;
  1439. }
  1440. /**
  1441. * 分数升序排名时,删除指定分数区间的元素
  1442. *
  1443. * @param key key
  1444. * @param min 最小分数
  1445. * @param max 最大分数
  1446. * @return 删除成功的个数
  1447. */
  1448. public static Long zremrangeByScore(String key, long min, long max) {
  1449. Jedis jedis = null;
  1450. Long num = null;
  1451. try {
  1452. jedis = getJedis();
  1453. num = jedis.zremrangeByScore(key, min, max);
  1454. } finally {
  1455. if (jedis != null) {
  1456. jedis.close();
  1457. }
  1458. }
  1459. return num;
  1460. }
  1461. /**
  1462. * 查询有序集合中某元素的分数
  1463. *
  1464. * @param key key
  1465. * @param member 元素
  1466. * @return 删除成功的个数
  1467. */
  1468. public static Double zscore(String key, String member) {
  1469. Jedis jedis = null;
  1470. Double score = null;
  1471. try {
  1472. jedis = getJedis();
  1473. score = jedis.zscore(key, member);
  1474. } finally {
  1475. if (jedis != null) {
  1476. jedis.close();
  1477. }
  1478. }
  1479. return score;
  1480. }
  1481. /**
  1482. * 给有序集合中某元素的分数增加(正数)或减少(负数)
  1483. *
  1484. * @param key key
  1485. * @param score 分数
  1486. * @param member 元素
  1487. * @return 新的分数
  1488. */
  1489. public static Double zincrby(String key, double score, String member) {
  1490. Jedis jedis = null;
  1491. Double newScore = null;
  1492. try {
  1493. jedis = getJedis();
  1494. newScore = jedis.zincrby(key, score, member);
  1495. } finally {
  1496. if (jedis != null) {
  1497. jedis.close();
  1498. }
  1499. }
  1500. return newScore;
  1501. }
  1502. /**
  1503. * 私有化构造器,不让实例化对象
  1504. */
  1505. private RedisUtils() {
  1506. }
  1507. public static void main(String[] args) {
  1508. // JedisPool jedisPool = MyJedisPoolConfig.getJedisPool();
  1509. //// JedisPool jedisPool1 = MyJedisPoolConfig.getJedisPool();
  1510. //// System.out.println(jedisPool == jedisPool1);//true
  1511. //
  1512. // Jedis j = new Jedis("127.0.0.1",6379);
  1513. //
  1514. // System.out.println(j.ping());
  1515. JedisPoolConfig poolConfig = new JedisPoolConfig();
  1516. //========= jedisPool的一些配置=============================
  1517. poolConfig.setMaxTotal(10000);//最大连接数
  1518. poolConfig.setMaxIdle(50);//最多空闲数
  1519. poolConfig.setMaxWaitMillis(5 * 1000);//当池中没有连接时,最多等待5秒
  1520. JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379, 3000, null);
  1521. Jedis j = jedisPool.getResource();
  1522. System.out.println(j.ping());
  1523. }
  1524. }