Redis客户端之Spring整合Jedis

1.下载相关jar包,并引入工程:
jedis-2.4.2.jar
commons-pool2-2.0.jar
2.将以下XML配置引入spring

  1. <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
  2.         <constructor-arg index="0" ref="jedisPoolConfig"/>
  3.         <constructor-arg index="1">
  4.             <list>
  5.                  <bean name="slaver" class="redis.clients.jedis.JedisShardInfo">
  6.                     <constructor-arg index="0" value="${redis.slaver.host}"/>
  7.                     <constructor-arg index="1" value="${redis.slaver.port}" type="int"/>
  8.                 </bean>
  9.                  <bean name="master" class="redis.clients.jedis.JedisShardInfo">
  10.                     <constructor-arg index="0" value="${redis.master.host}"/>
  11.                     <constructor-arg index="1" value="${redis.master.port}" type="int"/>
  12.                 </bean>
  13.             </list>
  14.         </constructor-arg>
  15.     </bean>

  16.     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  17.         <property name="maxTotal" value="2048" />
  18.         <property name="maxIdle" value="200" />
  19.         <property name="numTestsPerEvictionRun" value="1024"/>
  20.         <property name="timeBetweenEvictionRunsMillis" value="30000" />
  21.         <property name="minEvictableIdleTimeMillis" value="-1" />
  22.         <property name="softMinEvictableIdleTimeMillis" value="10000" />
  23.         <property name="maxWaitMillis" value="1500"/>
  24.         <property name="testOnBorrow" value="true" />
  25.         <property name="testWhileIdle" value="true"/>
  26.         <property name="testOnReturn" value="false"/>
  27.         <property name="jmxEnabled" value="true"/>
  28.         <property name="jmxNamePrefix" value="youyuan"/>
  29.         <property name="blockWhenExhausted" value="false"/>
  30.     </bean>
复制代码
3.将
[size=1em]shardedJedisPool注入相关的类中即可使用
  1. @Resource
  2.     private ShardedJedisPool shardedJedisPool;


  3.     /**
  4.      * 设置一个key的过期时间(单位:秒)
  5.      * @param key key值
  6.      * @param seconds 多少秒后过期
  7.      * @return 1:设置了过期时间  0:没有设置过期时间/不能设置过期时间
  8.      */
  9.     public long expire(String key, int seconds) {
  10.         if (key==null || key.equals("")) {
  11.             return 0;
  12.         }

  13.         ShardedJedis shardedJedis = null;
  14.         try {
  15.             shardedJedis = shardedJedisPool.getResource();
  16.             return shardedJedis.expire(key, seconds);
  17.         } catch (Exception ex) {
  18.             logger.error("EXPIRE error[key=" + key + " seconds=" + seconds + "]" + ex.getMessage(), ex);
  19.             returnBrokenResource(shardedJedis);
  20.         } finally {
  21.             returnResource(shardedJedis);
  22.         }
  23.         return 0;
  24.     }

  25.     /**
  26.      * 设置一个key在某个时间点过期
  27.      * @param key key值
  28.      * @param unixTimestamp unix时间戳,从1970-01-01 00:00:00开始到现在的秒数
  29.      * @return 1:设置了过期时间  0:没有设置过期时间/不能设置过期时间
  30.      */
  31.     public long expireAt(String key, int unixTimestamp) {
  32.         if (key==null || key.equals("")) {
  33.             return 0;
  34.         }

  35.         ShardedJedis shardedJedis = null;
  36.         try {
  37.             shardedJedis = shardedJedisPool.getResource();
  38.             return shardedJedis.expireAt(key, unixTimestamp);
  39.         } catch (Exception ex) {
  40.             logger.error("EXPIRE error[key=" + key + " unixTimestamp=" + unixTimestamp + "]" + ex.getMessage(), ex);
  41.             returnBrokenResource(shardedJedis);
  42.         } finally {
  43.             returnResource(shardedJedis);
  44.         }
  45.         return 0;
  46.     }

  47.     /**
  48.      * 截断一个List
  49.      * @param key 列表key
  50.      * @param start 开始位置 从0开始
  51.      * @param end 结束位置
  52.      * @return 状态码
  53.      */
  54.     public String trimList(String key, long start, long end) {
  55.         if (key == null || key.equals("")) {
  56.             return "-";
  57.         }
  58.         ShardedJedis shardedJedis = null;
  59.         try {
  60.             shardedJedis = shardedJedisPool.getResource();
  61.             return shardedJedis.ltrim(key, start, end);
  62.         } catch (Exception ex) {
  63.             logger.error("LTRIM 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage() , ex);
  64.             returnBrokenResource(shardedJedis);
  65.         } finally {
  66.             returnResource(shardedJedis);
  67.         }
  68.         return "-";
  69.     }
  70.     /**
  71.      * 检查Set长度
  72.      * @param key
  73.      * @return
  74.      */
  75.     public long countSet(String key){
  76.             if(key == null ){
  77.                     return 0;
  78.             }
  79.             ShardedJedis shardedJedis = null;
  80.         try {
  81.             shardedJedis = shardedJedisPool.getResource();
  82.             return shardedJedis.scard(key);
  83.         } catch (Exception ex) {
  84.             logger.error("countSet error.", ex);
  85.             returnBrokenResource(shardedJedis);
  86.         } finally {
  87.             returnResource(shardedJedis);
  88.         }
  89.             return 0;
  90.     }
  91.     /**
  92.      * 添加到Set中(同时设置过期时间)
  93.      * @param key key值
  94.      * @param seconds 过期时间 单位s
  95.      * @param value
  96.      * @return
  97.      */
  98.     public boolean addSet(String key,int seconds, String... value) {
  99.             boolean result = addSet(key, value);
  100.             if(result){
  101.                     long i = expire(key, seconds);
  102.                     return i==1;
  103.             }
  104.             return false;
  105.     }
  106.     /**
  107.      * 添加到Set中
  108.      * @param key
  109.      * @param value
  110.      * @return
  111.      */
  112.     public boolean addSet(String key, String... value) {
  113.             if(key == null || value == null){
  114.                     return false;
  115.             }
  116.             ShardedJedis shardedJedis = null;
  117.         try {
  118.             shardedJedis = shardedJedisPool.getResource();
  119.             shardedJedis.sadd(key, value);
  120.             return true;
  121.         } catch (Exception ex) {
  122.             logger.error("setList error.", ex);
  123.             returnBrokenResource(shardedJedis);
  124.         } finally {
  125.             returnResource(shardedJedis);
  126.         }
  127.         return false;
  128.     }

  129.     
  130.     /**
  131.      * @param key
  132.      * @param value
  133.      * @return 判断值是否包含在set中
  134.      */
  135.     public boolean containsInSet(String key, String value) {
  136.             if(key == null || value == null){
  137.                     return false;
  138.             }
  139.             ShardedJedis shardedJedis = null;
  140.         try {
  141.             shardedJedis = shardedJedisPool.getResource();
  142.             return shardedJedis.sismember(key, value);
  143.         } catch (Exception ex) {
  144.             logger.error("setList error.", ex);
  145.             returnBrokenResource(shardedJedis);
  146.         } finally {
  147.             returnResource(shardedJedis);
  148.         }
  149.         return false;
  150.     }
  151.     /**
  152.      * 获取Set
  153.      * @param key
  154.      * @return
  155.      */
  156.     public  Set<String> getSet(String key){
  157.             ShardedJedis shardedJedis = null;
  158.         try {
  159.             shardedJedis = shardedJedisPool.getResource();
  160.             return shardedJedis.smembers(key);
  161.         } catch (Exception ex) {
  162.             logger.error("getList error.", ex);
  163.             returnBrokenResource(shardedJedis);
  164.         } finally {
  165.             returnResource(shardedJedis);
  166.         }
  167.         return null;
  168.     }

  169.     /**
  170.      * 从set中删除value
  171.      * @param key
  172.      * @return
  173.      */
  174.     public  boolean removeSetValue(String key,String... value){
  175.             ShardedJedis shardedJedis = null;
  176.         try {
  177.             shardedJedis = shardedJedisPool.getResource();
  178.             shardedJedis.srem(key, value);
  179.             return true;
  180.         } catch (Exception ex) {
  181.             logger.error("getList error.", ex);
  182.             returnBrokenResource(shardedJedis);
  183.         } finally {
  184.             returnResource(shardedJedis);
  185.         }
  186.         return false;
  187.     }
  188.     
  189.     
  190.     
  191.     /**
  192.      * 从list中删除value 默认count 1
  193.      * @param key
  194.      * @param values 值list
  195.      * @return
  196.      */
  197.     public  int removeListValue(String key,List<String> values){
  198.             return removeListValue(key, 1, values);
  199.     }
  200.     /**
  201.      * 从list中删除value
  202.      * @param key
  203.      * @param count 
  204.      * @param values 值list
  205.      * @return
  206.      */
  207.     public  int removeListValue(String key,long count,List<String> values){
  208.             int result = 0;
  209.             if(values != null && values.size()>0){
  210.                     for(String value : values){
  211.                             if(removeListValue(key, count, value)){
  212.                                     result++;
  213.                             }
  214.                     }
  215.             }
  216.             return result;
  217.     }
  218.     /**
  219.      *  从list中删除value
  220.      * @param key
  221.      * @param count 要删除个数
  222.      * @param value
  223.      * @return
  224.      */
  225.     public  boolean removeListValue(String key,long count,String value){
  226.             ShardedJedis shardedJedis = null;
  227.         try {
  228.             shardedJedis = shardedJedisPool.getResource();
  229.             shardedJedis.lrem(key, count, value);
  230.             return true;
  231.         } catch (Exception ex) {
  232.             logger.error("getList error.", ex);
  233.             returnBrokenResource(shardedJedis);
  234.         } finally {
  235.             returnResource(shardedJedis);
  236.         }
  237.             return false;
  238.     }
  239.     
  240.     /**
  241.      * 截取List
  242.      * @param key 
  243.      * @param start 起始位置
  244.      * @param end 结束位置
  245.      * @return
  246.      */
  247.     public List<String> rangeList(String key, long start, long end) {
  248.         if (key == null || key.equals("")) {
  249.             return null;
  250.         }
  251.         ShardedJedis shardedJedis = null;
  252.         try {
  253.             shardedJedis = shardedJedisPool.getResource();
  254.             return shardedJedis.lrange(key, start, end);
  255.         } catch (Exception ex) {
  256.             logger.error("rangeList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage() , ex);
  257.             returnBrokenResource(shardedJedis);
  258.         } finally {
  259.             returnResource(shardedJedis);
  260.         }
  261.         return null;
  262.     }
  263.     
  264.     /**
  265.      * 检查List长度
  266.      * @param key
  267.      * @return
  268.      */
  269.     public long countList(String key){
  270.             if(key == null ){
  271.                     return 0;
  272.             }
  273.             ShardedJedis shardedJedis = null;
  274.         try {
  275.             shardedJedis = shardedJedisPool.getResource();
  276.             return shardedJedis.llen(key);
  277.         } catch (Exception ex) {
  278.             logger.error("countList error.", ex);
  279.             returnBrokenResource(shardedJedis);
  280.         } finally {
  281.             returnResource(shardedJedis);
  282.         }
  283.             return 0;
  284.     }
  285.     
  286.     /**
  287.      * 添加到List中(同时设置过期时间)
  288.      * @param key key值
  289.      * @param seconds 过期时间 单位s
  290.      * @param value 
  291.      * @return 
  292.      */
  293.     public boolean addList(String key,int seconds, String... value){
  294.             boolean result = addList(key, value);
  295.             if(result){
  296.                     long i = expire(key, seconds);
  297.                     return i==1;
  298.             }
  299.             return false;
  300.     }
  301.     /**
  302.      * 添加到List
  303.      * @param key
  304.      * @param value
  305.      * @return
  306.      */
  307.     public boolean addList(String key, String... value) {
  308.             if(key == null || value == null){
  309.                     return false;
  310.             }
  311.             ShardedJedis shardedJedis = null;
  312.         try {
  313.             shardedJedis = shardedJedisPool.getResource();
  314.             shardedJedis.lpush(key, value);
  315.             return true;
  316.         } catch (Exception ex) {
  317.             logger.error("setList error.", ex);
  318.             returnBrokenResource(shardedJedis);
  319.         } finally {
  320.             returnResource(shardedJedis);
  321.         }
  322.         return false;
  323.     }
  324.     /**
  325.      * 添加到List(只新增)
  326.      * @param key
  327.      * @param value
  328.      * @return
  329.      */
  330.     public boolean addList(String key, List<String> list) {
  331.             if(key == null || list == null || list.size() == 0){
  332.                     return false;
  333.             }
  334.             for(String value : list){
  335.                     addList(key, value);
  336.             }
  337.         return true;
  338.     }
  339.     
  340.     /**
  341.      * 获取List
  342.      * @param key
  343.      * @return
  344.      */
  345.     public  List<String> getList(String key){
  346.             ShardedJedis shardedJedis = null;
  347.         try {
  348.             shardedJedis = shardedJedisPool.getResource();
  349.             return shardedJedis.lrange(key, 0, -1);
  350.         } catch (Exception ex) {
  351.             logger.error("getList error.", ex);
  352.             returnBrokenResource(shardedJedis);
  353.         } finally {
  354.             returnResource(shardedJedis);
  355.         }
  356.         return null;
  357.     }
  358.     /**
  359.      * 设置HashSet对象
  360.      *
  361.      * @param domain 域名
  362.      * @param key    键值
  363.      * @param value  Json String or String value
  364.      * @return
  365.      */
  366.     public boolean setHSet(String domain, String key, String value) {
  367.         if (value == null) return false;
  368.         ShardedJedis shardedJedis = null;
  369.         try {
  370.             shardedJedis = shardedJedisPool.getResource();
  371.             shardedJedis.hset(domain, key, value);
  372.             return true;
  373.         } catch (Exception ex) {
  374.             logger.error("setHSet error.", ex);
  375.             returnBrokenResource(shardedJedis);
  376.         } finally {
  377.             returnResource(shardedJedis);
  378.         }
  379.         return false;
  380.     }

  381.     /**
  382.      * 获得HashSet对象
  383.      *
  384.      * @param domain 域名
  385.      * @param key    键值
  386.      * @return Json String or String value
  387.      */
  388.     public String getHSet(String domain, String key) {
  389.         ShardedJedis shardedJedis = null;
  390.         try {
  391.             shardedJedis = shardedJedisPool.getResource();
  392.             return shardedJedis.hget(domain, key);
  393.         } catch (Exception ex) {
  394.             logger.error("getHSet error.", ex);
  395.             returnBrokenResource(shardedJedis);
  396.         } finally {
  397.             returnResource(shardedJedis);
  398.         }
  399.         return null;
  400.     }

  401.     /**
  402.      * 删除HashSet对象
  403.      *
  404.      * @param domain 域名
  405.      * @param key    键值
  406.      * @return 删除的记录数
  407.      */
  408.     public long delHSet(String domain, String key) {
  409.         ShardedJedis shardedJedis = null;
  410.         long count = 0;
  411.         try {
  412.             shardedJedis = shardedJedisPool.getResource();
  413.             count = shardedJedis.hdel(domain, key);
  414.         } catch (Exception ex) {
  415.             logger.error("delHSet error.", ex);
  416.             returnBrokenResource(shardedJedis);
  417.         } finally {
  418.             returnResource(shardedJedis);
  419.         }
  420.         return count;
  421.     }

  422.     /**
  423.      * 删除HashSet对象
  424.      *
  425.      * @param domain 域名
  426.      * @param key    键值
  427.      * @return 删除的记录数
  428.      */
  429.     public long delHSet(String domain, String... key) {
  430.         ShardedJedis shardedJedis = null;
  431.         long count = 0;
  432.         try {
  433.             shardedJedis = shardedJedisPool.getResource();
  434.             count = shardedJedis.hdel(domain, key);
  435.         } catch (Exception ex) {
  436.             logger.error("delHSet error.", ex);
  437.             returnBrokenResource(shardedJedis);
  438.         } finally {
  439.             returnResource(shardedJedis);
  440.         }
  441.         return count;
  442.     }

  443.     /**
  444.      * 判断key是否存在
  445.      *
  446.      * @param domain 域名
  447.      * @param key    键值
  448.      * @return
  449.      */
  450.     public boolean existsHSet(String domain, String key) {
  451.         ShardedJedis shardedJedis = null;
  452.         boolean isExist = false;
  453.         try {
  454.             shardedJedis = shardedJedisPool.getResource();
  455.             isExist = shardedJedis.hexists(domain, key);
  456.         } catch (Exception ex) {
  457.             logger.error("existsHSet error.", ex);
  458.             returnBrokenResource(shardedJedis);
  459.         } finally {
  460.             returnResource(shardedJedis);
  461.         }
  462.         return isExist;
  463.     }

  464.     /**
  465.      * 全局扫描hset
  466.      *
  467.      * @param match field匹配模式
  468.      * @return
  469.      */
  470.     public List<Map.Entry<String, String>> scanHSet(String domain, String match) {
  471.         ShardedJedis shardedJedis = null;
  472.         try {
  473.             int cursor = 0;
  474.             shardedJedis = shardedJedisPool.getResource();
  475.             ScanParams scanParams = new ScanParams();
  476.             scanParams.match(match);
  477.             Jedis jedis = shardedJedis.getShard(domain);
  478.             ScanResult<Map.Entry<String, String>> scanResult;
  479.             List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();
  480.             do {
  481.                 scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams);
  482.                 list.addAll(scanResult.getResult());
  483.                 cursor = Integer.parseInt(scanResult.getStringCursor());
  484.             } while (cursor > 0);
  485.             return list;
  486.         } catch (Exception ex) {
  487.             logger.error("scanHSet error.", ex);
  488.             returnBrokenResource(shardedJedis);
  489.         } finally {
  490.             returnResource(shardedJedis);
  491.         }
  492.         return null;
  493.     }


  494.     /**
  495.      * 返回 domain 指定的哈希集中所有字段的value值
  496.      *
  497.      * @param domain
  498.      * @return
  499.      */

  500.     public List<String> hvals(String domain) {
  501.         ShardedJedis shardedJedis = null;
  502.         List<String> retList = null;
  503.         try {
  504.             shardedJedis = shardedJedisPool.getResource();
  505.             retList = shardedJedis.hvals(domain);
  506.         } catch (Exception ex) {
  507.             logger.error("hvals error.", ex);
  508.             returnBrokenResource(shardedJedis);
  509.         } finally {
  510.             returnResource(shardedJedis);
  511.         }
  512.         return retList;
  513.     }

  514.     /**
  515.      * 返回 domain 指定的哈希集中所有字段的key值
  516.      *
  517.      * @param domain
  518.      * @return
  519.      */

  520.     public Set<String> hkeys(String domain) {
  521.         ShardedJedis shardedJedis = null;
  522.         Set<String> retList = null;
  523.         try {
  524.             shardedJedis = shardedJedisPool.getResource();
  525.             retList = shardedJedis.hkeys(domain);
  526.         } catch (Exception ex) {
  527.             logger.error("hkeys error.", ex);
  528.             returnBrokenResource(shardedJedis);
  529.         } finally {
  530.             returnResource(shardedJedis);
  531.         }
  532.         return retList;
  533.     }

  534.     /**
  535.      * 返回 domain 指定的哈希key值总数
  536.      *
  537.      * @param domain
  538.      * @return
  539.      */
  540.     public long lenHset(String domain) {
  541.         ShardedJedis shardedJedis = null;
  542.         long retList = 0;
  543.         try {
  544.             shardedJedis = shardedJedisPool.getResource();
  545.             retList = shardedJedis.hlen(domain);
  546.         } catch (Exception ex) {
  547.             logger.error("hkeys error.", ex);
  548.             returnBrokenResource(shardedJedis);
  549.         } finally {
  550.             returnResource(shardedJedis);
  551.         }
  552.         return retList;
  553.     }

  554.     /**
  555.      * 设置排序集合
  556.      *
  557.      * @param key
  558.      * @param score
  559.      * @param value
  560.      * @return
  561.      */
  562.     public boolean setSortedSet(String key, long score, String value) {
  563.         ShardedJedis shardedJedis = null;
  564.         try {
  565.             shardedJedis = shardedJedisPool.getResource();
  566.             shardedJedis.zadd(key, score, value);
  567.             return true;
  568.         } catch (Exception ex) {
  569.             logger.error("setSortedSet error.", ex);
  570.             returnBrokenResource(shardedJedis);
  571.         } finally {
  572.             returnResource(shardedJedis);
  573.         }
  574.         return false;
  575.     }

  576.     /**
  577.      * 获得排序集合
  578.      *
  579.      * @param key
  580.      * @param startScore
  581.      * @param endScore
  582.      * @param orderByDesc
  583.      * @return
  584.      */
  585.     public Set<String> getSoredSet(String key, long startScore, long endScore, boolean orderByDesc) {
  586.         ShardedJedis shardedJedis = null;
  587.         try {
  588.             shardedJedis = shardedJedisPool.getResource();
  589.             if (orderByDesc) {
  590.                 return shardedJedis.zrevrangeByScore(key, endScore, startScore);
  591.             } else {
  592.                 return shardedJedis.zrangeByScore(key, startScore, endScore);
  593.             }
  594.         } catch (Exception ex) {
  595.             logger.error("getSoredSet error.", ex);
  596.             returnBrokenResource(shardedJedis);
  597.         } finally {
  598.             returnResource(shardedJedis);
  599.         }
  600.         return null;
  601.     }

  602.     /**
  603.      * 计算排序长度
  604.      *
  605.      * @param key
  606.      * @param startScore
  607.      * @param endScore
  608.      * @return
  609.      */
  610.     public long countSoredSet(String key, long startScore, long endScore) {
  611.         ShardedJedis shardedJedis = null;
  612.         try {
  613.             shardedJedis = shardedJedisPool.getResource();
  614.             Long count = shardedJedis.zcount(key, startScore, endScore);
  615.             return count == null ? 0L : count;
  616.         } catch (Exception ex) {
  617.             logger.error("countSoredSet error.", ex);
  618.             returnBrokenResource(shardedJedis);
  619.         } finally {
  620.             returnResource(shardedJedis);
  621.         }
  622.         return 0L;
  623.     }

  624.     /**
  625.      * 删除排序集合
  626.      *
  627.      * @param key
  628.      * @param value
  629.      * @return
  630.      */
  631.     public boolean delSortedSet(String key, String value) {
  632.         ShardedJedis shardedJedis = null;
  633.         try {
  634.             shardedJedis = shardedJedisPool.getResource();
  635.             long count = shardedJedis.zrem(key, value);
  636.             return count > 0;
  637.         } catch (Exception ex) {
  638.             logger.error("delSortedSet error.", ex);
  639.             returnBrokenResource(shardedJedis);
  640.         } finally {
  641.             returnResource(shardedJedis);
  642.         }
  643.         return false;
  644.     }

  645.     /**
  646.      * 获得排序集合
  647.      *
  648.      * @param key
  649.      * @param startRange
  650.      * @param endRange
  651.      * @param orderByDesc
  652.      * @return
  653.      */
  654.     public Set<String> getSoredSetByRange(String key, int startRange, int endRange, boolean orderByDesc) {
  655.         ShardedJedis shardedJedis = null;
  656.         try {
  657.             shardedJedis = shardedJedisPool.getResource();
  658.             if (orderByDesc) {
  659.                 return shardedJedis.zrevrange(key, startRange, endRange);
  660.             } else {
  661.                 return shardedJedis.zrange(key, startRange, endRange);
  662.             }
  663.         } catch (Exception ex) {
  664.             logger.error("getSoredSetByRange error.", ex);
  665.             returnBrokenResource(shardedJedis);
  666.         } finally {
  667.             returnResource(shardedJedis);
  668.         }
  669.         return null;
  670.     }

  671.     /**
  672.      * 获得排序打分
  673.      *
  674.      * @param key
  675.      * @return
  676.      */
  677.     public Double getScore(String key, String member) {
  678.         ShardedJedis shardedJedis = null;
  679.         try {
  680.             shardedJedis = shardedJedisPool.getResource();
  681.             return shardedJedis.zscore(key, member);
  682.         } catch (Exception ex) {
  683.             logger.error("getSoredSet error.", ex);
  684.             returnBrokenResource(shardedJedis);
  685.         } finally {
  686.             returnResource(shardedJedis);
  687.         }
  688.         return null;
  689.     }

  690.     public boolean set(String key, String value, int second) {
  691.         ShardedJedis shardedJedis = null;
  692.         try {
  693.             shardedJedis = shardedJedisPool.getResource();
  694.             shardedJedis.setex(key, second, value);
  695.             return true;
  696.         } catch (Exception ex) {
  697.             logger.error("set error.", ex);
  698.             returnBrokenResource(shardedJedis);
  699.         } finally {
  700.             returnResource(shardedJedis);
  701.         }
  702.         return false;
  703.     }

  704.     public boolean set(String key, String value) {
  705.         ShardedJedis shardedJedis = null;
  706.         try {
  707.             shardedJedis = shardedJedisPool.getResource();
  708.             shardedJedis.set(key, value);
  709.             return true;
  710.         } catch (Exception ex) {
  711.             logger.error("set error.", ex);
  712.             returnBrokenResource(shardedJedis);
  713.         } finally {
  714.             returnResource(shardedJedis);
  715.         }
  716.         return false;
  717.     }

  718.     public String get(String key, String defaultValue) {
  719.         ShardedJedis shardedJedis = null;
  720.         try {
  721.             shardedJedis = shardedJedisPool.getResource();
  722.             return shardedJedis.get(key) == null?defaultValue:shardedJedis.get(key);
  723.         } catch (Exception ex) {
  724.             logger.error("get error.", ex);
  725.             returnBrokenResource(shardedJedis);
  726.         } finally {
  727.             returnResource(shardedJedis);
  728.         }
  729.         return defaultValue;
  730.     }

  731.     public boolean del(String key) {
  732.         ShardedJedis shardedJedis = null;
  733.         try {
  734.             shardedJedis = shardedJedisPool.getResource();
  735.             shardedJedis.del(key);
  736.             return true;
  737.         } catch (Exception ex) {
  738.             logger.error("del error.", ex);
  739.             returnBrokenResource(shardedJedis);
  740.         } finally {
  741.             returnResource(shardedJedis);
  742.         }
  743.         return false;
  744.     }

  745.     public long incr(String key) {
  746.         ShardedJedis shardedJedis = null;
  747.         try {
  748.             shardedJedis = shardedJedisPool.getResource();
  749.             return shardedJedis.incr(key);
  750.         } catch (Exception ex) {
  751.             logger.error("incr error.", ex);
  752.             returnBrokenResource(shardedJedis);
  753.         } finally {
  754.             returnResource(shardedJedis);
  755.         }
  756.         return 0;
  757.     }

  758.     public long decr(String key) {
  759.         ShardedJedis shardedJedis = null;
  760.         try {
  761.             shardedJedis = shardedJedisPool.getResource();
  762.             return shardedJedis.decr(key);
  763.         } catch (Exception ex) {
  764.             logger.error("incr error.", ex);
  765.             returnBrokenResource(shardedJedis);
  766.         } finally {
  767.             returnResource(shardedJedis);
  768.         }
  769.         return 0;
  770.     }



  771.     private void returnBrokenResource(ShardedJedis shardedJedis) {
  772.         try {
  773.             shardedJedisPool.returnBrokenResource(shardedJedis);
  774.         } catch (Exception e) {
  775.             logger.error("returnBrokenResource error.", e);
  776.         }
  777.     }

  778.     private void returnResource(ShardedJedis shardedJedis) {
  779.         try {
  780.             shardedJedisPool.returnResource(shardedJedis);
  781.         } catch (Exception e) {
  782.             logger.error("returnResource error.", e);
  783.         }
  784.     }
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值