RedisClient类中实现Redis的相关操作

Java操作Redis

2016年03月31日 12:49:24 lxhjh 阅读数:6591

目前Jedis是官方推荐的比较好的Redis操作API包,我们这里结合Spring来看下如何使用Redis这一神器。

1、在pom文件中引入jredis包:


  redis.clients
  jedis
  2.8.1

2、在Spring的resource目录中增加Redis的配置文件/src/main/resources/redis.properties

# Redis settings
redis.host=192.168.56.101
redis.port=6379
redis.pass=lxh20160330
redis.timeout=0

redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

3、在Spring的配置文件中配置好Redis的相关Bean注入:

 

 
  1. 1

  2. 2

  3. 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

  4. 4 xmlns:context="http://www.springframework.org/schema/context"

  5. 5 xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

  6. 6 xmlns:aop="http://www.springframework.org/schema/aop"

  7. 7 xsi:schemaLocation="

  8. 8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

  9. 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  10. 10

  11. 11

  12. 12

  13. 13

  14. 14

  15. 15

  16. 16

  17. 17

  18. 18

  19. 19

  20. 20

  21. 21

  22. 22

  23. 23

  24. 24

  25. 25

  26. 26

  27. 27

  28. 28

  29. 29

  30. 30

  31. 31

  32. 32

  33. 33

  34. 34

  35. 35

  36. 36

  37. 37

  38. 38

 

4、获取Redis客户端对象的类

package com.redis.demo.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.redis.demo.service.RedisService;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Repository("redisService")
public class RedisServiceImpl implements RedisService {
     private static final Logger log = LoggerFactory.getLogger(RedisServiceImpl.class);

    @Autowired
    private ShardedJedisPool shardedJedisPool;
 
     public ShardedJedis getRedisClient() {
         try {
            ShardedJedis shardJedis = shardedJedisPool.getResource();
             return shardJedis;
         } catch (Exception e) {
             log.error("getRedisClent error", e);
         }
         return null;
    }
 
     public void returnResource(ShardedJedis shardedJedis) {
         shardedJedisPool.returnResource(shardedJedis);
     }
 
     public void returnResource(ShardedJedis shardedJedis, boolean broken) {
         if (broken) {
             shardedJedisPool.returnBrokenResource(shardedJedis);
         } else {
             shardedJedisPool.returnResource(shardedJedis);
         }
     }
 }

5、RedisClient类中实现Redis的相关操作

 

 
  1. package com.redis.demo;

  2.  
  3. import java.util.Collection;

  4. import java.util.List;

  5. import java.util.Map;

  6. import java.util.Set;

  7.  
  8. import org.slf4j.Logger;

  9. import org.slf4j.LoggerFactory;

  10. import org.springframework.beans.factory.annotation.Autowired;

  11.  
  12. import com.redis.demo.service.impl.RedisServiceImpl;

  13.  
  14. import redis.clients.jedis.BinaryClient.LIST_POSITION;

  15. import redis.clients.jedis.Jedis;

  16. import redis.clients.jedis.JedisShardInfo;

  17. import redis.clients.jedis.ShardedJedis;

  18. import redis.clients.jedis.ShardedJedisPipeline;

  19. import redis.clients.jedis.SortingParams;

  20. import redis.clients.jedis.Tuple;

  21.  
  22.  
  23. public class RedisClient {

  24.  
  25. private static final Logger log = LoggerFactory.getLogger(RedisClient.class);

  26.  
  27. @Autowired

  28. private RedisServiceImpl redisService;

  29.  
  30. public void disconnect() {

  31. ShardedJedis shardedJedis = redisService.getRedisClient();

  32. shardedJedis.disconnect();

  33. }

  34.  
  35. /**

  36. * 设置单个值

  37. */

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

  39. String result = null;

  40.  
  41. ShardedJedis shardedJedis = redisService.getRedisClient();

  42. if (shardedJedis == null) {

  43. return result;

  44. }

  45. boolean flag = false;

  46. try {

  47. result = shardedJedis.set(key, value);

  48. } catch (Exception e) {

  49. log.error(e.getMessage(), e);

  50. flag = true;

  51. } finally {

  52. redisService.returnResource(shardedJedis, flag);

  53. }

  54. return result;

  55. }

  56.  
  57. /**

  58. * 获取单个值

  59. */

  60. public String get(String key) {

  61. String result = null;

  62. ShardedJedis shardedJedis = redisService.getRedisClient();

  63. if (shardedJedis == null) {

  64. return result;

  65. }

  66.  
  67. boolean flag = false;

  68. try {

  69. result = shardedJedis.get(key);

  70.  
  71. } catch (Exception e) {

  72. log.error(e.getMessage(), e);

  73. flag = true;

  74. } finally {

  75. redisService.returnResource(shardedJedis, flag);

  76. }

  77. return result;

  78. }

  79.  
  80. public Boolean exists(String key) {

  81. Boolean result = false;

  82. ShardedJedis shardedJedis = redisService.getRedisClient();

  83. if (shardedJedis == null) {

  84. return result;

  85. }

  86. boolean flag = false;

  87. try {

  88. result = shardedJedis.exists(key);

  89. } catch (Exception e) {

  90. log.error(e.getMessage(), e);

  91. flag = true;

  92. } finally {

  93. redisService.returnResource(shardedJedis, flag);

  94. }

  95. return result;

  96. }

  97.  
  98. public String type(String key) {

  99. String result = null;

  100. ShardedJedis shardedJedis = redisService.getRedisClient();

  101. if (shardedJedis == null) {

  102. return result;

  103. }

  104. boolean flag = false;

  105. try {

  106. result = shardedJedis.type(key);

  107.  
  108. } catch (Exception e) {

  109. log.error(e.getMessage(), e);

  110. flag = true;

  111. } finally {

  112. redisService.returnResource(shardedJedis, flag);

  113. }

  114. return result;

  115. }

  116.  
  117. /**

  118. * 在某段时间后失效

  119. */

  120. public Long expire(String key, int seconds) {

  121. Long result = null;

  122. ShardedJedis shardedJedis = redisService.getRedisClient();

  123. if (shardedJedis == null) {

  124. return result;

  125. }

  126. boolean flag = false;

  127. try {

  128. result = shardedJedis.expire(key, seconds);

  129.  
  130. } catch (Exception e) {

  131. log.error(e.getMessage(), e);

  132. flag = true;

  133. } finally {

  134. redisService.returnResource(shardedJedis, flag);

  135. }

  136. return result;

  137. }

  138.  
  139. /**

  140. * 在某个时间点失效

  141. */

  142. public Long expireAt(String key, long time) {

  143. Long result = null;

  144. ShardedJedis shardedJedis = redisService.getRedisClient();

  145. if (shardedJedis == null) {

  146. return result;

  147. }

  148. boolean flag = false;

  149. try {

  150. result = shardedJedis.expireAt(key, time);

  151.  
  152. } catch (Exception e) {

  153. log.error(e.getMessage(), e);

  154. flag = true;

  155. } finally {

  156. redisService.returnResource(shardedJedis, flag);

  157. }

  158. return result;

  159. }

  160.  
  161. public Long ttl(String key) {

  162. Long result = null;

  163. ShardedJedis shardedJedis = redisService.getRedisClient();

  164. if (shardedJedis == null) {

  165. return result;

  166. }

  167. boolean flag = false;

  168. try {

  169. result = shardedJedis.ttl(key);

  170.  
  171. } catch (Exception e) {

  172. log.error(e.getMessage(), e);

  173. flag = true;

  174. } finally {

  175. redisService.returnResource(shardedJedis, flag);

  176. }

  177. return result;

  178. }

  179.  
  180. public boolean setbit(String key, long offset, boolean value) {

  181.  
  182. ShardedJedis shardedJedis = redisService.getRedisClient();

  183. boolean result = false;

  184. if (shardedJedis == null) {

  185. return result;

  186. }

  187. boolean flag = false;

  188. try {

  189. result = shardedJedis.setbit(key, offset, value);

  190. } catch (Exception e) {

  191. log.error(e.getMessage(), e);

  192. flag = true;

  193. } finally {

  194. redisService.returnResource(shardedJedis, flag);

  195. }

  196. return result;

  197. }

  198.  
  199. public boolean getbit(String key, long offset) {

  200. ShardedJedis shardedJedis = redisService.getRedisClient();

  201. boolean result = false;

  202. if (shardedJedis == null) {

  203. return result;

  204. }

  205. boolean flag = false;

  206.  
  207. try {

  208. result = shardedJedis.getbit(key, offset);

  209. } catch (Exception e) {

  210. log.error(e.getMessage(), e);

  211. flag = true;

  212. } finally {

  213. redisService.returnResource(shardedJedis, flag);

  214. }

  215. return result;

  216. }

  217.  
  218. public long setrange(String key, long offset, String value) {

  219. ShardedJedis shardedJedis = redisService.getRedisClient();

  220. long result = ;

  221. if (shardedJedis == null) {

  222. return result;

  223. }

  224. boolean flag = false;

  225. try {

  226. result = shardedJedis.setrange(key, offset, value);

  227. } catch (Exception e) {

  228. log.error(e.getMessage(), e);

  229. flag = true;

  230. } finally {

  231. redisService.returnResource(shardedJedis, flag);

  232. }

  233. return result;

  234. }

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

  237. ShardedJedis shardedJedis = redisService.getRedisClient();

  238. String result = null;

  239. if (shardedJedis == null) {

  240. return result;

  241. }

  242. boolean flag = false;

  243. try {

  244. result = shardedJedis.getrange(key, startOffset, endOffset);

  245.  
  246. } catch (Exception e) {

  247. log.error(e.getMessage(), e);

  248. flag = true;

  249. } finally {

  250. redisService.returnResource(shardedJedis, flag);

  251. }

  252. return result;

  253. }

  254.  
  255. public String getSet(String key, String value) {

  256. String result = null;

  257. ShardedJedis shardedJedis = redisService.getRedisClient();

  258. if (shardedJedis == null) {

  259. return result;

  260. }

  261. boolean flag = false;

  262. try {

  263. result = shardedJedis.getSet(key, value);

  264. } catch (Exception e) {

  265. log.error(e.getMessage(), e);

  266. flag = true;

  267. } finally {

  268. redisService.returnResource(shardedJedis, flag);

  269. }

  270. return result;

  271. }

  272.  
  273. public Long setnx(String key, String value) {

  274. Long result = null;

  275. ShardedJedis shardedJedis = redisService.getRedisClient();

  276. if (shardedJedis == null) {

  277. return result;

  278. }

  279. boolean flag = false;

  280. try {

  281. result = shardedJedis.setnx(key, value);

  282. } catch (Exception e) {

  283. log.error(e.getMessage(), e);

  284. flag = true;

  285. } finally {

  286. redisService.returnResource(shardedJedis, flag);

  287. }

  288. return result;

  289. }

  290.  
  291. public String setex(String key, int seconds, String value) {

  292. String result = null;

  293. ShardedJedis shardedJedis = redisService.getRedisClient();

  294. if (shardedJedis == null) {

  295. return result;

  296. }

  297. boolean flag = false;

  298. try {

  299. result = shardedJedis.setex(key, seconds, value);

  300.  
  301. } catch (Exception e) {

  302. log.error(e.getMessage(), e);

  303. flag = true;

  304. } finally {

  305. redisService.returnResource(shardedJedis, flag);

  306. }

  307. return result;

  308. }

  309.  
  310. public Long decrBy(String key, long integer) {

  311. Long result = null;

  312. ShardedJedis shardedJedis = redisService.getRedisClient();

  313. if (shardedJedis == null) {

  314. return result;

  315. }

  316. boolean flag = false;

  317. try {

  318. result = shardedJedis.decrBy(key, integer);

  319.  
  320. } catch (Exception e) {

  321. log.error(e.getMessage(), e);

  322. flag = true;

  323. } finally {

  324. redisService.returnResource(shardedJedis, flag);

  325. }

  326. return result;

  327. }

  328.  
  329. public Long decr(String key) {

  330. Long result = null;

  331. ShardedJedis shardedJedis = redisService.getRedisClient();

  332. if (shardedJedis == null) {

  333. return result;

  334. }

  335. boolean flag = false;

  336. try {

  337. result = shardedJedis.decr(key);

  338.  
  339. } catch (Exception e) {

  340. log.error(e.getMessage(), e);

  341. flag = true;

  342. } finally {

  343. redisService.returnResource(shardedJedis, flag);

  344. }

  345. return result;

  346. }

  347.  
  348. public Long incrBy(String key, long integer) {

  349. Long result = null;

  350. ShardedJedis shardedJedis = redisService.getRedisClient();

  351. if (shardedJedis == null) {

  352. return result;

  353. }

  354. boolean flag = false;

  355. try {

  356. result = shardedJedis.incrBy(key, integer);

  357.  
  358. } catch (Exception e) {

  359. log.error(e.getMessage(), e);

  360. flag = true;

  361. } finally {

  362. redisService.returnResource(shardedJedis, flag);

  363. }

  364. return result;

  365. }

  366.  
  367. public Long incr(String key) {

  368. Long result = null;

  369. ShardedJedis shardedJedis = redisService.getRedisClient();

  370. if (shardedJedis == null) {

  371. return result;

  372. }

  373. boolean flag = false;

  374. try {

  375. result = shardedJedis.incr(key);

  376.  
  377. } catch (Exception e) {

  378. log.error(e.getMessage(), e);

  379. flag = true;

  380. } finally {

  381. redisService.returnResource(shardedJedis, flag);

  382. }

  383. return result;

  384. }

  385.  
  386. public Long append(String key, String value) {

  387. Long result = null;

  388. ShardedJedis shardedJedis = redisService.getRedisClient();

  389. if (shardedJedis == null) {

  390. return result;

  391. }

  392. boolean flag = false;

  393. try {

  394. result = shardedJedis.append(key, value);

  395.  
  396. } catch (Exception e) {

  397. log.error(e.getMessage(), e);

  398. flag = true;

  399. } finally {

  400. redisService.returnResource(shardedJedis, flag);

  401. }

  402. return result;

  403. }

  404.  
  405. public String substr(String key, int start, int end) {

  406. String result = null;

  407. ShardedJedis shardedJedis = redisService.getRedisClient();

  408. if (shardedJedis == null) {

  409. return result;

  410. }

  411. boolean flag = false;

  412. try {

  413. result = shardedJedis.substr(key, start, end);

  414.  
  415. } catch (Exception e) {

  416. log.error(e.getMessage(), e);

  417. flag = true;

  418. } finally {

  419. redisService.returnResource(shardedJedis, flag);

  420. }

  421. return result;

  422. }

  423.  
  424. public Long hset(String key, String field, String value) {

  425. Long result = null;

  426. ShardedJedis shardedJedis = redisService.getRedisClient();

  427. if (shardedJedis == null) {

  428. return result;

  429. }

  430. boolean flag = false;

  431. try {

  432. result = shardedJedis.hset(key, field, value);

  433.  
  434. } catch (Exception e) {

  435. log.error(e.getMessage(), e);

  436. flag = true;

  437. } finally {

  438. redisService.returnResource(shardedJedis, flag);

  439. }

  440. return result;

  441. }

  442.  
  443. public String hget(String key, String field) {

  444. String result = null;

  445. ShardedJedis shardedJedis = redisService.getRedisClient();

  446. if (shardedJedis == null) {

  447. return result;

  448. }

  449. boolean flag = false;

  450. try {

  451. result = shardedJedis.hget(key, field);

  452.  
  453. } catch (Exception e) {

  454. log.error(e.getMessage(), e);

  455. flag = true;

  456. } finally {

  457. redisService.returnResource(shardedJedis, flag);

  458. }

  459. return result;

  460. }

  461.  
  462. public Long hsetnx(String key, String field, String value) {

  463. Long result = null;

  464. ShardedJedis shardedJedis = redisService.getRedisClient();

  465. if (shardedJedis == null) {

  466. return result;

  467. }

  468. boolean flag = false;

  469. try {

  470. result = shardedJedis.hsetnx(key, field, value);

  471.  
  472. } catch (Exception e) {

  473. log.error(e.getMessage(), e);

  474. flag = true;

  475. } finally {

  476. redisService.returnResource(shardedJedis, flag);

  477. }

  478. return result;

  479. }

  480.  
  481. public String hmset(String key, Map hash) {

  482. String result = null;

  483. ShardedJedis shardedJedis = redisService.getRedisClient();

  484. if (shardedJedis == null) {

  485. return result;

  486. }

  487. boolean flag = false;

  488. try {

  489. result = shardedJedis.hmset(key, hash);

  490.  
  491. } catch (Exception e) {

  492. log.error(e.getMessage(), e);

  493. flag = true;

  494. } finally {

  495. redisService.returnResource(shardedJedis, flag);

  496. }

  497. return result;

  498. }

  499.  
  500. public List hmget(String key, String... fields) {

  501. List result = null;

  502. ShardedJedis shardedJedis = redisService.getRedisClient();

  503. if (shardedJedis == null) {

  504. return result;

  505. }

  506. boolean flag = false;

  507. try {

  508. result = shardedJedis.hmget(key, fields);

  509.  
  510. } catch (Exception e) {

  511. log.error(e.getMessage(), e);

  512. flag = true;

  513. } finally {

  514. redisService.returnResource(shardedJedis, flag);

  515. }

  516. return result;

  517. }

  518.  
  519. public Long hincrBy(String key, String field, long value) {

  520. Long result = null;

  521. ShardedJedis shardedJedis = redisService.getRedisClient();

  522. if (shardedJedis == null) {

  523. return result;

  524. }

  525. boolean flag = false;

  526. try {

  527. result = shardedJedis.hincrBy(key, field, value);

  528.  
  529. } catch (Exception e) {

  530. log.error(e.getMessage(), e);

  531. flag = true;

  532. } finally {

  533. redisService.returnResource(shardedJedis, flag);

  534. }

  535. return result;

  536. }

  537.  
  538. public Boolean hexists(String key, String field) {

  539. Boolean result = false;

  540. ShardedJedis shardedJedis = redisService.getRedisClient();

  541. if (shardedJedis == null) {

  542. return result;

  543. }

  544. boolean flag = false;

  545. try {

  546. result = shardedJedis.hexists(key, field);

  547.  
  548. } catch (Exception e) {

  549. log.error(e.getMessage(), e);

  550. flag = true;

  551. } finally {

  552. redisService.returnResource(shardedJedis, flag);

  553. }

  554. return result;

  555. }

  556.  
  557. public Long del(String key) {

  558. Long result = null;

  559. ShardedJedis shardedJedis = redisService.getRedisClient();

  560. if (shardedJedis == null) {

  561. return result;

  562. }

  563. boolean flag = false;

  564. try {

  565. result = shardedJedis.del(key);

  566.  
  567. } catch (Exception e) {

  568. log.error(e.getMessage(), e);

  569. flag = true;

  570. } finally {

  571. redisService.returnResource(shardedJedis, flag);

  572. }

  573. return result;

  574. }

  575.  
  576. public Long hdel(String key, String field) {

  577. Long result = null;

  578. ShardedJedis shardedJedis = redisService.getRedisClient();

  579. if (shardedJedis == null) {

  580. return result;

  581. }

  582. boolean flag = false;

  583. try {

  584. result = shardedJedis.hdel(key, field);

  585.  
  586. } catch (Exception e) {

  587. log.error(e.getMessage(), e);

  588. flag = true;

  589. } finally {

  590. redisService.returnResource(shardedJedis, flag);

  591. }

  592. return result;

  593. }

  594.  
  595. public Long hlen(String key) {

  596. Long result = null;

  597. ShardedJedis shardedJedis = redisService.getRedisClient();

  598. if (shardedJedis == null) {

  599. return result;

  600. }

  601. boolean flag = false;

  602. try {

  603. result = shardedJedis.hlen(key);

  604.  
  605. } catch (Exception e) {

  606. log.error(e.getMessage(), e);

  607. flag = true;

  608. } finally {

  609. redisService.returnResource(shardedJedis, flag);

  610. }

  611. return result;

  612. }

  613.  
  614. public Set hkeys(String key) {

  615. Set result = null;

  616. ShardedJedis shardedJedis = redisService.getRedisClient();

  617. if (shardedJedis == null) {

  618. return result;

  619. }

  620. boolean flag = false;

  621. try {

  622. result = shardedJedis.hkeys(key);

  623.  
  624. } catch (Exception e) {

  625. log.error(e.getMessage(), e);

  626. flag = true;

  627. } finally {

  628. redisService.returnResource(shardedJedis, flag);

  629. }

  630. return result;

  631. }

  632.  
  633. public List hvals(String key) {

  634. List result = null;

  635. ShardedJedis shardedJedis = redisService.getRedisClient();

  636. if (shardedJedis == null) {

  637. return result;

  638. }

  639. boolean flag = false;

  640. try {

  641. result = shardedJedis.hvals(key);

  642.  
  643. } catch (Exception e) {

  644. log.error(e.getMessage(), e);

  645. flag = true;

  646. } finally {

  647. redisService.returnResource(shardedJedis, flag);

  648. }

  649. return result;

  650. }

  651.  
  652. public Map hgetAll(String key) {

  653. Map result = null;

  654. ShardedJedis shardedJedis = redisService.getRedisClient();

  655. if (shardedJedis == null) {

  656. return result;

  657. }

  658. boolean flag = false;

  659. try {

  660. result = shardedJedis.hgetAll(key);

  661.  
  662. } catch (Exception e) {

  663. log.error(e.getMessage(), e);

  664. flag = true;

  665. } finally {

  666. redisService.returnResource(shardedJedis, flag);

  667. }

  668. return result;

  669. }

  670.  
  671. /**

  672. * 在redis list尾部增加一个String

  673. * */

  674. public Long rpush(String key, String string) {

  675. Long result = null;

  676. ShardedJedis shardedJedis = redisService.getRedisClient();

  677. if (shardedJedis == null) {

  678. return result;

  679. }

  680. boolean flag = false;

  681. try {

  682. result = shardedJedis.rpush(key, string);

  683.  
  684. } catch (Exception e) {

  685. log.error(e.getMessage(), e);

  686. flag = true;

  687. } finally {

  688. redisService.returnResource(shardedJedis, flag);

  689. }

  690. return result;

  691. }

  692.  
  693. /**

  694. * 在redis list头部增加一个String

  695. * */

  696. public Long lpush(String key, String string) {

  697. Long result = null;

  698. ShardedJedis shardedJedis = redisService.getRedisClient();

  699. if (shardedJedis == null) {

  700. return result;

  701. }

  702. boolean flag = false;

  703. try {

  704. result = shardedJedis.lpush(key, string);

  705.  
  706. } catch (Exception e) {

  707. log.error(e.getMessage(), e);

  708. flag = true;

  709. } finally {

  710. redisService.returnResource(shardedJedis, flag);

  711. }

  712. return result;

  713. }

  714.  
  715. public Long llen(String key) {

  716. Long result = null;

  717. ShardedJedis shardedJedis = redisService.getRedisClient();

  718. if (shardedJedis == null) {

  719. return result;

  720. }

  721. boolean flag = false;

  722. try {

  723. result = shardedJedis.llen(key);

  724.  
  725. } catch (Exception e) {

  726. log.error(e.getMessage(), e);

  727. flag = true;

  728. } finally {

  729. redisService.returnResource(shardedJedis, flag);

  730. }

  731. return result;

  732. }

  733.  
  734. public List lrange(String key, long start, long end) {

  735. List result = null;

  736. ShardedJedis shardedJedis = redisService.getRedisClient();

  737. if (shardedJedis == null) {

  738. return result;

  739. }

  740. boolean flag = false;

  741. try {

  742. result = shardedJedis.lrange(key, start, end);

  743.  
  744. } catch (Exception e) {

  745. log.error(e.getMessage(), e);

  746. flag = true;

  747. } finally {

  748. redisService.returnResource(shardedJedis, flag);

  749. }

  750. return result;

  751. }

  752.  
  753. public String ltrim(String key, long start, long end) {

  754. String result = null;

  755. ShardedJedis shardedJedis = redisService.getRedisClient();

  756. if (shardedJedis == null) {

  757. return result;

  758. }

  759. boolean flag = false;

  760. try {

  761. result = shardedJedis.ltrim(key, start, end);

  762.  
  763. } catch (Exception e) {

  764. log.error(e.getMessage(), e);

  765. flag = true;

  766. } finally {

  767. redisService.returnResource(shardedJedis, flag);

  768. }

  769. return result;

  770. }

  771.  
  772. public String lIndex(String key, long index) {

  773. String result = null;

  774. ShardedJedis shardedJedis = redisService.getRedisClient();

  775. if (shardedJedis == null) {

  776. return result;

  777. }

  778. boolean flag = false;

  779. try {

  780. result = shardedJedis.lindex(key, index);

  781.  
  782. } catch (Exception e) {

  783. log.error(e.getMessage(), e);

  784. flag = true;

  785. } finally {

  786. redisService.returnResource(shardedJedis, flag);

  787. }

  788. return result;

  789. }

  790.  
  791. public String lset(String key, long index, String value) {

  792. String result = null;

  793. ShardedJedis shardedJedis = redisService.getRedisClient();

  794. if (shardedJedis == null) {

  795. return result;

  796. }

  797. boolean flag = false;

  798. try {

  799. result = shardedJedis.lset(key, index, value);

  800.  
  801. } catch (Exception e) {

  802. log.error(e.getMessage(), e);

  803. flag = true;

  804. } finally {

  805. redisService.returnResource(shardedJedis, flag);

  806. }

  807. return result;

  808. }

  809.  
  810. public Long lrem(String key, long count, String value) {

  811. Long result = null;

  812. ShardedJedis shardedJedis = redisService.getRedisClient();

  813. if (shardedJedis == null) {

  814. return result;

  815. }

  816. boolean flag = false;

  817. try {

  818. result = shardedJedis.lrem(key, count, value);

  819.  
  820. } catch (Exception e) {

  821. log.error(e.getMessage(), e);

  822. flag = true;

  823. } finally {

  824. redisService.returnResource(shardedJedis, flag);

  825. }

  826. return result;

  827. }

  828.  
  829. /**

  830. * 从redis list头部取出一个key

  831. * */

  832. public String lpop(String key) {

  833. String result = null;

  834. ShardedJedis shardedJedis = redisService.getRedisClient();

  835. if (shardedJedis == null) {

  836. return result;

  837. }

  838. boolean flag = false;

  839. try {

  840. result = shardedJedis.lpop(key);

  841.  
  842. } catch (Exception e) {

  843. log.error(e.getMessage(), e);

  844. flag = true;

  845. } finally {

  846. redisService.returnResource(shardedJedis, flag);

  847. }

  848. return result;

  849. }

  850.  
  851. /**

  852. * 从redis list尾部取出一个key

  853. * */

  854. public String rpop(String key) {

  855. String result = null;

  856. ShardedJedis shardedJedis = redisService.getRedisClient();

  857. if (shardedJedis == null) {

  858. return result;

  859. }

  860. boolean flag = false;

  861. try {

  862. result = shardedJedis.rpop(key);

  863.  
  864. } catch (Exception e) {

  865. log.error(e.getMessage(), e);

  866. flag = true;

  867. } finally {

  868. redisService.returnResource(shardedJedis, flag);

  869. }

  870. return result;

  871. }

  872.  
  873. public Long sadd(String key, String member) {

  874. Long result = null;

  875. ShardedJedis shardedJedis = redisService.getRedisClient();

  876. if (shardedJedis == null) {

  877. return result;

  878. }

  879. boolean flag = false;

  880. try {

  881. result = shardedJedis.sadd(key, member);

  882.  
  883. } catch (Exception e) {

  884. log.error(e.getMessage(), e);

  885. flag = true;

  886. } finally {

  887. redisService.returnResource(shardedJedis, flag);

  888. }

  889. return result;

  890. }

  891.  
  892. public Set smembers(String key) {

  893. Set result = null;

  894. ShardedJedis shardedJedis = redisService.getRedisClient();

  895. if (shardedJedis == null) {

  896. return result;

  897. }

  898. boolean flag = false;

  899. try {

  900. result = shardedJedis.smembers(key);

  901.  
  902. } catch (Exception e) {

  903. log.error(e.getMessage(), e);

  904. flag = true;

  905. } finally {

  906. redisService.returnResource(shardedJedis, flag);

  907. }

  908. return result;

  909. }

  910.  
  911. public Long srem(String key, String member) {

  912. ShardedJedis shardedJedis = redisService.getRedisClient();

  913.  
  914. Long result = null;

  915. if (shardedJedis == null) {

  916. return result;

  917. }

  918. boolean flag = false;

  919. try {

  920. result = shardedJedis.srem(key, member);

  921. } catch (Exception e) {

  922. log.error(e.getMessage(), e);

  923. flag = true;

  924. } finally {

  925. redisService.returnResource(shardedJedis, flag);

  926. }

  927. return result;

  928. }

  929.  
  930. public String spop(String key) {

  931. ShardedJedis shardedJedis = redisService.getRedisClient();

  932. String result = null;

  933. if (shardedJedis == null) {

  934. return result;

  935. }

  936. boolean flag = false;

  937. try {

  938. result = shardedJedis.spop(key);

  939. } catch (Exception e) {

  940. log.error(e.getMessage(), e);

  941. flag = true;

  942. } finally {

  943. redisService.returnResource(shardedJedis, flag);

  944. }

  945. return result;

  946. }

  947.  
  948. public Long scard(String key) {

  949. ShardedJedis shardedJedis = redisService.getRedisClient();

  950. Long result = null;

  951. if (shardedJedis == null) {

  952. return result;

  953. }

  954. boolean flag = false;

  955. try {

  956. result = shardedJedis.scard(key);

  957.  
  958. } catch (Exception e) {

  959. log.error(e.getMessage(), e);

  960. flag = true;

  961. } finally {

  962. redisService.returnResource(shardedJedis, flag);

  963. }

  964. return result;

  965. }

  966.  
  967. public Boolean sismember(String key, String member) {

  968. ShardedJedis shardedJedis = redisService.getRedisClient();

  969. Boolean result = null;

  970. if (shardedJedis == null) {

  971. return result;

  972. }

  973. boolean flag = false;

  974. try {

  975. result = shardedJedis.sismember(key, member);

  976. } catch (Exception e) {

  977. log.error(e.getMessage(), e);

  978. flag = true;

  979. } finally {

  980. redisService.returnResource(shardedJedis, flag);

  981. }

  982. return result;

  983. }

  984.  
  985. public String srandmember(String key) {

  986. ShardedJedis shardedJedis = redisService.getRedisClient();

  987. String result = null;

  988. if (shardedJedis == null) {

  989. return result;

  990. }

  991. boolean flag = false;

  992. try {

  993. result = shardedJedis.srandmember(key);

  994. } catch (Exception e) {

  995. log.error(e.getMessage(), e);

  996. flag = true;

  997. } finally {

  998. redisService.returnResource(shardedJedis, flag);

  999. }

  1000. return result;

  1001. }

  1002.  
  1003. public Long zadd(String key, double score, String member) {

  1004. Long result = null;

  1005. ShardedJedis shardedJedis = redisService.getRedisClient();

  1006. if (shardedJedis == null) {

  1007. return result;

  1008. }

  1009. boolean flag = false;

  1010. try {

  1011. result = shardedJedis.zadd(key, score, member);

  1012. } catch (Exception e) {

  1013. log.error(e.getMessage(), e);

  1014. flag = true;

  1015. } finally {

  1016. redisService.returnResource(shardedJedis, flag);

  1017. }

  1018. return result;

  1019. }

  1020.  
  1021. public Set zrange(String key, int start, int end) {

  1022. Set result = null;

  1023. ShardedJedis shardedJedis = redisService.getRedisClient();

  1024. if (shardedJedis == null) {

  1025. return result;

  1026. }

  1027. boolean flag = false;

  1028. try {

  1029. result = shardedJedis.zrange(key, start, end);

  1030. } catch (Exception e) {

  1031. log.error(e.getMessage(), e);

  1032. flag = true;

  1033. } finally {

  1034. redisService.returnResource(shardedJedis, flag);

  1035. }

  1036. return result;

  1037. }

  1038.  
  1039. public Long zrem(String key, String member) {

  1040. Long result = null;

  1041. ShardedJedis shardedJedis = redisService.getRedisClient();

  1042. if (shardedJedis == null) {

  1043. return result;

  1044. }

  1045. boolean flag = false;

  1046. try {

  1047. result = shardedJedis.zrem(key, member);

  1048. } catch (Exception e) {

  1049. log.error(e.getMessage(), e);

  1050. flag = true;

  1051. } finally {

  1052. redisService.returnResource(shardedJedis, flag);

  1053. }

  1054. return result;

  1055. }

  1056.  
  1057. public Double zincrby(String key, double score, String member) {

  1058. Double result = null;

  1059. ShardedJedis shardedJedis = redisService.getRedisClient();

  1060. if (shardedJedis == null) {

  1061. return result;

  1062. }

  1063. boolean flag = false;

  1064. try {

  1065.  
  1066. result = shardedJedis.zincrby(key, score, member);

  1067.  
  1068. } catch (Exception e) {

  1069. log.error(e.getMessage(), e);

  1070. flag = true;

  1071. } finally {

  1072. redisService.returnResource(shardedJedis, flag);

  1073. }

  1074. return result;

  1075. }

  1076.  
  1077. public Long zrank(String key, String member) {

  1078. Long result = null;

  1079. ShardedJedis shardedJedis = redisService.getRedisClient();

  1080. if (shardedJedis == null) {

  1081. return result;

  1082. }

  1083. boolean flag = false;

  1084. try {

  1085.  
  1086. result = shardedJedis.zrank(key, member);

  1087.  
  1088. } catch (Exception e) {

  1089. log.error(e.getMessage(), e);

  1090. flag = true;

  1091. } finally {

  1092. redisService.returnResource(shardedJedis, flag);

  1093. }

  1094. return result;

  1095. }

  1096.  
  1097. public Long zrevrank(String key, String member) {

  1098. Long result = null;

  1099. ShardedJedis shardedJedis = redisService.getRedisClient();

  1100. if (shardedJedis == null) {

  1101. return result;

  1102. }

  1103. boolean flag = false;

  1104. try {

  1105.  
  1106. result = shardedJedis.zrevrank(key, member);

  1107.  
  1108. } catch (Exception e) {

  1109. log.error(e.getMessage(), e);

  1110. flag = true;

  1111. } finally {

  1112. redisService.returnResource(shardedJedis, flag);

  1113. }

  1114. return result;

  1115. }

  1116.  
  1117. public Set zrevrange(String key, int start, int end) {

  1118. Set result = null;

  1119. ShardedJedis shardedJedis = redisService.getRedisClient();

  1120. if (shardedJedis == null) {

  1121. return result;

  1122. }

  1123. boolean flag = false;

  1124. try {

  1125.  
  1126. result = shardedJedis.zrevrange(key, start, end);

  1127.  
  1128. } catch (Exception e) {

  1129. log.error(e.getMessage(), e);

  1130. flag = true;

  1131. } finally {

  1132. redisService.returnResource(shardedJedis, flag);

  1133. }

  1134. return result;

  1135. }

  1136.  
  1137. public Set zrangeWithScores(String key, int start, int end) {

  1138. Set result = null;

  1139. ShardedJedis shardedJedis = redisService.getRedisClient();

  1140. if (shardedJedis == null) {

  1141. return result;

  1142. }

  1143. boolean flag = false;

  1144. try {

  1145.  
  1146. result = shardedJedis.zrangeWithScores(key, start, end);

  1147.  
  1148. } catch (Exception e) {

  1149. log.error(e.getMessage(), e);

  1150. flag = true;

  1151. } finally {

  1152. redisService.returnResource(shardedJedis, flag);

  1153. }

  1154. return result;

  1155. }

你可能感兴趣的:(java)