redis在Java web项目的简单应用

来源:互联网 发布:url的正则表达式php 编辑:程序博客网 时间:2024/04/30 04:03

redis在Java web项目的简单应用

19116人阅读 评论(0)收藏举报
本文章已收录于:

目录(?)[+]

  1. 一准备要使用的的jar包我使用了以下jar包关于json的jar包是方便存储和转换redis用到的值
  2. 二编写加载redis是要用到的实例类
  3. 四代码中的使用原则就是在执行service时先通过key到redis里检测是否存在如果存在则在redis里去数据如果没有则到数据库去数据然后存到redis里

一.准备要使用的的jar包,我使用了以下jar包,关于json的jar包是方便存储和转换redis用到的值;

二.编写加载redis是要用到的实例类

[java] view plain copy
print?
  1. package com.redis;  
  2.   
  3. import redis.clients.jedis.ShardedJedis;  
  4.   
  5. /**   
  6.  * @Title: RedisDataSource.java 
  7.  * @copyright  
  8.  * @Package com.cy.redis 
  9.  * @Description: 该接口用于获取redis客户端实例,通过该实例操作redis数据源 
  10.  * @author  
  11.  * @date 2016-10-21 下午04:22:41 
  12.  */  
  13. public interface RedisDataSource {  
  14.       
  15.     public abstract ShardedJedis getRedisClient();  
  16.       
  17.     public void returnResource(ShardedJedis shardedJedis);  
  18.       
  19.         public void returnResource(ShardedJedis shardedJedis,boolean broken);  
  20.   
  21. }  

[java] view plain copy
print?
  1. package com.redis;  
  2.   
  3. import org.slf4j.LoggerFactory;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5.   
  6. import redis.clients.jedis.ShardedJedis;  
  7. import redis.clients.jedis.ShardedJedisPool;  
  8.   
  9. /**   
  10.  * @Title: RedisDataSourceImpl.java 
  11.  * @copyright  
  12.  * @Package com.cy.redis 
  13.  * @Description: 获取redis客户端实例,通过该实例操作redis数据源 
  14.  * @author  
  15.  * @date 2016-10-21 下午04:22:54 
  16.  */  
  17. public class RedisDataSourceImpl implements RedisDataSource{  
  18.   
  19.     private static final org.slf4j.Logger log = LoggerFactory.getLogger(RedisDataSourceImpl.class);  
  20.       
  21.     @Autowired  
  22.     private ShardedJedisPool shardedJedisPool;  
  23.       
  24.     public ShardedJedisPool getShardedJedisPool() {  
  25.         return shardedJedisPool;  
  26.     }  
  27.   
  28.     public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {  
  29.         this.shardedJedisPool = shardedJedisPool;  
  30.     }  
  31.   
  32.     /** 
  33.      * 获取redis客户端 
  34.      */  
  35.     public ShardedJedis getRedisClient() {  
  36.         try {  
  37.             ShardedJedis shardedJedis=shardedJedisPool.getResource();  
  38.             return shardedJedis;  
  39.         } catch (Exception e) {  
  40.             log.error("getRedisClient ,error",e);  
  41.             e.printStackTrace();  
  42.         }  
  43.         return null;  
  44.     }  
  45.   
  46.     /** 
  47.      * 将资源返还给pool 
  48.      */  
  49.     @SuppressWarnings("deprecation")  
  50.     public void returnResource(ShardedJedis shardedJedis) {  
  51.         shardedJedisPool.returnResource(shardedJedis);  
  52.           
  53.     }  
  54.   
  55.     /** 
  56.      * 出现异常后返回资源给pool 
  57.      */  
  58.     @SuppressWarnings("deprecation")  
  59.     public void returnResource(ShardedJedis shardedJedis, boolean broken) {  
  60.         if(broken){  
  61.             shardedJedisPool.returnBrokenResource(shardedJedis);  
  62.         }else{  
  63.             shardedJedisPool.returnResource(shardedJedis);  
  64.         }  
  65.           
  66.     }  
  67.   
  68. }  

[java] view plain copy
print?
  1. package com.redis;  
  2.   
  3. import org.slf4j.LoggerFactory;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5.   
  6. import redis.clients.jedis.Jedis;  
  7. import redis.clients.jedis.JedisShardInfo;  
  8. import redis.clients.jedis.ShardedJedis;  
  9.   
  10. /**   
  11.  * @Title: RedisClientTemplate.java 
  12.  * @copyright  
  13.  * @Package com.cy.redis 
  14.  * @Description: 用于操作redis取值、存值操作的工具类 
  15.  * @author  
  16.  * @date 2016-10-21 下午04:21:59 
  17.  */  
  18. public class RedisClientTemplate {  
  19.       
  20.     private static final org.slf4j.Logger log=LoggerFactory.getLogger(RedisClientTemplate.class);  
  21.       
  22.     @Autowired  
  23.     private RedisDataSource redisDataSource;  
  24.   
  25.     public RedisDataSource getRedisDataSource() {  
  26.         return redisDataSource;  
  27.     }  
  28.   
  29.     public void setRedisDataSource(RedisDataSource redisDataSource) {  
  30.         this.redisDataSource = redisDataSource;  
  31.     }  
  32.       
  33.     public void disconnect(){  
  34.         ShardedJedis shardedJedis=redisDataSource.getRedisClient();  
  35.         shardedJedis.disconnect();  
  36.     }  
  37.       
  38.     /** 
  39.     * @Description: 在redis里设置单个值 
  40.     * @author Mr.chen 
  41.     * @date 2016-10-21 下午04:37:06 
  42.      */  
  43.     public String set(String key, String value){  
  44.         String result=null;  
  45.         ShardedJedis shardedJedis=redisDataSource.getRedisClient();  
  46.         if(shardedJedis==null){  
  47.             return result;  
  48.         }  
  49.         boolean broken=false;  
  50.         try {  
  51.             result=shardedJedis.set(key, value);  
  52.         } catch (Exception e) {  
  53.             broken=true;  
  54.             e.printStackTrace();  
  55.         }finally{  
  56.             redisDataSource.returnResource(shardedJedis, broken);  
  57.         }  
  58.           
  59.         return result;  
  60.     }  
  61.       
  62.     /** 
  63.     * @Description: 获取redis单个值  
  64.     * @author Mr.chen 
  65.     * @date 2016-10-21 下午04:40:57 
  66.      */  
  67.     public String get(String key) {  
  68.         String result = null;  
  69.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  70.         if (shardedJedis == null) {  
  71.             return result;  
  72.         }  
  73.         boolean broken = false;  
  74.         try {  
  75.             result = shardedJedis.get(key);  
  76.   
  77.         } catch (Exception e) {  
  78.             log.error(e.getMessage(), e);  
  79.             broken = true;  
  80.         } finally {  
  81.             redisDataSource.returnResource(shardedJedis, broken);  
  82.         }  
  83.         return result;  
  84.     }  
  85.       
  86.     /** 
  87.     * @Description: 判断redis是否存在key 
  88.     * @author Mr.chen 
  89.     * @date 2016-10-21 下午04:41:39 
  90.      */  
  91.     public Boolean exists(String key) {  
  92.         Boolean result = false;  
  93.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  94.         if (shardedJedis == null) {  
  95.             return result;  
  96.         }  
  97.         boolean broken = false;  
  98.         try {  
  99.             result = shardedJedis.exists(key);  
  100.         } catch (Exception e) {  
  101.             log.error(e.getMessage(), e);  
  102.             broken = true;  
  103.         } finally {  
  104.             redisDataSource.returnResource(shardedJedis, broken);  
  105.         }  
  106.         return result;  
  107.     }  
  108.       
  109.     /** 
  110.     * @Description: 获取key返回存储值的类型  
  111.     * @author Mr.chen 
  112.     * @date 2016-10-21 下午04:42:08 
  113.      */  
  114.     public String type(String key) {  
  115.         String result = null;  
  116.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  117.         if (shardedJedis == null) {  
  118.             return result;  
  119.         }  
  120.         boolean broken = false;  
  121.         try {  
  122.             result = shardedJedis.type(key);  
  123.   
  124.         } catch (Exception e) {  
  125.             log.error(e.getMessage(), e);  
  126.             broken = true;  
  127.         } finally {  
  128.             redisDataSource.returnResource(shardedJedis, broken);  
  129.         }  
  130.         return result;  
  131.     }  
  132.       
  133.     /** 
  134.     * @Description: 在某段时间后实现 
  135.     * @author Mr.chen 
  136.     * @date 2016-10-21 下午04:43:25 
  137.      */  
  138.     public Long expire(String key, int seconds) {  
  139.         Long result = null;  
  140.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  141.         if (shardedJedis == null) {  
  142.             return result;  
  143.         }  
  144.         boolean broken = false;  
  145.         try {  
  146.             result = shardedJedis.expire(key, seconds);  
  147.   
  148.         } catch (Exception e) {  
  149.             log.error(e.getMessage(), e);  
  150.             broken = true;  
  151.         } finally {  
  152.             redisDataSource.returnResource(shardedJedis, broken);  
  153.         }  
  154.         return result;  
  155.     }  
  156.   
  157.     /** 
  158.     * @Description: 在某个时间点失效  
  159.     * @author Mr.chen 
  160.     * @date 2016-10-21 下午04:43:40 
  161.      */  
  162.     public Long expireAt(String key, long unixTime) {  
  163.         Long result = null;  
  164.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  165.         if (shardedJedis == null) {  
  166.             return result;  
  167.         }  
  168.         boolean broken = false;  
  169.         try {  
  170.             result = shardedJedis.expireAt(key, unixTime);  
  171.   
  172.         } catch (Exception e) {  
  173.             log.error(e.getMessage(), e);  
  174.             broken = true;  
  175.         } finally {  
  176.             redisDataSource.returnResource(shardedJedis, broken);  
  177.         }  
  178.         return result;  
  179.     }  
  180.   
  181.     /** 
  182.     * @Description: 以秒为单位,返回给定 key 的剩余生存时间 
  183.     * @author Mr.chen 
  184.     * @date 2016-10-21 下午04:44:00 
  185.      */  
  186.     public Long ttl(String key) {  
  187.         Long result = null;  
  188.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  189.         if (shardedJedis == null) {  
  190.             return result;  
  191.         }  
  192.         boolean broken = false;  
  193.         try {  
  194.             result = shardedJedis.ttl(key);  
  195.   
  196.         } catch (Exception e) {  
  197.             log.error(e.getMessage(), e);  
  198.             broken = true;  
  199.         } finally {  
  200.             redisDataSource.returnResource(shardedJedis, broken);  
  201.         }  
  202.         return result;  
  203.     }  
  204.   
  205.     /** 
  206.     * @Description: 将 key 的值设为 value ,当且仅当 key 不存在 
  207.     * @author Mr.chen 
  208.     * @date 2016-10-21 下午04:44:17 
  209.      */  
  210.     public Long setnx(String key, String value) {  
  211.         Long result = null;  
  212.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  213.         if (shardedJedis == null) {  
  214.             return result;  
  215.         }  
  216.         boolean broken = false;  
  217.         try {  
  218.             result = shardedJedis.setnx(key, value);  
  219.         } catch (Exception e) {  
  220.             log.error(e.getMessage(), e);  
  221.             broken = true;  
  222.         } finally {  
  223.             redisDataSource.returnResource(shardedJedis, broken);  
  224.         }  
  225.         return result;  
  226.     }  
  227.     /** 
  228.      * 将值 value 关联到 key ,并将 key 的生存时间设为 seconds (以秒为单位) 
  229.      * @param key 
  230.      * @param seconds 
  231.      * @param value 
  232.      * @return 
  233.      */  
  234.     public String setex(String key, int seconds, String value) {  
  235.         String result = null;  
  236.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  237.         if (shardedJedis == null) {  
  238.             return result;  
  239.         }  
  240.         boolean broken = false;  
  241.         try {  
  242.             result = shardedJedis.setex(key, seconds, value);  
  243.   
  244.         } catch (Exception e) {  
  245.             log.error(e.getMessage(), e);  
  246.             broken = true;  
  247.         } finally {  
  248.             redisDataSource.returnResource(shardedJedis, broken);  
  249.         }  
  250.         return result;  
  251.     }  
  252.     /** 
  253.      * 将 key 所储存的值减去减量 integer  
  254.      * @param key 
  255.      * @param integer 
  256.      * @return 
  257.      */  
  258.     public Long decrBy(String key, long integer) {  
  259.         Long result = null;  
  260.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  261.         if (shardedJedis == null) {  
  262.             return result;  
  263.         }  
  264.         boolean broken = false;  
  265.         try {  
  266.             result = shardedJedis.decrBy(key, integer);  
  267.   
  268.         } catch (Exception e) {  
  269.             log.error(e.getMessage(), e);  
  270.             broken = true;  
  271.         } finally {  
  272.             redisDataSource.returnResource(shardedJedis, broken);  
  273.         }  
  274.         return result;  
  275.     }  
  276.     /** 
  277.      * 将 key 中储存的数字值减一。 
  278.      * @param key 
  279.      * @return 
  280.      */  
  281.     public Long decr(String key) {  
  282.         Long result = null;  
  283.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  284.         if (shardedJedis == null) {  
  285.             return result;  
  286.         }  
  287.         boolean broken = false;  
  288.         try {  
  289.             result = shardedJedis.decr(key);  
  290.   
  291.         } catch (Exception e) {  
  292.             log.error(e.getMessage(), e);  
  293.             broken = true;  
  294.         } finally {  
  295.             redisDataSource.returnResource(shardedJedis, broken);  
  296.         }  
  297.         return result;  
  298.     }  
  299.     /** 
  300.      * 将 key 所储存的值加上增量 integer  
  301.      * @param key 
  302.      * @param integer 
  303.      * @return 
  304.      */  
  305.     public Long incrBy(String key, long integer) {  
  306.         Long result = null;  
  307.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  308.         if (shardedJedis == null) {  
  309.             return result;  
  310.         }  
  311.         boolean broken = false;  
  312.         try {  
  313.             result = shardedJedis.incrBy(key, integer);  
  314.   
  315.         } catch (Exception e) {  
  316.             log.error(e.getMessage(), e);  
  317.             broken = true;  
  318.         } finally {  
  319.             redisDataSource.returnResource(shardedJedis, broken);  
  320.         }  
  321.         return result;  
  322.     }  
  323.     /** 
  324.      * 将 key 中储存的数字值增一 
  325.      * @param key 
  326.      * @return 
  327.      */  
  328.     public Long incr(String key) {  
  329.         Long result = null;  
  330.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  331.         if (shardedJedis == null) {  
  332.             return result;  
  333.         }  
  334.         boolean broken = false;  
  335.         try {  
  336.             result = shardedJedis.incr(key);  
  337.   
  338.         } catch (Exception e) {  
  339.             log.error(e.getMessage(), e);  
  340.             broken = true;  
  341.         } finally {  
  342.             redisDataSource.returnResource(shardedJedis, broken);  
  343.         }  
  344.         return result;  
  345.     }  
  346.     /** 
  347.      * 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 
  348.      * 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 
  349.      * @param key 
  350.      * @param value 
  351.      * @return 
  352.      */  
  353.     public Long append(String key, String value) {  
  354.         Long result = null;  
  355.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  356.         if (shardedJedis == null) {  
  357.             return result;  
  358.         }  
  359.         boolean broken = false;  
  360.         try {  
  361.             result = shardedJedis.append(key, value);  
  362.   
  363.         } catch (Exception e) {  
  364.             log.error(e.getMessage(), e);  
  365.             broken = true;  
  366.         } finally {  
  367.             redisDataSource.returnResource(shardedJedis, broken);  
  368.         }  
  369.         return result;  
  370.     }  
  371.     /** 
  372.      * 返回名称为key的string的value的子串 
  373.      * @param key 
  374.      * @param start 
  375.      * @param end 
  376.      * @return 
  377.      */  
  378.     public String substr(String key, int start, int end) {  
  379.         String result = null;  
  380.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  381.         if (shardedJedis == null) {  
  382.             return result;  
  383.         }  
  384.         boolean broken = false;  
  385.         try {  
  386.             result = shardedJedis.substr(key, start, end);  
  387.   
  388.         } catch (Exception e) {  
  389.             log.error(e.getMessage(), e);  
  390.             broken = true;  
  391.         } finally {  
  392.             redisDataSource.returnResource(shardedJedis, broken);  
  393.         }  
  394.         return result;  
  395.     }  
  396.     /** 
  397.      * 将哈希表 key 中的域 field 的值设为 value  
  398.      * @param key 
  399.      * @param field 
  400.      * @param value 
  401.      * @return 
  402.      */  
  403.     public Long hset(String key, String field, String value) {  
  404.         Long result = null;  
  405.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  406.         if (shardedJedis == null) {  
  407.             return result;  
  408.         }  
  409.         boolean broken = false;  
  410.         try {  
  411.             result = shardedJedis.hset(key, field, value);  
  412.   
  413.         } catch (Exception e) {  
  414.             log.error(e.getMessage(), e);  
  415.             broken = true;  
  416.         } finally {  
  417.             redisDataSource.returnResource(shardedJedis, broken);  
  418.         }  
  419.         return result;  
  420.     }  
  421.     /** 
  422.      * 返回哈希表 key 中给定域 field 的值 
  423.      * @param key 
  424.      * @param field 
  425.      * @return 
  426.      */  
  427.     public String hget(String key, String field) {  
  428.         String result = null;  
  429.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  430.         if (shardedJedis == null) {  
  431.             return result;  
  432.         }  
  433.         boolean broken = false;  
  434.         try {  
  435.             result = shardedJedis.hget(key, field);  
  436.   
  437.         } catch (Exception e) {  
  438.             log.error(e.getMessage(), e);  
  439.             broken = true;  
  440.         } finally {  
  441.             redisDataSource.returnResource(shardedJedis, broken);  
  442.         }  
  443.         return result;  
  444.     }  
  445.     /** 
  446.      * 同时将多个 field-value (域-值)对设置到哈希表 key 中。 
  447.      * @param key 
  448.      * @param hash 
  449.      * @return 
  450.      */  
  451.     public String hmset(String key, Map<String, String> hash) {  
  452.         String result = null;  
  453.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  454.         if (shardedJedis == null) {  
  455.             return result;  
  456.         }  
  457.         boolean broken = false;  
  458.         try {  
  459.             result = shardedJedis.hmset(key, hash);  
  460.   
  461.         } catch (Exception e) {  
  462.             log.error(e.getMessage(), e);  
  463.             broken = true;  
  464.         } finally {  
  465.             redisDataSource.returnResource(shardedJedis, broken);  
  466.         }  
  467.         return result;  
  468.     }  
  469.     /** 
  470.      * 返回哈希表 key 中,一个或多个给定域的值 
  471.      * @param key 
  472.      * @param fields 
  473.      * @return 
  474.      */  
  475.     public List<String> hmget(String key, String... fields) {  
  476.         List<String> result = null;  
  477.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  478.         if (shardedJedis == null) {  
  479.             return result;  
  480.         }  
  481.         boolean broken = false;  
  482.         try {  
  483.             result = shardedJedis.hmget(key, fields);  
  484.   
  485.         } catch (Exception e) {  
  486.             log.error(e.getMessage(), e);  
  487.             broken = true;  
  488.         } finally {  
  489.             redisDataSource.returnResource(shardedJedis, broken);  
  490.         }  
  491.         return result;  
  492.     }  
  493.     /** 
  494.      * 为哈希表 key 中的域 field 的值加上增量 value 
  495.      * @param key 
  496.      * @param field 
  497.      * @param value 
  498.      * @return 
  499.      */  
  500.     public Long hincrBy(String key, String field, long value) {  
  501.         Long result = null;  
  502.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  503.         if (shardedJedis == null) {  
  504.             return result;  
  505.         }  
  506.         boolean broken = false;  
  507.         try {  
  508.             result = shardedJedis.hincrBy(key, field, value);  
  509.   
  510.         } catch (Exception e) {  
  511.             log.error(e.getMessage(), e);  
  512.             broken = true;  
  513.         } finally {  
  514.             redisDataSource.returnResource(shardedJedis, broken);  
  515.         }  
  516.         return result;  
  517.     }  
  518.     /** 
  519.      * 查看哈希表 key 中,给定域 field 是否存在。 
  520.      * @param key 
  521.      * @param field 
  522.      * @return 
  523.      */  
  524.     public Boolean hexists(String key, String field) {  
  525.         Boolean result = false;  
  526.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  527.         if (shardedJedis == null) {  
  528.             return result;  
  529.         }  
  530.         boolean broken = false;  
  531.         try {  
  532.             result = shardedJedis.hexists(key, field);  
  533.   
  534.         } catch (Exception e) {  
  535.             log.error(e.getMessage(), e);  
  536.             broken = true;  
  537.         } finally {  
  538.             redisDataSource.returnResource(shardedJedis, broken);  
  539.         }  
  540.         return result;  
  541.     }  
  542.     /** 
  543.      * 删除key 
  544.      * @param key 
  545.      * @return 
  546.      */  
  547.     public Long del(String key) {  
  548.         Long result = null;  
  549.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  550.         if (shardedJedis == null) {  
  551.             return result;  
  552.         }  
  553.         boolean broken = false;  
  554.         try {  
  555.             result = shardedJedis.del(key);  
  556.   
  557.         } catch (Exception e) {  
  558.             log.error(e.getMessage(), e);  
  559.             broken = true;  
  560.         } finally {  
  561.             redisDataSource.returnResource(shardedJedis, broken);  
  562.         }  
  563.         return result;  
  564.     }  
  565.     /** 
  566.      * 删除哈希表 key 中的一个或多个指定域。 
  567.      * @param key 
  568.      * @param field 
  569.      * @return 
  570.      */  
  571.     public Long hdel(String key, String field) {  
  572.         Long result = null;  
  573.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  574.         if (shardedJedis == null) {  
  575.             return result;  
  576.         }  
  577.         boolean broken = false;  
  578.         try {  
  579.             result = shardedJedis.hdel(key, field);  
  580.   
  581.         } catch (Exception e) {  
  582.             log.error(e.getMessage(), e);  
  583.             broken = true;  
  584.         } finally {  
  585.             redisDataSource.returnResource(shardedJedis, broken);  
  586.         }  
  587.         return result;  
  588.     }  
  589.     /** 
  590.      * 返回哈希表 key 中域的数量。 
  591.      * @param key 
  592.      * @return 
  593.      */  
  594.     public Long hlen(String key) {  
  595.         Long result = null;  
  596.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  597.         if (shardedJedis == null) {  
  598.             return result;  
  599.         }  
  600.         boolean broken = false;  
  601.         try {  
  602.             result = shardedJedis.hlen(key);  
  603.   
  604.         } catch (Exception e) {  
  605.             log.error(e.getMessage(), e);  
  606.             broken = true;  
  607.         } finally {  
  608.             redisDataSource.returnResource(shardedJedis, broken);  
  609.         }  
  610.         return result;  
  611.     }  
  612.     /** 
  613.      * 返回哈希表 key 中的所有域。 
  614.      * @param key 
  615.      * @return 
  616.      */  
  617.     public Set<String> hkeys(String key) {  
  618.         Set<String> result = null;  
  619.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  620.         if (shardedJedis == null) {  
  621.             return result;  
  622.         }  
  623.         boolean broken = false;  
  624.         try {  
  625.             result = shardedJedis.hkeys(key);  
  626.   
  627.         } catch (Exception e) {  
  628.             log.error(e.getMessage(), e);  
  629.             broken = true;  
  630.         } finally {  
  631.             redisDataSource.returnResource(shardedJedis, broken);  
  632.         }  
  633.         return result;  
  634.     }  
  635.     /** 
  636.      * 返回哈希表 key 中所有域的值。 
  637.      * @param key 
  638.      * @return 
  639.      */  
  640.     public List<String> hvals(String key) {  
  641.         List<String> result = null;  
  642.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  643.         if (shardedJedis == null) {  
  644.             return result;  
  645.         }  
  646.         boolean broken = false;  
  647.         try {  
  648.             result = shardedJedis.hvals(key);  
  649.   
  650.         } catch (Exception e) {  
  651.             log.error(e.getMessage(), e);  
  652.             broken = true;  
  653.         } finally {  
  654.             redisDataSource.returnResource(shardedJedis, broken);  
  655.         }  
  656.         return result;  
  657.     }  
  658.      /** 
  659.       * 返回哈希表 key 中,所有的域和值。 
  660.       * @param key 
  661.       * @return 
  662.       */  
  663.     public Map<String, String> hgetAll(String key) {  
  664.         Map<String, String> result = null;  
  665.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  666.         if (shardedJedis == null) {  
  667.             return result;  
  668.         }  
  669.         boolean broken = false;  
  670.         try {  
  671.             result = shardedJedis.hgetAll(key);  
  672.   
  673.         } catch (Exception e) {  
  674.             log.error(e.getMessage(), e);  
  675.             broken = true;  
  676.         } finally {  
  677.             redisDataSource.returnResource(shardedJedis, broken);  
  678.         }  
  679.         return result;  
  680.     }  
  681.   
  682.     // ================list ====== l表示 list或 left, r表示right====================  
  683.     /** 
  684.      * 将一个或多个值 value 插入到列表 key 的表尾(最右边) 
  685.      * @param key 
  686.      * @param string 
  687.      * @return 
  688.      */  
  689.     public Long rpush(String key, String string) {  
  690.         Long result = null;  
  691.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  692.         if (shardedJedis == null) {  
  693.             return result;  
  694.         }  
  695.         boolean broken = false;  
  696.         try {  
  697.             result = shardedJedis.rpush(key, string);  
  698.   
  699.         } catch (Exception e) {  
  700.             log.error(e.getMessage(), e);  
  701.             broken = true;  
  702.         } finally {  
  703.             redisDataSource.returnResource(shardedJedis, broken);  
  704.         }  
  705.         return result;  
  706.     }  
  707.     /** 
  708.      * 将一个或多个值 value 插入到列表 key 的表头 
  709.      * @param key 
  710.      * @param string 
  711.      * @return 
  712.      */  
  713.     public Long lpush(String key, String string) {  
  714.         Long result = null;  
  715.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  716.         if (shardedJedis == null) {  
  717.             return result;  
  718.         }  
  719.         boolean broken = false;  
  720.         try {  
  721.             result = shardedJedis.lpush(key, string);  
  722.   
  723.         } catch (Exception e) {  
  724.             log.error(e.getMessage(), e);  
  725.             broken = true;  
  726.         } finally {  
  727.             redisDataSource.returnResource(shardedJedis, broken);  
  728.         }  
  729.         return result;  
  730.     }  
  731.     /** 
  732.      * 返回列表 key 的长度。 
  733.      * @param key 
  734.      * @return 
  735.      */  
  736.     public Long llen(String key) {  
  737.         Long result = null;  
  738.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  739.         if (shardedJedis == null) {  
  740.             return result;  
  741.         }  
  742.         boolean broken = false;  
  743.         try {  
  744.             result = shardedJedis.llen(key);  
  745.   
  746.         } catch (Exception e) {  
  747.             log.error(e.getMessage(), e);  
  748.             broken = true;  
  749.         } finally {  
  750.             redisDataSource.returnResource(shardedJedis, broken);  
  751.         }  
  752.         return result;  
  753.     }  
  754.      /** 
  755.       * 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定 
  756.       * @param key 
  757.       * @param start 
  758.       * @param end 
  759.       * @return 
  760.       */  
  761.     public List<String> lrange(String key, long start, long end) {  
  762.         List<String> result = null;  
  763.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  764.         if (shardedJedis == null) {  
  765.             return result;  
  766.         }  
  767.         boolean broken = false;  
  768.         try {  
  769.             result = shardedJedis.lrange(key, start, end);  
  770.   
  771.         } catch (Exception e) {  
  772.             log.error(e.getMessage(), e);  
  773.             broken = true;  
  774.         } finally {  
  775.             redisDataSource.returnResource(shardedJedis, broken);  
  776.         }  
  777.         return result;  
  778.     }  
  779.     /** 
  780.      * 只保留指定区间内的元素,不在指定区间之内的元素都将被删除 
  781.      * @param key 
  782.      * @param start 
  783.      * @param end 
  784.      * @return 
  785.      */  
  786.     public String ltrim(String key, long start, long end) {  
  787.         String result = null;  
  788.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  789.         if (shardedJedis == null) {  
  790.             return result;  
  791.         }  
  792.         boolean broken = false;  
  793.         try {  
  794.             result = shardedJedis.ltrim(key, start, end);  
  795.   
  796.         } catch (Exception e) {  
  797.             log.error(e.getMessage(), e);  
  798.             broken = true;  
  799.         } finally {  
  800.             redisDataSource.returnResource(shardedJedis, broken);  
  801.         }  
  802.         return result;  
  803.     }  
  804.     /** 
  805.      * 返回列表 key 中,下标为 index 的元素。 
  806.      * @param key 
  807.      * @param index 
  808.      * @return 
  809.      */  
  810.     public String lindex(String key, long index) {  
  811.         String result = null;  
  812.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  813.         if (shardedJedis == null) {  
  814.             return result;  
  815.         }  
  816.         boolean broken = false;  
  817.         try {  
  818.             result = shardedJedis.lindex(key, index);  
  819.   
  820.         } catch (Exception e) {  
  821.             log.error(e.getMessage(), e);  
  822.             broken = true;  
  823.         } finally {  
  824.             redisDataSource.returnResource(shardedJedis, broken);  
  825.         }  
  826.         return result;  
  827.     }  
  828.     /** 
  829.      * 将列表 key 下标为 index 的元素的值设置为 value 
  830.      * @param key 
  831.      * @param index 
  832.      * @param value 
  833.      * @return 
  834.      */  
  835.     public String lset(String key, long index, String value) {  
  836.         String result = null;  
  837.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  838.         if (shardedJedis == null) {  
  839.             return result;  
  840.         }  
  841.         boolean broken = false;  
  842.         try {  
  843.             result = shardedJedis.lset(key, index, value);  
  844.   
  845.         } catch (Exception e) {  
  846.             log.error(e.getMessage(), e);  
  847.             broken = true;  
  848.         } finally {  
  849.             redisDataSource.returnResource(shardedJedis, broken);  
  850.         }  
  851.         return result;  
  852.     }  
  853.   
  854.   /** 
  855.    * 移除并返回列表 key 的头元素 
  856.    * @param key 
  857.    * @return 
  858.    */  
  859.     public String lpop(String key) {  
  860.         String result = null;  
  861.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  862.         if (shardedJedis == null) {  
  863.             return result;  
  864.         }  
  865.         boolean broken = false;  
  866.         try {  
  867.             result = shardedJedis.lpop(key);  
  868.   
  869.         } catch (Exception e) {  
  870.             log.error(e.getMessage(), e);  
  871.             broken = true;  
  872.         } finally {  
  873.             redisDataSource.returnResource(shardedJedis, broken);  
  874.         }  
  875.         return result;  
  876.     }  
  877.     /** 
  878.      * 移除并返回列表 key 的尾元素。 
  879.      * @param key 
  880.      * @return 
  881.      */  
  882.     public String rpop(String key) {  
  883.         String result = null;  
  884.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  885.         if (shardedJedis == null) {  
  886.             return result;  
  887.         }  
  888.         boolean broken = false;  
  889.         try {  
  890.             result = shardedJedis.rpop(key);  
  891.   
  892.         } catch (Exception e) {  
  893.             log.error(e.getMessage(), e);  
  894.             broken = true;  
  895.         } finally {  
  896.             redisDataSource.returnResource(shardedJedis, broken);  
  897.         }  
  898.         return result;  
  899.     }  
  900.   
  901.     //return 1 add a not exist value ,  
  902.     //return 0 add a exist value  
  903.     /** 
  904.      * 将一个或多个 member 元素加入到集合 key 当中 
  905.      * @param key 
  906.      * @param member 
  907.      * @return 
  908.      */  
  909.     public Long sadd(String key, String member) {  
  910.         Long result = null;  
  911.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  912.         if (shardedJedis == null) {  
  913.             return result;  
  914.         }  
  915.         boolean broken = false;  
  916.         try {  
  917.             result = shardedJedis.sadd(key, member);  
  918.   
  919.         } catch (Exception e) {  
  920.             log.error(e.getMessage(), e);  
  921.             broken = true;  
  922.         } finally {  
  923.             redisDataSource.returnResource(shardedJedis, broken);  
  924.         }  
  925.         return result;  
  926.     }  
  927.     /** 
  928.      * 返回集合 key 中的所有成员。 
  929.      * @param key 
  930.      * @return 
  931.      */  
  932.     public Set<String> smembers(String key) {  
  933.         Set<String> result = null;  
  934.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  935.         if (shardedJedis == null) {  
  936.             return result;  
  937.         }  
  938.         boolean broken = false;  
  939.         try {  
  940.             result = shardedJedis.smembers(key);  
  941.   
  942.         } catch (Exception e) {  
  943.             log.error(e.getMessage(), e);  
  944.             broken = true;  
  945.         } finally {  
  946.             redisDataSource.returnResource(shardedJedis, broken);  
  947.         }  
  948.         return result;  
  949.     }  
  950.   
  951.     /** 
  952.      * 返回集合 key 的基数(集合中元素的数量) 
  953.      * @param key 
  954.      * @return 
  955.      */  
  956.     public Long scard(String key) {  
  957.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  958.         Long result = null;  
  959.         if (shardedJedis == null) {  
  960.             return result;  
  961.         }  
  962.         boolean broken = false;  
  963.         try {  
  964.             result = shardedJedis.scard(key);  
  965.   
  966.         } catch (Exception e) {  
  967.             log.error(e.getMessage(), e);  
  968.             broken = true;  
  969.         } finally {  
  970.             redisDataSource.returnResource(shardedJedis, broken);  
  971.         }  
  972.         return result;  
  973.     }  
  974.     /** 
  975.      * 将一个或多个 member 元素及其 score 值加入到有序集 key 当中 
  976.      * @param key 
  977.      * @param score 
  978.      * @param member 
  979.      * @return 
  980.      */  
  981.     public Long zadd(String key, double score, String member) {  
  982.         Long result = null;  
  983.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  984.         if (shardedJedis == null) {  
  985.             return result;  
  986.         }  
  987.         boolean broken = false;  
  988.         try {  
  989.             result = shardedJedis.zadd(key, score, member);  
  990.         } catch (Exception e) {  
  991.             log.error(e.getMessage(), e);  
  992.             broken = true;  
  993.         } finally {  
  994.             redisDataSource.returnResource(shardedJedis, broken);  
  995.         }  
  996.         return result;  
  997.     }  
  998.     /** 
  999.      * 返回有序集 key 中,指定区间内的成员 
  1000.      * @param key 
  1001.      * @param start 
  1002.      * @param end 
  1003.      * @return 
  1004.      */  
  1005.     public Set<String> zrange(String key, int start, int end) {  
  1006.         Set<String> result = null;  
  1007.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1008.         if (shardedJedis == null) {  
  1009.             return result;  
  1010.         }  
  1011.         boolean broken = false;  
  1012.         try {  
  1013.             result = shardedJedis.zrange(key, start, end);  
  1014.         } catch (Exception e) {  
  1015.             log.error(e.getMessage(), e);  
  1016.             broken = true;  
  1017.         } finally {  
  1018.             redisDataSource.returnResource(shardedJedis, broken);  
  1019.         }  
  1020.         return result;  
  1021.     }  
  1022.     /** 
  1023.      * 移除有序集 key 中的一个或多个成员,不存在的成员将被忽略 
  1024.      * @param key 
  1025.      * @param member 
  1026.      * @return 
  1027.      */  
  1028.     public Long zrem(String key, String member) {  
  1029.         Long result = null;  
  1030.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1031.         if (shardedJedis == null) {  
  1032.             return result;  
  1033.         }  
  1034.         boolean broken = false;  
  1035.         try {  
  1036.             result = shardedJedis.zrem(key, member);  
  1037.         } catch (Exception e) {  
  1038.             log.error(e.getMessage(), e);  
  1039.             broken = true;  
  1040.         } finally {  
  1041.             redisDataSource.returnResource(shardedJedis, broken);  
  1042.         }  
  1043.         return result;  
  1044.     }  
  1045.     /** 
  1046.      * 为有序集 key 的成员 member 的 score 值加上增量 member 。 
  1047.      * @param key 
  1048.      * @param score 
  1049.      * @param member 
  1050.      * @return 
  1051.      */  
  1052.     public Double zincrby(String key, double score, String member) {  
  1053.         Double result = null;  
  1054.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1055.         if (shardedJedis == null) {  
  1056.             return result;  
  1057.         }  
  1058.         boolean broken = false;  
  1059.         try {  
  1060.   
  1061.             result = shardedJedis.zincrby(key, score, member);  
  1062.   
  1063.         } catch (Exception e) {  
  1064.             log.error(e.getMessage(), e);  
  1065.             broken = true;  
  1066.         } finally {  
  1067.             redisDataSource.returnResource(shardedJedis, broken);  
  1068.         }  
  1069.         return result;  
  1070.     }  
  1071.     /** 
  1072.      * 回有序集 key 中成员 member 的排名。其中有序集成员按 score 值递增(从小到大)顺序排列 
  1073.      * @param key 
  1074.      * @param member 
  1075.      * @return 
  1076.      */  
  1077.     public Long zrank(String key, String member) {  
  1078.         Long result = null;  
  1079.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1080.         if (shardedJedis == null) {  
  1081.             return result;  
  1082.         }  
  1083.         boolean broken = false;  
  1084.         try {  
  1085.   
  1086.             result = shardedJedis.zrank(key, member);  
  1087.   
  1088.         } catch (Exception e) {  
  1089.             log.error(e.getMessage(), e);  
  1090.             broken = true;  
  1091.         } finally {  
  1092.             redisDataSource.returnResource(shardedJedis, broken);  
  1093.         }  
  1094.         return result;  
  1095.     }  
  1096.     /** 
  1097.      * 返回有序集 key 的基数 
  1098.      * @param key 
  1099.      * @return 
  1100.      */  
  1101.         public Long zcard(String key) {  
  1102.         Long result = null;  
  1103.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1104.         if (shardedJedis == null) {  
  1105.             return result;  
  1106.         }  
  1107.         boolean broken = false;  
  1108.         try {  
  1109.   
  1110.             result = shardedJedis.zcard(key);  
  1111.   
  1112.         } catch (Exception e) {  
  1113.             log.error(e.getMessage(), e);  
  1114.             broken = true;  
  1115.         } finally {  
  1116.             redisDataSource.returnResource(shardedJedis, broken);  
  1117.         }  
  1118.         return result;  
  1119.     }  
  1120.     /** 
  1121.      * 返回有序集 key 中,成员 member 的 score 值。 
  1122.      * @param key 
  1123.      * @param member 
  1124.      * @return 
  1125.      */  
  1126.     public Double zscore(String key, String member) {  
  1127.         Double result = null;  
  1128.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1129.         if (shardedJedis == null) {  
  1130.             return result;  
  1131.         }  
  1132.         boolean broken = false;  
  1133.         try {  
  1134.   
  1135.             result = shardedJedis.zscore(key, member);  
  1136.   
  1137.         } catch (Exception e) {  
  1138.             log.error(e.getMessage(), e);  
  1139.             broken = true;  
  1140.         } finally {  
  1141.             redisDataSource.returnResource(shardedJedis, broken);  
  1142.         }  
  1143.         return result;  
  1144.     }  
  1145.   
  1146.    /** 
  1147.     * 返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量 
  1148.     * @param key 
  1149.     * @param min 
  1150.     * @param max 
  1151.     * @return 
  1152.     */  
  1153.     public Long zcount(String key, double min, double max) {  
  1154.         Long result = null;  
  1155.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1156.         if (shardedJedis == null) {  
  1157.             return result;  
  1158.         }  
  1159.         boolean broken = false;  
  1160.         try {  
  1161.   
  1162.             result = shardedJedis.zcount(key, min, max);  
  1163.   
  1164.         } catch (Exception e) {  
  1165.             log.error(e.getMessage(), e);  
  1166.             broken = true;  
  1167.         } finally {  
  1168.             redisDataSource.returnResource(shardedJedis, broken);  
  1169.         }  
  1170.         return result;  
  1171.     }  
  1172.   
  1173.     /** 
  1174.      * 批量存储 
  1175.      * @param key 
  1176.      * @param value 
  1177.      * @return 
  1178.      */  
  1179.     public String set(byte[] key, byte[] value) {  
  1180.         String result = null;  
  1181.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1182.         if (shardedJedis == null) {  
  1183.             return result;  
  1184.         }  
  1185.         boolean broken = false;  
  1186.         try {  
  1187.   
  1188.             result = shardedJedis.set(key, value);  
  1189.   
  1190.         } catch (Exception e) {  
  1191.             log.error(e.getMessage(), e);  
  1192.             broken = true;  
  1193.         } finally {  
  1194.             redisDataSource.returnResource(shardedJedis, broken);  
  1195.         }  
  1196.         return result;  
  1197.     }  
  1198.     /** 
  1199.      * 获取多个key的值 
  1200.      * @param key 
  1201.      * @return 
  1202.      */  
  1203.     public byte[] get(byte[] key) {  
  1204.         byte[] result = null;  
  1205.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1206.         if (shardedJedis == null) {  
  1207.             return result;  
  1208.         }  
  1209.         boolean broken = false;  
  1210.         try {  
  1211.   
  1212.             result = shardedJedis.get(key);  
  1213.   
  1214.         } catch (Exception e) {  
  1215.             log.error(e.getMessage(), e);  
  1216.             broken = true;  
  1217.         } finally {  
  1218.             redisDataSource.returnResource(shardedJedis, broken);  
  1219.         }  
  1220.         return result;  
  1221.     }  
  1222.     /** 
  1223.      * 判断多个key存在 
  1224.      * @param key 
  1225.      * @return 
  1226.      */  
  1227.     public Boolean exists(byte[] key) {  
  1228.         Boolean result = false;  
  1229.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1230.         if (shardedJedis == null) {  
  1231.             return result;  
  1232.         }  
  1233.         boolean broken = false;  
  1234.         try {  
  1235.   
  1236.             result = shardedJedis.exists(key);  
  1237.   
  1238.         } catch (Exception e) {  
  1239.             log.error(e.getMessage(), e);  
  1240.             broken = true;  
  1241.         } finally {  
  1242.             redisDataSource.returnResource(shardedJedis, broken);  
  1243.         }  
  1244.         return result;  
  1245.     }  
  1246.   
  1247.     public Long expire(byte[] key, int seconds) {  
  1248.         Long result = null;  
  1249.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1250.         if (shardedJedis == null) {  
  1251.             return result;  
  1252.         }  
  1253.         boolean broken = false;  
  1254.         try {  
  1255.   
  1256.             result = shardedJedis.expire(key, seconds);  
  1257.   
  1258.         } catch (Exception e) {  
  1259.             log.error(e.getMessage(), e);  
  1260.             broken = true;  
  1261.         } finally {  
  1262.             redisDataSource.returnResource(shardedJedis, broken);  
  1263.         }  
  1264.         return result;  
  1265.     }  
  1266.   
  1267.     public Long expireAt(byte[] key, long unixTime) {  
  1268.         Long result = null;  
  1269.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1270.         if (shardedJedis == null) {  
  1271.             return result;  
  1272.         }  
  1273.         boolean broken = false;  
  1274.         try {  
  1275.   
  1276.             result = shardedJedis.expireAt(key, unixTime);  
  1277.   
  1278.         } catch (Exception e) {  
  1279.             log.error(e.getMessage(), e);  
  1280.             broken = true;  
  1281.         } finally {  
  1282.             redisDataSource.returnResource(shardedJedis, broken);  
  1283.         }  
  1284.         return result;  
  1285.     }  
  1286.   
  1287.     public Long ttl(byte[] key) {  
  1288.         Long result = null;  
  1289.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1290.         if (shardedJedis == null) {  
  1291.             return result;  
  1292.         }  
  1293.         boolean broken = false;  
  1294.         try {  
  1295.   
  1296.             result = shardedJedis.ttl(key);  
  1297.   
  1298.         } catch (Exception e) {  
  1299.             log.error(e.getMessage(), e);  
  1300.             broken = true;  
  1301.         } finally {  
  1302.             redisDataSource.returnResource(shardedJedis, broken);  
  1303.         }  
  1304.         return result;  
  1305.     }  
  1306.   
  1307.     public Long append(byte[] key, byte[] value) {  
  1308.         Long result = null;  
  1309.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1310.         if (shardedJedis == null) {  
  1311.             return result;  
  1312.         }  
  1313.         boolean broken = false;  
  1314.         try {  
  1315.   
  1316.             result = shardedJedis.append(key, value);  
  1317.   
  1318.         } catch (Exception e) {  
  1319.             log.error(e.getMessage(), e);  
  1320.             broken = true;  
  1321.         } finally {  
  1322.             redisDataSource.returnResource(shardedJedis, broken);  
  1323.         }  
  1324.         return result;  
  1325.     }  
  1326.   
  1327.    /** 
  1328.     * 批量增加到hash 
  1329.     * @param key 
  1330.     * @param field 
  1331.     * @param value 
  1332.     * @return 
  1333.     */  
  1334.     public Long hset(byte[] key, byte[] field, byte[] value) {  
  1335.         Long result = null;  
  1336.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1337.         if (shardedJedis == null) {  
  1338.             return result;  
  1339.         }  
  1340.         boolean broken = false;  
  1341.         try {  
  1342.   
  1343.             result = shardedJedis.hset(key, field, value);  
  1344.   
  1345.         } catch (Exception e) {  
  1346.             log.error(e.getMessage(), e);  
  1347.             broken = true;  
  1348.         } finally {  
  1349.             redisDataSource.returnResource(shardedJedis, broken);  
  1350.         }  
  1351.         return result;  
  1352.     }  
  1353.     /** 
  1354.      * 批量获取field域值 
  1355.      * @param key 
  1356.      * @param field 
  1357.      * @return 
  1358.      */  
  1359.     public byte[] hget(byte[] key, byte[] field) {  
  1360.         byte[] result = null;  
  1361.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1362.         if (shardedJedis == null) {  
  1363.             return result;  
  1364.         }  
  1365.         boolean broken = false;  
  1366.         try {  
  1367.   
  1368.             result = shardedJedis.hget(key, field);  
  1369.   
  1370.         } catch (Exception e) {  
  1371.             log.error(e.getMessage(), e);  
  1372.             broken = true;  
  1373.         } finally {  
  1374.             redisDataSource.returnResource(shardedJedis, broken);  
  1375.         }  
  1376.         return result;  
  1377.     }  
  1378.   
  1379.     public String hmset(byte[] key, Map<byte[], byte[]> hash) {  
  1380.         String result = null;  
  1381.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1382.         if (shardedJedis == null) {  
  1383.             return result;  
  1384.         }  
  1385.         boolean broken = false;  
  1386.         try {  
  1387.   
  1388.             result = shardedJedis.hmset(key, hash);  
  1389.   
  1390.         } catch (Exception e) {  
  1391.   
  1392.             log.error(e.getMessage(), e);  
  1393.             broken = true;  
  1394.         } finally {  
  1395.             redisDataSource.returnResource(shardedJedis, broken);  
  1396.         }  
  1397.         return result;  
  1398.     }  
  1399.   
  1400.     public List<byte[]> hmget(byte[] key, byte[]... fields) {  
  1401.         List<byte[]> result = null;  
  1402.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1403.         if (shardedJedis == null) {  
  1404.             return result;  
  1405.         }  
  1406.         boolean broken = false;  
  1407.         try {  
  1408.   
  1409.             result = shardedJedis.hmget(key, fields);  
  1410.   
  1411.         } catch (Exception e) {  
  1412.   
  1413.             log.error(e.getMessage(), e);  
  1414.             broken = true;  
  1415.         } finally {  
  1416.             redisDataSource.returnResource(shardedJedis, broken);  
  1417.         }  
  1418.         return result;  
  1419.     }  
  1420.   
  1421.       
  1422.     public Boolean hexists(byte[] key, byte[] field) {  
  1423.         Boolean result = false;  
  1424.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1425.         if (shardedJedis == null) {  
  1426.             return result;  
  1427.         }  
  1428.         boolean broken = false;  
  1429.         try {  
  1430.   
  1431.             result = shardedJedis.hexists(key, field);  
  1432.   
  1433.         } catch (Exception e) {  
  1434.   
  1435.             log.error(e.getMessage(), e);  
  1436.             broken = true;  
  1437.         } finally {  
  1438.             redisDataSource.returnResource(shardedJedis, broken);  
  1439.         }  
  1440.         return result;  
  1441.     }  
  1442.     /** 
  1443.      * 批量删除hash的key 
  1444.      * @param key 
  1445.      * @param field 
  1446.      * @return 
  1447.      */  
  1448.     public Long hdel(byte[] key, byte[] field) {  
  1449.         Long result = null;  
  1450.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1451.         if (shardedJedis == null) {  
  1452.             return result;  
  1453.         }  
  1454.         boolean broken = false;  
  1455.         try {  
  1456.   
  1457.             result = shardedJedis.hdel(key, field);  
  1458.   
  1459.         } catch (Exception e) {  
  1460.   
  1461.             log.error(e.getMessage(), e);  
  1462.             broken = true;  
  1463.         } finally {  
  1464.             redisDataSource.returnResource(shardedJedis, broken);  
  1465.         }  
  1466.         return result;  
  1467.     }  
  1468.     public Long rpush(byte[] key, byte[] string) {  
  1469.         Long result = null;  
  1470.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1471.         if (shardedJedis == null) {  
  1472.             return result;  
  1473.         }  
  1474.         boolean broken = false;  
  1475.         try {  
  1476.   
  1477.             result = shardedJedis.rpush(key, string);  
  1478.   
  1479.         } catch (Exception e) {  
  1480.   
  1481.             log.error(e.getMessage(), e);  
  1482.             broken = true;  
  1483.         } finally {  
  1484.             redisDataSource.returnResource(shardedJedis, broken);  
  1485.         }  
  1486.         return result;  
  1487.     }  
  1488.   
  1489.     public Long lpush(byte[] key, byte[] string) {  
  1490.         Long result = null;  
  1491.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1492.         if (shardedJedis == null) {  
  1493.             return result;  
  1494.         }  
  1495.         boolean broken = false;  
  1496.         try {  
  1497.   
  1498.             result = shardedJedis.lpush(key, string);  
  1499.   
  1500.         } catch (Exception e) {  
  1501.   
  1502.             log.error(e.getMessage(), e);  
  1503.             broken = true;  
  1504.         } finally {  
  1505.             redisDataSource.returnResource(shardedJedis, broken);  
  1506.         }  
  1507.         return result;  
  1508.     }  
  1509.   
  1510.     public Long llen(byte[] key) {  
  1511.         Long result = null;  
  1512.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1513.         if (shardedJedis == null) {  
  1514.             return result;  
  1515.         }  
  1516.         boolean broken = false;  
  1517.         try {  
  1518.   
  1519.             result = shardedJedis.llen(key);  
  1520.   
  1521.         } catch (Exception e) {  
  1522.   
  1523.             log.error(e.getMessage(), e);  
  1524.             broken = true;  
  1525.         } finally {  
  1526.             redisDataSource.returnResource(shardedJedis, broken);  
  1527.         }  
  1528.         return result;  
  1529.     }  
  1530.   
  1531.     public List<byte[]> lrange(byte[] key, int start, int end) {  
  1532.         List<byte[]> result = null;  
  1533.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1534.         if (shardedJedis == null) {  
  1535.             return result;  
  1536.         }  
  1537.         boolean broken = false;  
  1538.         try {  
  1539.   
  1540.             result = shardedJedis.lrange(key, start, end);  
  1541.   
  1542.         } catch (Exception e) {  
  1543.   
  1544.             log.error(e.getMessage(), e);  
  1545.             broken = true;  
  1546.         } finally {  
  1547.             redisDataSource.returnResource(shardedJedis, broken);  
  1548.         }  
  1549.         return result;  
  1550.     }  
  1551.   
  1552.      
  1553.     public String lset(byte[] key, int index, byte[] value) {  
  1554.         String result = null;  
  1555.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1556.         if (shardedJedis == null) {  
  1557.             return result;  
  1558.         }  
  1559.         boolean broken = false;  
  1560.         try {  
  1561.   
  1562.             result = shardedJedis.lset(key, index, value);  
  1563.   
  1564.         } catch (Exception e) {  
  1565.   
  1566.             log.error(e.getMessage(), e);  
  1567.             broken = true;  
  1568.         } finally {  
  1569.             redisDataSource.returnResource(shardedJedis, broken);  
  1570.         }  
  1571.         return result;  
  1572.     }  
  1573.     public Long lrem(byte[] key, int count, byte[] value) {  
  1574.         Long result = null;  
  1575.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1576.         if (shardedJedis == null) {  
  1577.             return result;  
  1578.         }  
  1579.         boolean broken = false;  
  1580.         try {  
  1581.   
  1582.             result = shardedJedis.lrem(key, count, value);  
  1583.   
  1584.         } catch (Exception e) {  
  1585.   
  1586.             log.error(e.getMessage(), e);  
  1587.             broken = true;  
  1588.         } finally {  
  1589.             redisDataSource.returnResource(shardedJedis, broken);  
  1590.         }  
  1591.         return result;  
  1592.     }  
  1593.   
  1594.     public byte[] lpop(byte[] key) {  
  1595.         byte[] result = null;  
  1596.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1597.         if (shardedJedis == null) {  
  1598.             return result;  
  1599.         }  
  1600.         boolean broken = false;  
  1601.         try {  
  1602.   
  1603.             result = shardedJedis.lpop(key);  
  1604.   
  1605.         } catch (Exception e) {  
  1606.   
  1607.             log.error(e.getMessage(), e);  
  1608.             broken = true;  
  1609.         } finally {  
  1610.             redisDataSource.returnResource(shardedJedis, broken);  
  1611.         }  
  1612.         return result;  
  1613.     }  
  1614.   
  1615.     public byte[] rpop(byte[] key) {  
  1616.         byte[] result = null;  
  1617.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1618.         if (shardedJedis == null) {  
  1619.             return result;  
  1620.         }  
  1621.         boolean broken = false;  
  1622.         try {  
  1623.   
  1624.             result = shardedJedis.rpop(key);  
  1625.   
  1626.         } catch (Exception e) {  
  1627.   
  1628.             log.error(e.getMessage(), e);  
  1629.             broken = true;  
  1630.         } finally {  
  1631.             redisDataSource.returnResource(shardedJedis, broken);  
  1632.         }  
  1633.         return result;  
  1634.     }  
  1635.   /** 
  1636.    *   批量增加到set 
  1637.    * @param key 
  1638.    * @param member 
  1639.    * @return 
  1640.    */  
  1641.     public Long sadd(byte[] key, byte[] member) {  
  1642.         Long result = null;  
  1643.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1644.         if (shardedJedis == null) {  
  1645.             return result;  
  1646.         }  
  1647.         boolean broken = false;  
  1648.         try {  
  1649.   
  1650.             result = shardedJedis.sadd(key, member);  
  1651.   
  1652.         } catch (Exception e) {  
  1653.   
  1654.             log.error(e.getMessage(), e);  
  1655.             broken = true;  
  1656.         } finally {  
  1657.             redisDataSource.returnResource(shardedJedis, broken);  
  1658.         }  
  1659.         return result;  
  1660.     }  
  1661.   
  1662.     public Set<byte[]> smembers(byte[] key) {  
  1663.         Set<byte[]> result = null;  
  1664.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1665.         if (shardedJedis == null) {  
  1666.             return result;  
  1667.         }  
  1668.         boolean broken = false;  
  1669.         try {  
  1670.   
  1671.             result = shardedJedis.smembers(key);  
  1672.   
  1673.         } catch (Exception e) {  
  1674.   
  1675.             log.error(e.getMessage(), e);  
  1676.             broken = true;  
  1677.         } finally {  
  1678.             redisDataSource.returnResource(shardedJedis, broken);  
  1679.         }  
  1680.         return result;  
  1681.     }  
  1682.   
  1683.     public Long scard(byte[] key) {  
  1684.         Long result = null;  
  1685.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1686.         if (shardedJedis == null) {  
  1687.             return result;  
  1688.         }  
  1689.         boolean broken = false;  
  1690.         try {  
  1691.   
  1692.             result = shardedJedis.scard(key);  
  1693.   
  1694.         } catch (Exception e) {  
  1695.   
  1696.             log.error(e.getMessage(), e);  
  1697.             broken = true;  
  1698.         } finally {  
  1699.             redisDataSource.returnResource(shardedJedis, broken);  
  1700.         }  
  1701.         return result;  
  1702.     }  
  1703.   
  1704.     public Long zadd(byte[] key, double score, byte[] member) {  
  1705.         Long result = null;  
  1706.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1707.         if (shardedJedis == null) {  
  1708.             return result;  
  1709.         }  
  1710.         boolean broken = false;  
  1711.         try {  
  1712.   
  1713.             result = shardedJedis.zadd(key, score, member);  
  1714.   
  1715.         } catch (Exception e) {  
  1716.   
  1717.             log.error(e.getMessage(), e);  
  1718.             broken = true;  
  1719.         } finally {  
  1720.             redisDataSource.returnResource(shardedJedis, broken);  
  1721.         }  
  1722.         return result;  
  1723.     }  
  1724.   
  1725.      
  1726.     public Long zcard(byte[] key) {  
  1727.         Long result = null;  
  1728.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1729.         if (shardedJedis == null) {  
  1730.             return result;  
  1731.         }  
  1732.         boolean broken = false;  
  1733.         try {  
  1734.   
  1735.             result = shardedJedis.zcard(key);  
  1736.   
  1737.         } catch (Exception e) {  
  1738.   
  1739.             log.error(e.getMessage(), e);  
  1740.             broken = true;  
  1741.         } finally {  
  1742.             redisDataSource.returnResource(shardedJedis, broken);  
  1743.         }  
  1744.         return result;  
  1745.     }  
  1746.   
  1747.     public JedisShardInfo getShardInfo(String key) {  
  1748.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1749.         JedisShardInfo result = null;  
  1750.         if (shardedJedis == null) {  
  1751.             return result;  
  1752.         }  
  1753.         boolean broken = false;  
  1754.         try {  
  1755.             result = shardedJedis.getShardInfo(key);  
  1756.         } catch (Exception e) {  
  1757.             log.error(e.getMessage(), e);  
  1758.             broken = true;  
  1759.         } finally {  
  1760.             redisDataSource.returnResource(shardedJedis, broken);  
  1761.         }  
  1762.         return result;  
  1763.     }  
  1764.   
  1765.     public Collection<JedisShardInfo> getAllShardInfo() {  
  1766.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1767.         Collection<JedisShardInfo> result = null;  
  1768.         if (shardedJedis == null) {  
  1769.             return result;  
  1770.         }  
  1771.         boolean broken = false;  
  1772.         try {  
  1773.             result = shardedJedis.getAllShardInfo();  
  1774.   
  1775.         } catch (Exception e) {  
  1776.             log.error(e.getMessage(), e);  
  1777.             broken = true;  
  1778.         } finally {  
  1779.             redisDataSource.returnResource(shardedJedis, broken);  
  1780.         }  
  1781.         return result;  
  1782.     }  
  1783.   
  1784.     public Collection<Jedis> getAllShards() {  
  1785.         ShardedJedis shardedJedis = redisDataSource.getRedisClient();  
  1786.         Collection<Jedis> result = null;  
  1787.         if (shardedJedis == null) {  
  1788.             return result;  
  1789.         }  
  1790.         boolean broken = false;  
  1791.         try {  
  1792.             result = shardedJedis.getAllShards();  
  1793.   
  1794.         } catch (Exception e) {  
  1795.             log.error(e.getMessage(), e);  
  1796.             broken = true;  
  1797.         } finally {  
  1798.             redisDataSource.returnResource(shardedJedis, broken);  
  1799.         }  
  1800.         return result;  
  1801.     }  
  1802.   
  1803. }  

三.配置spring xml文件

[html] view plain copy
print?
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xsi:schemaLocation="  
  5.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
  7.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">  
  8.     
  9.       
  10.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  11.        <property name="maxTotal" value="2048" />    
  12.        <property name="maxIdle" value="200" />    
  13.        <property name="numTestsPerEvictionRun" value="1024"/>    
  14.        <property name="timeBetweenEvictionRunsMillis" value="30000" />    
  15.        <property name="minEvictableIdleTimeMillis" value="-1" />    
  16.        <property name="softMinEvictableIdleTimeMillis" value="10000" />    
  17.        <property name="maxWaitMillis" value="1500"/>    
  18.        <property name="testOnBorrow" value="true" />    
  19.        <property name="testWhileIdle" value="true"/>    
  20.        <property name="testOnReturn" value="false"/>    
  21.        <property name="jmxEnabled" value="true"/>    
  22.        <property name="blockWhenExhausted" value="false"/>   
  23.     </bean>  
  24.   
  25.     <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">  
  26.         <constructor-arg index="0" ref="jedisPoolConfig" />  
  27.         <constructor-arg index="1">  
  28.             <list>  
  29.                 <bean class="redis.clients.jedis.JedisShardInfo">  
  30.                     <constructor-arg name="host" value="127.0.0.1" />  
  31.                     <constructor-arg name="port" value="6379" />  
  32.                     <constructor-arg name="timeout" value="10000" />  
  33.                 </bean>  
  34.             </list>  
  35.         </constructor-arg>  
  36.     </bean>  
  37.     <bean id="redisDataSource" class="redis.dao.RedisDataSourceImpl">  
  38.         <property name="shardedJedisPool" ref="shardedJedisPool"></property>  
  39.     </bean>  
  40.     <bean id="redisClientTemplate" class="redis.dao.RedisClientTemplate">  
  41.         <property name="redisDataSource" ref="redisDataSource"></property>  
  42.     </bean>  
  43. </beans>  

四.代码中的使用,原则就是在执行service时,先通过key到redis里检测是否存在,如果存在则在redis里去数据,如果没有则到数据库去数据,然后存到redis里

[java] view plain copy
print?
  1. public class StudentinfoServiceImpl implements StudentinfoService{  
  2.       
  3.     private StudentinfoDao studentinfoDao;  
  4.       
  5.     private RedisClientTemplate redisClientTemplate;  
  6.       
  7.     private static final ObjectMapper mapper=new ObjectMapper();  
  8.       
  9.     public List<Studentinfo> getStudentinfoByStu(Studentinfo stu) {  
  10.         String key="getStudentinfoByStu:usernumber:"+stu.getUsernumber()+":name:"+stu.getName()+":idnumber:"+stu.getIdnumber();  
  11.         try {  
  12.             boolean flag=redisClientTemplate.exists(key);  
  13.             if(flag){  
  14.                 String stuJsonStr=redisClientTemplate.get(key);  
  15.                 List<Studentinfo> list=mapper.readValue(stuJsonStr, new TypeReference<List<Studentinfo>>() {});  
  16.                 return list;  
  17.             }  
  18.         } catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.         String hql="from Studentinfo where 1=1";  
  22.         if(stu!=null){  
  23.             if(stu.getUsernumber()!=null){  
  24.                 hql+=" and usernumber like '%"+stu.getUsernumber()+"%' ";  
  25.             }  
  26.         }  
  27.         List<Studentinfo> list = studentinfoDao.getStudentinfoByStu(hql);  
  28.         try {  
  29.             String stuJsonStr=mapper.writeValueAsString(list);  
  30.             redisClientTemplate.setex(key, 60, stuJsonStr);  
  31.         } catch (Exception e) {  
  32.             e.printStackTrace();  
  33.         }  
  34.         return list;  
  35.     }  
  36.       
  37.   
  38.     public StudentinfoDao getStudentinfoDao() {  
  39.         return studentinfoDao;  
  40.           
  41.     }  
  42.   
  43.     public void setStudentinfoDao(StudentinfoDao studentinfoDao) {  
  44.         this.studentinfoDao = studentinfoDao;  
  45.     }  
  46.   
  47.     public RedisClientTemplate getRedisClientTemplate() {  
  48.         return redisClientTemplate;  
  49.     }  
  50.   
  51.     public void setRedisClientTemplate(RedisClientTemplate redisClientTemplate) {  
  52.         this.redisClientTemplate = redisClientTemplate;  
  53.     }  
  54.   
  55. }  



4
1
 
 

  相关文章推荐
  • redis在Java web项目的简单应用
  • redis在Java web项目的简单应用
  • webmagic采集CSDN的Java_WebDevelop页面
  • Java Web开发-简单的Java Web项目(含访问本地MySQL)-应用例程
  • eclipse 导入下载或拷贝的java Web项目时报错 ,或者是报错Unbound classpath container: 'JRE System Library
  • JAVA Web项目中用到Redis的代码调试
  • 在java web项目中调用c++程序简单案例
  • 一个简单的Java Web项目
  • java web项目利用Filter进行单点登录的简单实现
  • 在java web项目中调用c++程序简单案例
原创粉丝点击