jedis操作

  1. package com.wxj.common;

  2.  
  3. import java.util.List;

  4. import java.util.Map;

  5. import java.util.Set;

  6.  
  7. import redis.clients.jedis.BinaryClient.LIST_POSITION;

  8. import redis.clients.jedis.Jedis;

  9. import redis.clients.jedis.JedisPool;

  10. import redis.clients.jedis.SortingParams;

  11. import redis.clients.util.SafeEncoder;

  12.  
  13. public class JedisUtil {

  14. /*缓存生存时间 */

  15. private int expire = 60000;

  16.  
  17. /*对key的操作方法 */

  18. private static JedisPool jedisPool = null;

  19.  
  20. public JedisPool getPool(){

  21. return jedisPool;

  22. }

  23.  
  24. /*从jedispool中获取jedis对象*/

  25. public Jedis getJedis(){

  26. if(jedisPool == null){

  27. throw new NullPointerException();

  28. }

  29. return jedisPool.getResource();

  30. }

  31.  
  32. public static JedisPool getJedisPool(){

  33. return jedisPool;

  34. }

  35.  
  36. public static void setJedisPool(JedisPool jedisPool){

  37. JedisUtil.jedisPool = jedisPool;

  38. }

  39.  
  40.  
  41. /*

  42. * 在finaally中回收jedis

  43. */

  44. public void returnJedis(Jedis jedis){

  45. if(null != jedis && null != jedisPool){

  46. jedisPool.returnResource(jedis);

  47. }

  48. }

  49.  
  50. /*

  51. * 销毁链接(放入catch

  52. */

  53. public static void returnBrokenResource(Jedis jedis){

  54. if (null !=jedis && null !=jedisPool) {

  55. jedisPool.returnResource(jedis);

  56. }

  57. }

  58.  
  59. /*

  60. * 设置过期时间

  61. */

  62. public void expire(String key,int seconds){

  63. if(seconds<0){

  64. return;

  65. }

  66. Jedis jedis = getJedis();

  67. jedis.expire(key, seconds);

  68. returnJedis(jedis);

  69. }

  70.  
  71. /*

  72. * 设置默认过期时间

  73. */

  74. public void expire(String key){

  75. expire(key,expire);

  76. }

  77.  
  78. /*

  79. * 清空所有key

  80. */

  81. public String flushAll(){

  82. Jedis jedis = getJedis();

  83. String stata = jedis.flushAll();

  84. returnJedis(jedis);

  85. return stata;

  86. }

  87.  
  88. /*

  89. * 更改key 返回值是状态吗

  90. */

  91.  
  92. public String rename(String oldkey,String newkey){

  93. return rename(SafeEncoder.encode(oldkey),SafeEncoder.encode(newkey));

  94. }

  95.  
  96. /*

  97. * 更改key,仅当新key不存在时才执行

  98. */

  99. public long renamenx(String oldkey,String newkey){

  100. Jedis jedis = getJedis();

  101. long status = jedis.renamenx(oldkey, newkey);

  102. returnJedis(jedis);

  103. return status;

  104.  
  105. }

  106.  
  107. /*

  108. * 更改key

  109. */

  110. public String rename(byte[] oldkey,byte[] newkey){

  111. Jedis jedis = getJedis();

  112. String status = jedis.rename(oldkey,newkey);

  113. returnJedis(jedis);

  114. return status;

  115. }

  116.  
  117. /*

  118. * 设置key的过期时间,以秒为单位 返回值是影响的记录数

  119. */

  120. public long expired(String key,int seconds){

  121. Jedis jedis = getJedis();

  122. long count = jedis.expire(key, seconds);

  123. returnJedis(jedis);

  124. return count;

  125. }

  126.  
  127. /*

  128. * 设置key的过期时间,它是距历元(即格林威治标准时间 1970年1月1日的00:00:00,格里高利历)的偏移量

  129. */

  130. public long expireAt(String key,long timestamp){

  131. Jedis jedis = getJedis();

  132. long count = jedis.expireAt(key, timestamp);

  133. returnJedis(jedis);

  134. return count;

  135. }

  136. /*

  137. * 查询key的过期时间 以秒为单位的时间表示返回的是指定key的剩余的生存时间

  138. */

  139. public long ttl(String key){

  140. Jedis sjedis = getJedis();

  141. long len = sjedis.ttl(key);

  142. returnJedis(sjedis);

  143. return len;

  144.  
  145. }

  146. /*

  147. * 取消对key过期时间的设置 将带生存时间的转换成一个永不过期的key

  148. *

  149. * 当移除成功时返回1,key不存在或者移除不成功时返回0

  150. */

  151. public long persist(String key){

  152. Jedis jedis = getJedis();

  153. long count = jedis.persist(key);

  154. returnJedis(jedis);

  155. return count;

  156. }

  157.  
  158. /*

  159. * 删除keys对应的记录,可以是多个key

  160. *

  161. * 返回值是被删除的数量

  162. */

  163. public long del(String...keys){

  164. Jedis jedis = getJedis();

  165. long count = jedis.del(keys);

  166. returnJedis(jedis) ;

  167. return count;

  168. }

  169.  
  170. /*

  171. * 删除keys对应的记录,可以是多个key

  172. */

  173. public long del(byte[]...keys){

  174. Jedis jedis = getJedis();

  175. long count = jedis.del(keys);

  176. returnJedis(jedis);

  177. return count;

  178. }

  179. /*

  180. * 判断key是否存在

  181. */

  182. public boolean exists(String key){

  183. Jedis jedis = getJedis();

  184. boolean exists = jedis.exists(key);

  185. returnJedis(jedis);

  186. return exists;

  187. }

  188.  
  189. /*

  190. * 对List,set,SortSet 进行排序,如果集合数据较大应避免使用这个方法

  191. *

  192. * 返回排序后的结果,默认升序 sort key Desc为降序

  193. */

  194. public List<String> sort(String key){

  195. Jedis jedis = getJedis();

  196. List<String> list = jedis.sort(key);

  197.  
  198. returnJedis(jedis);

  199. return list;

  200. }

  201.  
  202. /*

  203. * 对List,set,SortSet 进行排序,如果集合数据较大应避免使用这个方法

  204. *

  205. * 返回排序后的结果,默认升序 sort key Desc为降序

  206. */

  207. public List<String> sort(String key,SortingParams parame){

  208. Jedis jedis = getJedis();

  209. List<String> list = jedis.sort(key,parame);

  210. returnJedis(jedis);

  211. return list;

  212. }

  213. /*

  214. * 返回指定key的存储类型

  215. */

  216.  
  217. public String type(String key){

  218. Jedis jedis = getJedis();

  219. String type = jedis.type(key);

  220. returnJedis(jedis);

  221. return type;

  222. }

  223.  
  224. /*

  225. * 查找所有匹配模式的键

  226. *

  227. * key的查询表达式 *代表任意多个 ?代表一个

  228. */

  229. public Set<String> Keys(String pattern){

  230. Jedis jedis = getJedis();

  231. Set<String> set = jedis.keys(pattern);

  232.  
  233. returnJedis(jedis);

  234. return set;

  235. }

  236.  
  237. /*************************set部分*******************************/

  238. /*

  239. * 向set添加一条记录,如果member已经存在则返回0,否则返回1

  240. */

  241. public long sadd(String key,String member){

  242. Jedis jedis = getJedis();

  243.  
  244. Long s = jedis.sadd(key, member);

  245. returnJedis(jedis);

  246. return s;

  247. }

  248.  
  249. public long sadd(byte[] key,byte[] member){

  250. Jedis jedis = getJedis();

  251. Long s = jedis.sadd(key, member);

  252. returnJedis(jedis);

  253. return s;

  254. }

  255.  
  256. /*

  257. * 获取给定key中元素个数

  258. *

  259. * @return 元素个数

  260. */

  261. public long scard(String key){

  262. Jedis jedis = getJedis();

  263. Long count = jedis.scard(key);

  264. returnJedis(jedis);

  265. return count;

  266. }

  267.  
  268. /*

  269. * 返回从第一组和所有的给定集合之间的有差异的成员

  270. *

  271. * @return 有差异成员的集合

  272. */

  273. public Set<String> sdiff(String...keys){

  274. Jedis jedis = getJedis();

  275. Set<String> set = jedis.sdiff(keys);

  276. returnJedis(jedis);

  277. return set;

  278. }

  279.  
  280. /*

  281. * 这个命令的作用和 SDIFF 类似,但它将结果保存到 destination 集合,而不是简单地返回结果集,如果目标已存在,则覆盖

  282. *

  283. * @return 新集合的记录数

  284. */

  285. public long sdiffstore(String newkey,String...keys){

  286. Jedis jedis = getJedis();

  287. Long count = jedis.sdiffstore(newkey, keys);

  288. returnJedis(jedis);

  289. return count;

  290. }

  291.  
  292. /*

  293. * sinter 返回给定集合交集成员,如果其中一个集合为不存在或为空,则返回空set

  294. * @return 交集成员的集合

  295. */

  296. public Set<String> sinter(String...keys){

  297. Jedis jedis = getJedis();

  298. Set<String> set = jedis.sinter(keys);

  299. returnJedis(jedis);

  300. return set;

  301. }

  302. /*

  303. * sinterstore 这个命令类似于 SINTER 命令,但它将结果保存到 destination 集合,而不是简单地返回结果集。

  304. * 如果 destination 集合已经存在,则将其覆盖。destination 可以是 key 本身

  305. *

  306. * @return 新集合的记录数

  307. */

  308. public long sinterstore(String dstkey,String...keys){

  309. Jedis jedis = getJedis();

  310. long count = jedis.sinterstore(dstkey, keys);

  311. returnJedis(jedis);

  312. return count;

  313. }

  314. /*

  315. * sismember 确定一个给定的值是否存在

  316. * @param String member 要判断的值

  317. * @return 存在返回1,不存在返回0

  318. */

  319. public boolean sismember(String key,String member){

  320. Jedis jedis = getJedis();

  321. Boolean s = jedis.sismember(key,member);

  322. returnJedis(jedis);

  323. return s;

  324. }

  325. /*

  326. * smembers 返回集合中的所有成员

  327. * @return 成员集合

  328. */

  329.  
  330. public Set<String> smembers(String key){

  331. Jedis jedis = getJedis();

  332. Set<String> set = jedis.smembers(key);

  333. returnJedis(jedis);

  334. return set;

  335. }

  336.  
  337. public Set<byte[]> smembers(byte[] key){

  338. Jedis jedis = getJedis();

  339. Set<byte[]> set = jedis.smembers(key);

  340. returnJedis(jedis);

  341. return set;

  342. }

  343.  
  344. /*

  345. * smove 将成员从源集合移除放入目标集合 </br>

  346. * 如果源集合不存在或不包含指定成员,不进行任何操作,返回0</br>

  347. * 否则该成员从源集合上删除,并添加到目标集合,如果目标集合成员以存在,则只在源集合进行删除

  348. * @param srckey 源集合 dstkey目标集合 member源集合中的成员

  349. *

  350. * @return 状态码 1成功 0失败

  351. */

  352. public long smove(String srckey,String dstkey,String member){

  353. Jedis jedis = getJedis();

  354. Long s = jedis.smove(srckey, dstkey, member);

  355. returnJedis(jedis);

  356. return s;

  357. }

  358. /*

  359. * spop 从集合中删除成员 移除并返回集合中的一个随机元素。

  360. *

  361. * @return 被删除的随机成员

  362. */

  363. public String spop(String key){

  364. Jedis jedis = getJedis();

  365. String s = jedis.spop(key); //s 被移除的随机成员

  366. returnJedis(jedis);

  367. return s;

  368. }

  369. /*

  370. * 从集合中删除指定成员

  371. * 移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略。当 key 不是集合类型,返回一个错误。

  372. *

  373. * @param key member要删除的成员

  374. * @return 状态码 成功返回1,成员不存在返回0

  375. */

  376. public long srem(String key,String member){

  377. Jedis jedis = getJedis();

  378. Long s = jedis.srem(key, member);

  379. returnJedis(jedis);

  380. return s;

  381. }

  382.  
  383. /*

  384. * sunion 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖

  385. */

  386. public Set<String> sunion(String...keys){

  387. Jedis jedis = getJedis();

  388. Set<String> set = jedis.sunion();

  389. returnJedis(jedis);

  390. return set;

  391. }

  392.  
  393. /*

  394. *这个命令类似于 SUNION 命令,但它将结果保存到 destination 集合,而不是简单地返回结果集。

  395. *如果 destination 已经存在,则将其覆盖。 destination 可以是 key 本身

  396. */

  397. public long sunionstore(String dstkey,String...keys){

  398. Jedis jedis = getJedis();

  399. Long s = jedis.sunionstore(dstkey, keys);

  400. returnJedis(jedis);

  401. return s;

  402. }

  403.  
  404.  
  405.  
  406. /******************************SortSet******************************/

  407.  
  408. /*

  409. * zadd 向集合中增加一条记录,如果这个值已经存在,这个值对应的权重将被置为新的权重

  410. *

  411. * @param double score 权重 member要加入的值

  412. * @return 状态码 1成功 0已经存在member值

  413. */

  414.  
  415. public long zadd(String key,double score,String member){

  416. Jedis jedis = getJedis();

  417. long s = jedis.zadd(key, score,member);

  418. returnJedis(jedis);

  419. return s;

  420. }

  421. /*

  422. * 获取集合中元素的数量

  423. * @param String key

  424. * @return 当 key 存在且是有序集类型时,返回有序集的基数。 当 key 不存在时,返回 0 。

  425. */

  426. public long zcard(String key){

  427. Jedis jedis = getJedis();

  428. long count= jedis.zcard(key);

  429. returnJedis(jedis);

  430. return count;

  431. }

  432. /*

  433. * zcount 获取指定权重区间内的集合数量

  434. *

  435. * @param double min最小排序位置 max最大排序位置

  436. */

  437. public long zcount(String key,double min,double max){

  438. Jedis jedis = getJedis();

  439. long count = jedis.zcount(key,min,max);

  440. returnJedis(jedis);

  441. return count;

  442. }

  443. /*

  444. * zrange 返回有序集合key中,指定区间的成员0,-1指的是整个区间的成员

  445. */

  446. public Set<String> zrange(String key,int start,int end){

  447.  
  448. Jedis jedis = getJedis();

  449. Set<String> set = jedis.zrange(key,0,-1);

  450. returnJedis(jedis);

  451. return set;

  452. }

  453.  
  454.  
  455.  
  456. /*

  457. * zrevrange 返回有序集 key 中,指定区间内的成员。其中成员的位置按 score 值递减(从大到小)来排列

  458. */

  459. public Set<String> zrevrange(String key, int start, int end) {

  460. // ShardedJedis sjedis = getShardedJedis();

  461. Jedis sjedis = getJedis();

  462. Set<String> set = sjedis.zrevrange(key, start, end);

  463. returnJedis(sjedis);

  464. return set;

  465. }

  466.  
  467. /*

  468. * zrangeByScore 根据上下权重查询集合

  469. */

  470. public Set<String> zrangeByScore(String key,double min,double max){

  471. Jedis jedis = getJedis();

  472. Set<String> set = jedis.zrangeByScore(key, min, max);

  473. returnJedis(jedis);

  474. return set;

  475. }

  476. /*

  477. * 接上面方法,获取有序集合长度

  478. */

  479. public long zlength(String key){

  480. long len = 0;

  481. Set<String> set = zrange(key,0,-1);

  482. len = set.size();

  483. return len;

  484. }

  485.  
  486. /*

  487. * zincrby 为有序集 key 的成员 member 的 score 值加上增量 increment

  488. *

  489. * @return member 成员的新 score 值,以字符串形式表示

  490. */

  491.  
  492. public double zincrby(String key,double score,String member){

  493. Jedis jedis = getJedis();

  494. double s = jedis.zincrby(key, score, member);

  495. returnJedis(jedis);

  496. return s;

  497. }

  498. /*

  499. * zrank 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递增(从小到大)顺序排列

  500. */

  501.  
  502. public long zrank(String key,String member){

  503. Jedis jedis = getJedis();

  504. long index = jedis.zrank(key, member);

  505. returnJedis(jedis);

  506. return index;

  507. }

  508.  
  509. /*

  510. *zrevrank 返回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递减(从大到小)排序。

  511. */

  512. public long zrevrank(String key,String member){

  513. Jedis jedis = getJedis();

  514. long index = jedis.zrevrank(key, member);

  515. returnJedis(jedis);

  516. return index;

  517. }

  518.  
  519. /*

  520. * zrem 移除有序集 key 中的一个或多个成员,不存在的成员将被忽略。当 key 存在但不是有序集类型时,返回一个错误。在 Redis 2.4 版本以前, ZREM 每次只能删除一个元素。

  521. * @return 被成功移除的成员的数量,不包括被忽略的成员

  522. */

  523. public long zrem(String key,String member){

  524. Jedis jedis = getJedis();

  525. long count= jedis.zrem(key, member);

  526. returnJedis(jedis);

  527. return count;

  528.  
  529. }

  530.  
  531. /*

  532. *zremrangebyrank 移除有序集 key 中,指定排名(rank)区间内的所有成员。

  533. *@return 被移除成员的数量

  534. */

  535. public long zremrangeByRank(String key,int start,int end){

  536. Jedis jedis = getJedis();

  537. long count= jedis.zremrangeByRank(key, start,end);

  538. returnJedis(jedis);

  539. return count;

  540.  
  541. }

  542.  
  543.  
  544. /*

  545. * zremrangeByScore 删除指定权重区间的元素

  546. */

  547. public long zremrangeByScore(String key, double min, double max) {

  548. Jedis jedis = getJedis();

  549. long count = jedis.zremrangeByScore(key, min, max);

  550. returnJedis(jedis);

  551. return count;

  552. }

  553.  
  554.  
  555. /*

  556. * 获取给定值在集合中的权重

  557. */

  558. public double zscore(String key,String member){

  559. Jedis jedis = getJedis();

  560. Double score = jedis.zscore(key, member);

  561. returnJedis(jedis);

  562. if(score !=null){

  563. return score;

  564. }

  565. return 0;

  566. }

  567.  
  568.  
  569.  
  570.  
  571.  
  572. /*******************************hash***********************************/

  573. /**

  574. * 从hash中删除指定的存储

  575. *

  576. * @param String

  577. * key

  578. * @param String

  579. * fieid 存储的名字

  580. * @return 状态码,1成功,0失败

  581. * */

  582. public long hdel(String key, String fieid) {

  583. Jedis jedis = getJedis();

  584. long s = jedis.hdel(key, fieid);

  585. returnJedis(jedis);

  586. return s;

  587. }

  588.  
  589. public long hdel(String key) {

  590. Jedis jedis = getJedis();

  591. long s = jedis.del(key);

  592. returnJedis(jedis);

  593. return s;

  594. }

  595.  
  596. /**

  597. * 测试hash中指定的存储是否存在

  598. *

  599. * @param String

  600. * key

  601. * @param String

  602. * fieid 存储的名字

  603. * @return 1存在,0不存在

  604. * */

  605. public boolean hexists(String key, String fieid) {

  606. // ShardedJedis sjedis = getShardedJedis();

  607. Jedis sjedis = getJedis();

  608. boolean s = sjedis.hexists(key, fieid);

  609. returnJedis(sjedis);

  610. return s;

  611. }

  612.  
  613. /**

  614. * 返回hash中指定存储位置的值

  615. *

  616. * @param String

  617. * key

  618. * @param String

  619. * fieid 存储的名字

  620. * @return 存储对应的值

  621. * */

  622. public String hget(String key, String fieid) {

  623. // ShardedJedis sjedis = getShardedJedis();

  624. Jedis sjedis = getJedis();

  625. String s = sjedis.hget(key, fieid);

  626. returnJedis(sjedis);

  627. return s;

  628. }

  629.  
  630. public byte[] hget(byte[] key, byte[] fieid) {

  631. // ShardedJedis sjedis = getShardedJedis();

  632. Jedis sjedis = getJedis();

  633. byte[] s = sjedis.hget(key, fieid);

  634. returnJedis(sjedis);

  635. return s;

  636. }

  637.  
  638. /**

  639. * 以Map的形式返回hash中的存储和值

  640. *

  641. * @param String

  642. * key

  643. * @return Map<Strinig,String>

  644. * */

  645. public Map<String, String> hgetAll(String key) {

  646. // ShardedJedis sjedis = getShardedJedis();

  647. Jedis sjedis = getJedis();

  648. Map<String, String> map = sjedis.hgetAll(key);

  649. returnJedis(sjedis);

  650. return map;

  651. }

  652.  
  653. /**

  654. * 添加一个对应关系

  655. *

  656. * @param String

  657. * key

  658. * @param String

  659. * fieid

  660. * @param String

  661. * value

  662. * @return 状态码 1成功,0失败,fieid已存在将更新,也返回0

  663. * **/

  664. public long hset(String key, String fieid, String value) {

  665. Jedis jedis = getJedis();

  666. long s = jedis.hset(key, fieid, value);

  667. returnJedis(jedis);

  668. return s;

  669. }

  670.  
  671. public long hset(String key, String fieid, byte[] value) {

  672. Jedis jedis = getJedis();

  673. long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);

  674. returnJedis(jedis);

  675. return s;

  676. }

  677.  
  678. /**

  679. * 添加对应关系,只有在fieid不存在时才执行

  680. *

  681. * @param String

  682. * key

  683. * @param String

  684. * fieid

  685. * @param String

  686. * value

  687. * @return 状态码 1成功,0失败fieid已存

  688. * **/

  689. public long hsetnx(String key, String fieid, String value) {

  690. Jedis jedis = getJedis();

  691. long s = jedis.hsetnx(key, fieid, value);

  692. returnJedis(jedis);

  693. return s;

  694. }

  695.  
  696. /**

  697. * 获取hash中value的集合

  698. *

  699. * @param String

  700. * key

  701. * @return List<String>

  702. * */

  703. public List<String> hvals(String key) {

  704. // ShardedJedis sjedis = getShardedJedis();

  705. Jedis sjedis = getJedis();

  706. List<String> list = sjedis.hvals(key);

  707. returnJedis(sjedis);

  708. return list;

  709. }

  710.  
  711. /**

  712. * 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型

  713. *

  714. * @param String

  715. * key

  716. * @param String

  717. * fieid 存储位置

  718. * @param String

  719. * long value 要增加的值,可以是负数

  720. * @return 增加指定数字后,存储位置的值

  721. * */

  722. public long hincrby(String key, String fieid, long value) {

  723. Jedis jedis = getJedis();

  724. long s = jedis.hincrBy(key, fieid, value);

  725. returnJedis(jedis);

  726. return s;

  727. }

  728.  
  729. /**

  730. * 返回指定hash中的所有存储名字,类似Map中的keySet方法

  731. *

  732. * @param String

  733. * key

  734. * @return Set<String> 存储名称的集合

  735. * */

  736. public Set<String> hkeys(String key) {

  737. // ShardedJedis sjedis = getShardedJedis();

  738. Jedis sjedis = getJedis();

  739. Set<String> set = sjedis.hkeys(key);

  740. returnJedis(sjedis);

  741. return set;

  742. }

  743.  
  744. /**

  745. * 获取hash中存储的个数,类似Map中size方法

  746. *

  747. * @param String

  748. * key

  749. * @return long 存储的个数

  750. * */

  751. public long hlen(String key) {

  752. // ShardedJedis sjedis = getShardedJedis();

  753. Jedis sjedis = getJedis();

  754. long len = sjedis.hlen(key);

  755. returnJedis(sjedis);

  756. return len;

  757. }

  758.  
  759. /**

  760. * 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null

  761. *

  762. * @param String

  763. * key

  764. * @param String

  765. * ... fieids 存储位置

  766. * @return List<String>

  767. * */

  768. public List<String> hmget(String key, String... fieids) {

  769. // ShardedJedis sjedis = getShardedJedis();

  770. Jedis sjedis = getJedis();

  771. List<String> list = sjedis.hmget(key, fieids);

  772. returnJedis(sjedis);

  773. return list;

  774. }

  775.  
  776. public List<byte[]> hmget(byte[] key, byte[]... fieids) {

  777. // ShardedJedis sjedis = getShardedJedis();

  778. Jedis sjedis = getJedis();

  779. List<byte[]> list = sjedis.hmget(key, fieids);

  780. returnJedis(sjedis);

  781. return list;

  782. }

  783.  
  784. /**

  785. * 添加对应关系,如果对应关系已存在,则覆盖

  786. *

  787. * @param Strin

  788. * key

  789. * @param Map

  790. * <String,String> 对应关系

  791. * @return 状态,成功返回OK

  792. * */

  793. public String hmset(String key, Map<String, String> map) {

  794. Jedis jedis = getJedis();

  795. String s = jedis.hmset(key, map);

  796. returnJedis(jedis);

  797. return s;

  798. }

  799.  
  800. /**

  801. * 添加对应关系,如果对应关系已存在,则覆盖

  802. *

  803. * @param Strin

  804. * key

  805. * @param Map

  806. * <String,String> 对应关系

  807. * @return 状态,成功返回OK

  808. * */

  809. public String hmset(byte[] key, Map<byte[], byte[]> map) {

  810. Jedis jedis = getJedis();

  811. String s = jedis.hmset(key, map);

  812. returnJedis(jedis);

  813. return s;

  814. }

  815.  
  816. // *******************************************Strings*******************************************//

  817. /**

  818. * 根据key获取记录

  819. *

  820. * @param String

  821. * key

  822. * @return 值

  823. * */

  824. public String get(String key) {

  825. // ShardedJedis sjedis = getShardedJedis();

  826. Jedis sjedis = getJedis();

  827. String value = sjedis.get(key);

  828. returnJedis(sjedis);

  829. return value;

  830. }

  831.  
  832. /**

  833. * 根据key获取记录

  834. *

  835. * @param byte[] key

  836. * @return 值

  837. * */

  838. public byte[] get(byte[] key) {

  839. // ShardedJedis sjedis = getShardedJedis();

  840. Jedis sjedis = getJedis();

  841. byte[] value = sjedis.get(key);

  842. returnJedis(sjedis);

  843. return value;

  844. }

  845.  
  846. /**

  847. * 添加有过期时间的记录

  848. *

  849. * @param String

  850. * key

  851. * @param int seconds 过期时间,以秒为单位

  852. * @param String

  853. * value

  854. * @return String 操作状态

  855. * */

  856. public String setEx(String key, int seconds, String value) {

  857. Jedis jedis = getJedis();

  858. String str = jedis.setex(key, seconds, value);

  859. returnJedis(jedis);

  860. return str;

  861. }

  862.  
  863. /**

  864. * 添加有过期时间的记录

  865. *

  866. * @param String

  867. * key

  868. * @param int seconds 过期时间,以秒为单位

  869. * @param String

  870. * value

  871. * @return String 操作状态

  872. * */

  873. public String setEx(byte[] key, int seconds, byte[] value) {

  874. Jedis jedis = getJedis();

  875. String str = jedis.setex(key, seconds, value);

  876. returnJedis(jedis);

  877. return str;

  878. }

  879.  
  880. /**

  881. * 添加一条记录,仅当给定的key不存在时才插入

  882. *

  883. * @param String

  884. * key

  885. * @param String

  886. * value

  887. * @return long 状态码,1插入成功且key不存在,0未插入,key存在

  888. * */

  889. public long setnx(String key, String value) {

  890. Jedis jedis = getJedis();

  891. long str = jedis.setnx(key, value);

  892. returnJedis(jedis);

  893. return str;

  894. }

  895.  
  896. /**

  897. * 添加记录,如果记录已存在将覆盖原有的value

  898. *

  899. * @param String

  900. * key

  901. * @param String

  902. * value

  903. * @return 状态码

  904. * */

  905. public String set(String key, String value) {

  906. return set(SafeEncoder.encode(key), SafeEncoder.encode(value));

  907. }

  908.  
  909. /**

  910. * 添加记录,如果记录已存在将覆盖原有的value

  911. *

  912. * @param String

  913. * key

  914. * @param String

  915. * value

  916. * @return 状态码

  917. * */

  918. public String set(String key, byte[] value) {

  919. return set(SafeEncoder.encode(key), value);

  920. }

  921.  
  922. /**

  923. * 添加记录,如果记录已存在将覆盖原有的value

  924. *

  925. * @param byte[] key

  926. * @param byte[] value

  927. * @return 状态码

  928. * */

  929. public String set(byte[] key, byte[] value) {

  930. Jedis jedis = getJedis();

  931. String status = jedis.set(key, value);

  932. returnJedis(jedis);

  933. return status;

  934. }

  935.  
  936. /**

  937. * 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/>

  938. * 例:String str1="123456789";<br/>

  939. * 对str1操作后setRange(key,4,0000),str1="123400009";

  940. *

  941. * @param String

  942. * key

  943. * @param long offset

  944. * @param String

  945. * value

  946. * @return long value的长度

  947. * */

  948. public long setRange(String key, long offset, String value) {

  949. Jedis jedis = getJedis();

  950. long len = jedis.setrange(key, offset, value);

  951. returnJedis(jedis);

  952. return len;

  953. }

  954.  
  955. /**

  956. * 在指定的key中追加value

  957. *

  958. * @param String

  959. * key

  960. * @param String

  961. * value

  962. * @return long 追加后value的长度

  963. * **/

  964. public long append(String key, String value) {

  965. Jedis jedis = getJedis();

  966. long len = jedis.append(key, value);

  967. returnJedis(jedis);

  968. return len;

  969. }

  970.  
  971. /**

  972. * 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用

  973. *

  974. * @param String

  975. * key

  976. * @param long number 要减去的值

  977. * @return long 减指定值后的值

  978. * */

  979. public long decrBy(String key, long number) {

  980. Jedis jedis = getJedis();

  981. long len = jedis.decrBy(key, number);

  982. returnJedis(jedis);

  983. return len;

  984. }

  985.  
  986. /**

  987. * <b>可以作为获取唯一id的方法</b><br/>

  988. * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用

  989. *

  990. * @param String

  991. * key

  992. * @param long number 要减去的值

  993. * @return long 相加后的值

  994. * */

  995. public long incrBy(String key, long number) {

  996. Jedis jedis = getJedis();

  997. long len = jedis.incrBy(key, number);

  998. returnJedis(jedis);

  999. return len;

  1000. }

  1001.  
  1002. /**

  1003. * 对指定key对应的value进行截取

  1004. *

  1005. * @param String

  1006. * key

  1007. * @param long startOffset 开始位置(包含)

  1008. * @param long endOffset 结束位置(包含)

  1009. * @return String 截取的值

  1010. * */

  1011. public String getrange(String key, long startOffset, long endOffset) {

  1012. // ShardedJedis sjedis = getShardedJedis();

  1013. Jedis sjedis = getJedis();

  1014. String value = sjedis.getrange(key, startOffset, endOffset);

  1015. returnJedis(sjedis);

  1016. return value;

  1017. }

  1018.  
  1019. /**

  1020. * 获取并设置指定key对应的value<br/>

  1021. * 如果key存在返回之前的value,否则返回null

  1022. *

  1023. * @param String

  1024. * key

  1025. * @param String

  1026. * value

  1027. * @return String 原始value或null

  1028. * */

  1029. public String getSet(String key, String value) {

  1030. Jedis jedis = getJedis();

  1031. String str = jedis.getSet(key, value);

  1032. returnJedis(jedis);

  1033. return str;

  1034. }

  1035.  
  1036. /**

  1037. * 批量获取记录,如果指定的key不存在返回List的对应位置将是null

  1038. *

  1039. * @param String

  1040. * keys

  1041. * @return List<String> 值得集合

  1042. * */

  1043. public List<String> mget(String... keys) {

  1044. Jedis jedis = getJedis();

  1045. List<String> str = jedis.mget(keys);

  1046. returnJedis(jedis);

  1047. return str;

  1048. }

  1049.  
  1050. /**

  1051. * 批量存储记录

  1052. *

  1053. * @param String

  1054. * keysvalues 例:keysvalues="key1","value1","key2","value2";

  1055. * @return String 状态码

  1056. * */

  1057. public String mset(String... keysvalues) {

  1058. Jedis jedis = getJedis();

  1059. String str = jedis.mset(keysvalues);

  1060. returnJedis(jedis);

  1061. return str;

  1062. }

  1063.  
  1064. /**

  1065. * 获取key对应的值的长度

  1066. *

  1067. * @param String

  1068. * key

  1069. * @return value值得长度

  1070. * */

  1071. public long strlen(String key) {

  1072. Jedis jedis = getJedis();

  1073. long len = jedis.strlen(key);

  1074. returnJedis(jedis);

  1075. return len;

  1076. }

  1077.  
  1078. // *******************************************Lists*******************************************//

  1079. /**

  1080. * List长度

  1081. *

  1082. * @param String

  1083. * key

  1084. * @return 长度

  1085. * */

  1086. public long llen(String key) {

  1087. return llen(SafeEncoder.encode(key));

  1088. }

  1089.  
  1090. /**

  1091. * List长度

  1092. *

  1093. * @param byte[] key

  1094. * @return 长度

  1095. * */

  1096. public long llen(byte[] key) {

  1097. // ShardedJedis sjedis = getShardedJedis();

  1098. Jedis sjedis = getJedis();

  1099. long count = sjedis.llen(key);

  1100. returnJedis(sjedis);

  1101. return count;

  1102. }

  1103.  
  1104. /**

  1105. * 覆盖操作,将覆盖List中指定位置的值

  1106. *

  1107. * @param byte[] key

  1108. * @param int index 位置

  1109. * @param byte[] value 值

  1110. * @return 状态码

  1111. * */

  1112. public String lset(byte[] key, int index, byte[] value) {

  1113. Jedis jedis = getJedis();

  1114. String status = jedis.lset(key, index, value);

  1115. returnJedis(jedis);

  1116. return status;

  1117. }

  1118.  
  1119. /**

  1120. * 覆盖操作,将覆盖List中指定位置的值

  1121. *

  1122. * @param key

  1123. * @param int index 位置

  1124. * @param String

  1125. * value 值

  1126. * @return 状态码

  1127. * */

  1128. public String lset(String key, int index, String value) {

  1129. return lset(SafeEncoder.encode(key), index, SafeEncoder.encode(value));

  1130. }

  1131.  
  1132. /**

  1133. * 在value的相对位置插入记录

  1134. *

  1135. * @param key

  1136. * @param LIST_POSITION

  1137. * 前面插入或后面插入

  1138. * @param String

  1139. * pivot 相对位置的内容

  1140. * @param String

  1141. * value 插入的内容

  1142. * @return 记录总数

  1143. * */

  1144. public long linsert(String key, LIST_POSITION where, String pivot,

  1145. String value) {

  1146. return linsert(SafeEncoder.encode(key), where,

  1147. SafeEncoder.encode(pivot), SafeEncoder.encode(value));

  1148. }

  1149.  
  1150. /**

  1151. * 在指定位置插入记录

  1152. *

  1153. * @param String

  1154. * key

  1155. * @param LIST_POSITION

  1156. * 前面插入或后面插入

  1157. * @param byte[] pivot 相对位置的内容

  1158. * @param byte[] value 插入的内容

  1159. * @return 记录总数

  1160. * */

  1161. public long linsert(byte[] key, LIST_POSITION where, byte[] pivot,

  1162. byte[] value) {

  1163. Jedis jedis = getJedis();

  1164. long count = jedis.linsert(key, where, pivot, value);

  1165. returnJedis(jedis);

  1166. return count;

  1167. }

  1168.  
  1169. /**

  1170. * 获取List中指定位置的值

  1171. *

  1172. * @param String

  1173. * key

  1174. * @param int index 位置

  1175. * @return 值

  1176. * **/

  1177. public String lindex(String key, int index) {

  1178. return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));

  1179. }

  1180.  
  1181. /**

  1182. * 获取List中指定位置的值

  1183. *

  1184. * @param byte[] key

  1185. * @param int index 位置

  1186. * @return 值

  1187. * **/

  1188. public byte[] lindex(byte[] key, int index) {

  1189. // ShardedJedis sjedis = getShardedJedis();

  1190. Jedis sjedis = getJedis();

  1191. byte[] value = sjedis.lindex(key, index);

  1192. returnJedis(sjedis);

  1193. return value;

  1194. }

  1195.  
  1196. /**

  1197. * 将List中的第一条记录移出List

  1198. *

  1199. * @param String

  1200. * key

  1201. * @return 移出的记录

  1202. * */

  1203. public String lpop(String key) {

  1204. return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));

  1205. }

  1206.  
  1207. /**

  1208. * 将List中的第一条记录移出List

  1209. *

  1210. * @param byte[] key

  1211. * @return 移出的记录

  1212. * */

  1213. public byte[] lpop(byte[] key) {

  1214. Jedis jedis = getJedis();

  1215. byte[] value = jedis.lpop(key);

  1216. returnJedis(jedis);

  1217. return value;

  1218. }

  1219.  
  1220. /**

  1221. * 将List中最后第一条记录移出List

  1222. *

  1223. * @param byte[] key

  1224. * @return 移出的记录

  1225. * */

  1226. public String rpop(String key) {

  1227. Jedis jedis = getJedis();

  1228. String value = jedis.rpop(key);

  1229. returnJedis(jedis);

  1230. return value;

  1231. }

  1232.  
  1233. /**

  1234. * 向List尾部追加记录

  1235. *

  1236. * @param String

  1237. * key

  1238. * @param String

  1239. * value

  1240. * @return 记录总数

  1241. * */

  1242. public long lpush(String key, String value) {

  1243. return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));

  1244. }

  1245.  
  1246. /**

  1247. * 向List头部追加记录

  1248. *

  1249. * @param String

  1250. * key

  1251. * @param String

  1252. * value

  1253. * @return 记录总数

  1254. * */

  1255. public long rpush(String key, String value) {

  1256. Jedis jedis = getJedis();

  1257. long count = jedis.rpush(key, value);

  1258. returnJedis(jedis);

  1259. return count;

  1260. }

  1261.  
  1262. /**

  1263. * 向List头部追加记录

  1264. *

  1265. * @param String

  1266. * key

  1267. * @param String

  1268. * value

  1269. * @return 记录总数

  1270. * */

  1271. public long rpush(byte[] key, byte[] value) {

  1272. Jedis jedis = getJedis();

  1273. long count = jedis.rpush(key, value);

  1274. returnJedis(jedis);

  1275. return count;

  1276. }

  1277.  
  1278. /**

  1279. * 向List中追加记录

  1280. *

  1281. * @param byte[] key

  1282. * @param byte[] value

  1283. * @return 记录总数

  1284. * */

  1285. public long lpush(byte[] key, byte[] value) {

  1286. Jedis jedis = getJedis();

  1287. long count = jedis.lpush(key, value);

  1288. returnJedis(jedis);

  1289. return count;

  1290. }

  1291.  
  1292. /**

  1293. * 获取指定范围的记录,可以做为分页使用

  1294. *

  1295. * @param String

  1296. * key

  1297. * @param long start

  1298. * @param long end

  1299. * @return List

  1300. * */

  1301. public List<String> lrange(String key, long start, long end) {

  1302. // ShardedJedis sjedis = getShardedJedis();

  1303. Jedis sjedis = getJedis();

  1304. List<String> list = sjedis.lrange(key, start, end);

  1305. returnJedis(sjedis);

  1306. return list;

  1307. }

  1308.  
  1309. /**

  1310. * 获取指定范围的记录,可以做为分页使用

  1311. *

  1312. * @param byte[] key

  1313. * @param int start

  1314. * @param int end 如果为负数,则尾部开始计算

  1315. * @return List

  1316. * */

  1317. public List<byte[]> lrange(byte[] key, int start, int end) {

  1318. // ShardedJedis sjedis = getShardedJedis();

  1319. Jedis sjedis = getJedis();

  1320. List<byte[]> list = sjedis.lrange(key, start, end);

  1321. returnJedis(sjedis);

  1322. return list;

  1323. }

  1324.  
  1325. /**

  1326. * 删除List中c条记录,被删除的记录值为value

  1327. *

  1328. * @param byte[] key

  1329. * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录

  1330. * @param byte[] value 要匹配的值

  1331. * @return 删除后的List中的记录数

  1332. * */

  1333. public long lrem(byte[] key, int c, byte[] value) {

  1334. Jedis jedis = getJedis();

  1335. long count = jedis.lrem(key, c, value);

  1336. returnJedis(jedis);

  1337. return count;

  1338. }

  1339.  
  1340. /**

  1341. * 删除List中c条记录,被删除的记录值为value

  1342. *

  1343. * @param String

  1344. * key

  1345. * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录

  1346. * @param String

  1347. * value 要匹配的值

  1348. * @return 删除后的List中的记录数

  1349. * */

  1350. public long lrem(String key, int c, String value) {

  1351. return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));

  1352. }

  1353.  
  1354. /**

  1355. * 算是删除吧,只保留start与end之间的记录

  1356. *

  1357. * @param byte[] key

  1358. * @param int start 记录的开始位置(0表示第一条记录)

  1359. * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)

  1360. * @return 执行状态码

  1361. * */

  1362. public String ltrim(byte[] key, int start, int end) {

  1363. Jedis jedis = getJedis();

  1364. String str = jedis.ltrim(key, start, end);

  1365. returnJedis(jedis);

  1366. return str;

  1367. }

  1368.  
  1369. /**

  1370. * 算是删除吧,只保留start与end之间的记录

  1371. *

  1372. * @param String

  1373. * key

  1374. * @param int start 记录的开始位置(0表示第一条记录)

  1375. * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)

  1376. * @return 执行状态码

  1377. * */

  1378. public String ltrim(String key, int start, int end) {

  1379. return ltrim(SafeEncoder.encode(key), start, end);

  1380. }

  1381.  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值