星空网 > 软件开发 > Java

Redis客户端之Spring整合Jedis

 

 

1.下载相关jar包,并引入工程:

jedis-2.4.2.jar

commons-pool2-2.0.jar

2.将以下

  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.   
  17.    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  18.        <property name="maxTotal" value="2048" />  
  19.        <property name="maxIdle" value="200" />  
  20.        <property name="numTestsPerEvictionRun" value="1024"/>  
  21.        <property name="timeBetweenEvictionRunsMillis" value="30000" />  
  22.        <property name="minEvictableIdleTimeMillis" value="-1" />  
  23.        <property name="softMinEvictableIdleTimeMillis" value="10000" />  
  24.        <property name="maxWaitMillis" value="1500"/>  
  25.        <property name="testOnBorrow" value="true" />  
  26.        <property name="testWhileIdle" value="true"/>  
  27.        <property name="testOnReturn" value="false"/>  
  28.        <property name="jmxEnabled" value="true"/>  
  29.        <property name="jmxNamePrefix" value="youyuan"/>  
  30.        <property name="blockWhenExhausted" value="false"/>  
  31.    </bean>  

3.将shardedJedisPool注入相关的类中即可使用

 

 

 

 

Java代码  Redis客户端之Spring整合Jedis

  1. @Resource  
  2.     private ShardedJedisPool shardedJedisPool;  
  3.   
  4.   
  5.     /** 
  6.      * 设置一个key的过期时间(单位:秒) 
  7.      * @param key key值 
  8.      * @param seconds 多少秒后过期 
  9.      * @return 1:设置了过期时间  0:没有设置过期时间/不能设置过期时间 
  10.      */  
  11.     public long expire(String key, int seconds) {  
  12.         if (key==null || key.equals("")) {  
  13.             return 0;  
  14.         }  
  15.   
  16.         ShardedJedis shardedJedis = null;  
  17.         try {  
  18.             shardedJedis = shardedJedisPool.getResource();  
  19.             return shardedJedis.expire(key, seconds);  
  20.         } catch (Exception ex) {  
  21.             logger.error("EXPIRE error[key=" + key + " seconds=" + seconds + "]" + ex.getMessage(), ex);  
  22.             returnBrokenResource(shardedJedis);  
  23.         } finally {  
  24.             returnResource(shardedJedis);  
  25.         }  
  26.         return 0;  
  27.     }  
  28.   
  29.     /** 
  30.      * 设置一个key在某个时间点过期 
  31.      * @param key key值 
  32.      * @param unixTimestamp unix时间戳,从1970-01-01 00:00:00开始到现在的秒数 
  33.      * @return 1:设置了过期时间  0:没有设置过期时间/不能设置过期时间 
  34.      */  
  35.     public long expireAt(String key, int unixTimestamp) {  
  36.         if (key==null || key.equals("")) {  
  37.             return 0;  
  38.         }  
  39.   
  40.         ShardedJedis shardedJedis = null;  
  41.         try {  
  42.             shardedJedis = shardedJedisPool.getResource();  
  43.             return shardedJedis.expireAt(key, unixTimestamp);  
  44.         } catch (Exception ex) {  
  45.             logger.error("EXPIRE error[key=" + key + " unixTimestamp=" + unixTimestamp + "]" + ex.getMessage(), ex);  
  46.             returnBrokenResource(shardedJedis);  
  47.         } finally {  
  48.             returnResource(shardedJedis);  
  49.         }  
  50.         return 0;  
  51.     }  
  52.   
  53.     /** 
  54.      * 截断一个List 
  55.      * @param key 列表key 
  56.      * @param start 开始位置 从0开始 
  57.      * @param end 结束位置 
  58.      * @return 状态码 
  59.      */  
  60.     public String trimList(String key, long start, long end) {  
  61.         if (key == null || key.equals("")) {  
  62.             return "-";  
  63.         }  
  64.         ShardedJedis shardedJedis = null;  
  65.         try {  
  66.             shardedJedis = shardedJedisPool.getResource();  
  67.             return shardedJedis.ltrim(key, start, end);  
  68.         } catch (Exception ex) {  
  69.             logger.error("LTRIM 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage() , ex);  
  70.             returnBrokenResource(shardedJedis);  
  71.         } finally {  
  72.             returnResource(shardedJedis);  
  73.         }  
  74.         return "-";  
  75.     }  
  76.     /** 
  77.      * 检查Set长度 
  78.      * @param key 
  79.      * @return 
  80.      */  
  81.     public long countSet(String key){  
  82.         if(key == null ){  
  83.             return 0;  
  84.         }  
  85.         ShardedJedis shardedJedis = null;  
  86.         try {  
  87.             shardedJedis = shardedJedisPool.getResource();  
  88.             return shardedJedis.scard(key);  
  89.         } catch (Exception ex) {  
  90.             logger.error("countSet error.", ex);  
  91.             returnBrokenResource(shardedJedis);  
  92.         } finally {  
  93.             returnResource(shardedJedis);  
  94.         }  
  95.         return 0;  
  96.     }  
  97.     /** 
  98.      * 添加到Set中(同时设置过期时间) 
  99.      * @param key key值 
  100.      * @param seconds 过期时间 单位s 
  101.      * @param value 
  102.      * @return 
  103.      */  
  104.     public boolean addSet(String key,int seconds, String... value) {  
  105.         boolean result = addSet(key, value);  
  106.         if(result){  
  107.             long i = expire(key, seconds);  
  108.             return i==1;  
  109.         }  
  110.         return false;  
  111.     }  
  112.     /** 
  113.      * 添加到Set中 
  114.      * @param key 
  115.      * @param value 
  116.      * @return 
  117.      */  
  118.     public boolean addSet(String key, String... value) {  
  119.         if(key == null || value == null){  
  120.             return false;  
  121.         }  
  122.         ShardedJedis shardedJedis = null;  
  123.         try {  
  124.             shardedJedis = shardedJedisPool.getResource();  
  125.             shardedJedis.sadd(key, value);  
  126.             return true;  
  127.         } catch (Exception ex) {  
  128.             logger.error("setList error.", ex);  
  129.             returnBrokenResource(shardedJedis);  
  130.         } finally {  
  131.             returnResource(shardedJedis);  
  132.         }  
  133.         return false;  
  134.     }  
  135.   
  136.       
  137.     /** 
  138.      * @param key 
  139.      * @param value 
  140.      * @return 判断值是否包含在set中 
  141.      */  
  142.     public boolean containsInSet(String key, String value) {  
  143.         if(key == null || value == null){  
  144.             return false;  
  145.         }  
  146.         ShardedJedis shardedJedis = null;  
  147.         try {  
  148.             shardedJedis = shardedJedisPool.getResource();  
  149.             return shardedJedis.sismember(key, value);  
  150.         } catch (Exception ex) {  
  151.             logger.error("setList error.", ex);  
  152.             returnBrokenResource(shardedJedis);  
  153.         } finally {  
  154.             returnResource(shardedJedis);  
  155.         }  
  156.         return false;  
  157.     }  
  158.     /** 
  159.      * 获取Set 
  160.      * @param key 
  161.      * @return 
  162.      */  
  163.     public  Set<String> getSet(String key){  
  164.         ShardedJedis shardedJedis = null;  
  165.         try {  
  166.             shardedJedis = shardedJedisPool.getResource();  
  167.             return shardedJedis.smembers(key);  
  168.         } catch (Exception ex) {  
  169.             logger.error("getList error.", ex);  
  170.             returnBrokenResource(shardedJedis);  
  171.         } finally {  
  172.             returnResource(shardedJedis);  
  173.         }  
  174.         return null;  
  175.     }  
  176.   
  177.     /** 
  178.      * 从set中删除value 
  179.      * @param key 
  180.      * @return 
  181.      */  
  182.     public  boolean removeSetValue(String key,String... value){  
  183.         ShardedJedis shardedJedis = null;  
  184.         try {  
  185.             shardedJedis = shardedJedisPool.getResource();  
  186.             shardedJedis.srem(key, value);  
  187.             return true;  
  188.         } catch (Exception ex) {  
  189.             logger.error("getList error.", ex);  
  190.             returnBrokenResource(shardedJedis);  
  191.         } finally {  
  192.             returnResource(shardedJedis);  
  193.         }  
  194.         return false;  
  195.     }  
  196.       
  197.       
  198.       
  199.     /** 
  200.      * 从list中删除value 默认count 1 
  201.      * @param key 
  202.      * @param values 值list 
  203.      * @return 
  204.      */  
  205.     public  int removeListValue(String key,List<String> values){  
  206.         return removeListValue(key, 1, values);  
  207.     }  
  208.     /** 
  209.      * 从list中删除value 
  210.      * @param key 
  211.      * @param count  
  212.      * @param values 值list 
  213.      * @return 
  214.      */  
  215.     public  int removeListValue(String key,long count,List<String> values){  
  216.         int result = 0;  
  217.         if(values != null && values.size()>0){  
  218.             for(String value : values){  
  219.                 if(removeListValue(key, count, value)){  
  220.                     result++;  
  221.                 }  
  222.             }  
  223.         }  
  224.         return result;  
  225.     }  
  226.     /** 
  227.      *  从list中删除value 
  228.      * @param key 
  229.      * @param count 要删除个数 
  230.      * @param value 
  231.      * @return 
  232.      */  
  233.     public  boolean removeListValue(String key,long count,String value){  
  234.         ShardedJedis shardedJedis = null;  
  235.         try {  
  236.             shardedJedis = shardedJedisPool.getResource();  
  237.             shardedJedis.lrem(key, count, value);  
  238.             return true;  
  239.         } catch (Exception ex) {  
  240.             logger.error("getList error.", ex);  
  241.             returnBrokenResource(shardedJedis);  
  242.         } finally {  
  243.             returnResource(shardedJedis);  
  244.         }  
  245.         return false;  
  246.     }  
  247.       
  248.     /** 
  249.      * 截取List 
  250.      * @param key  
  251.      * @param start 起始位置 
  252.      * @param end 结束位置 
  253.      * @return 
  254.      */  
  255.     public List<String> rangeList(String key, long start, long end) {  
  256.         if (key == null || key.equals("")) {  
  257.             return null;  
  258.         }  
  259.         ShardedJedis shardedJedis = null;  
  260.         try {  
  261.             shardedJedis = shardedJedisPool.getResource();  
  262.             return shardedJedis.lrange(key, start, end);  
  263.         } catch (Exception ex) {  
  264.             logger.error("rangeList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage() , ex);  
  265.             returnBrokenResource(shardedJedis);  
  266.         } finally {  
  267.             returnResource(shardedJedis);  
  268.         }  
  269.         return null;  
  270.     }  
  271.       
  272.     /** 
  273.      * 检查List长度 
  274.      * @param key 
  275.      * @return 
  276.      */  
  277.     public long countList(String key){  
  278.         if(key == null ){  
  279.             return 0;  
  280.         }  
  281.         ShardedJedis shardedJedis = null;  
  282.         try {  
  283.             shardedJedis = shardedJedisPool.getResource();  
  284.             return shardedJedis.llen(key);  
  285.         } catch (Exception ex) {  
  286.             logger.error("countList error.", ex);  
  287.             returnBrokenResource(shardedJedis);  
  288.         } finally {  
  289.             returnResource(shardedJedis);  
  290.         }  
  291.         return 0;  
  292.     }  
  293.       
  294.     /** 
  295.      * 添加到List中(同时设置过期时间) 
  296.      * @param key key值 
  297.      * @param seconds 过期时间 单位s 
  298.      * @param value  
  299.      * @return  
  300.      */  
  301.     public boolean addList(String key,int seconds, String... value){  
  302.         boolean result = addList(key, value);  
  303.         if(result){  
  304.             long i = expire(key, seconds);  
  305.             return i==1;  
  306.         }  
  307.         return false;  
  308.     }  
  309.     /** 
  310.      * 添加到List 
  311.      * @param key 
  312.      * @param value 
  313.      * @return 
  314.      */  
  315.     public boolean addList(String key, String... value) {  
  316.         if(key == null || value == null){  
  317.             return false;  
  318.         }  
  319.         ShardedJedis shardedJedis = null;  
  320.         try {  
  321.             shardedJedis = shardedJedisPool.getResource();  
  322.             shardedJedis.lpush(key, value);  
  323.             return true;  
  324.         } catch (Exception ex) {  
  325.             logger.error("setList error.", ex);  
  326.             returnBrokenResource(shardedJedis);  
  327.         } finally {  
  328.             returnResource(shardedJedis);  
  329.         }  
  330.         return false;  
  331.     }  
  332.     /** 
  333.      * 添加到List(只新增) 
  334.      * @param key 
  335.      * @param value 
  336.      * @return 
  337.      */  
  338.     public boolean addList(String key, List<String> list) {  
  339.         if(key == null || list == null || list.size() == 0){  
  340.             return false;  
  341.         }  
  342.         for(String value : list){  
  343.             addList(key, value);  
  344.         }  
  345.         return true;  
  346.     }  
  347.       
  348.     /** 
  349.      * 获取List 
  350.      * @param key 
  351.      * @return 
  352.      */  
  353.     public  List<String> getList(String key){  
  354.         ShardedJedis shardedJedis = null;  
  355.         try {  
  356.             shardedJedis = shardedJedisPool.getResource();  
  357.             return shardedJedis.lrange(key, 0, -1);  
  358.         } catch (Exception ex) {  
  359.             logger.error("getList error.", ex);  
  360.             returnBrokenResource(shardedJedis);  
  361.         } finally {  
  362.             returnResource(shardedJedis);  
  363.         }  
  364.         return null;  
  365.     }  
  366.     /** 
  367.      * 设置HashSet对象 
  368.      * 
  369.      * @param domain 域名 
  370.      * @param key    键值 
  371.      * @param value  Json String or String value 
  372.      * @return 
  373.      */  
  374.     public boolean setHSet(String domain, String key, String value) {  
  375.         if (value == null) return false;  
  376.         ShardedJedis shardedJedis = null;  
  377.         try {  
  378.             shardedJedis = shardedJedisPool.getResource();  
  379.             shardedJedis.hset(domain, key, value);  
  380.             return true;  
  381.         } catch (Exception ex) {  
  382.             logger.error("setHSet error.", ex);  
  383.             returnBrokenResource(shardedJedis);  
  384.         } finally {  
  385.             returnResource(shardedJedis);  
  386.         }  
  387.         return false;  
  388.     }  
  389.   
  390.     /** 
  391.      * 获得HashSet对象 
  392.      * 
  393.      * @param domain 域名 
  394.      * @param key    键值 
  395.      * @return Json String or String value 
  396.      */  
  397.     public String getHSet(String domain, String key) {  
  398.         ShardedJedis shardedJedis = null;  
  399.         try {  
  400.             shardedJedis = shardedJedisPool.getResource();  
  401.             return shardedJedis.hget(domain, key);  
  402.         } catch (Exception ex) {  
  403.             logger.error("getHSet error.", ex);  
  404.             returnBrokenResource(shardedJedis);  
  405.         } finally {  
  406.             returnResource(shardedJedis);  
  407.         }  
  408.         return null;  
  409.     }  
  410.   
  411.     /** 
  412.      * 删除HashSet对象 
  413.      * 
  414.      * @param domain 域名 
  415.      * @param key    键值 
  416.      * @return 删除的记录数 
  417.      */  
  418.     public long delHSet(String domain, String key) {  
  419.         ShardedJedis shardedJedis = null;  
  420.         long count = 0;  
  421.         try {  
  422.             shardedJedis = shardedJedisPool.getResource();  
  423.             count = shardedJedis.hdel(domain, key);  
  424.         } catch (Exception ex) {  
  425.             logger.error("delHSet error.", ex);  
  426.             returnBrokenResource(shardedJedis);  
  427.         } finally {  
  428.             returnResource(shardedJedis);  
  429.         }  
  430.         return count;  
  431.     }  
  432.   
  433.     /** 
  434.      * 删除HashSet对象 
  435.      * 
  436.      * @param domain 域名 
  437.      * @param key    键值 
  438.      * @return 删除的记录数 
  439.      */  
  440.     public long delHSet(String domain, String... key) {  
  441.         ShardedJedis shardedJedis = null;  
  442.         long count = 0;  
  443.         try {  
  444.             shardedJedis = shardedJedisPool.getResource();  
  445.             count = shardedJedis.hdel(domain, key);  
  446.         } catch (Exception ex) {  
  447.             logger.error("delHSet error.", ex);  
  448.             returnBrokenResource(shardedJedis);  
  449.         } finally {  
  450.             returnResource(shardedJedis);  
  451.         }  
  452.         return count;  
  453.     }  
  454.   
  455.     /** 
  456.      * 判断key是否存在 
  457.      * 
  458.      * @param domain 域名 
  459.      * @param key    键值 
  460.      * @return 
  461.      */  
  462.     public boolean existsHSet(String domain, String key) {  
  463.         ShardedJedis shardedJedis = null;  
  464.         boolean isExist = false;  
  465.         try {  
  466.             shardedJedis = shardedJedisPool.getResource();  
  467.             isExist = shardedJedis.hexists(domain, key);  
  468.         } catch (Exception ex) {  
  469.             logger.error("existsHSet error.", ex);  
  470.             returnBrokenResource(shardedJedis);  
  471.         } finally {  
  472.             returnResource(shardedJedis);  
  473.         }  
  474.         return isExist;  
  475.     }  
  476.   
  477.     /** 
  478.      * 全局扫描hset 
  479.      * 
  480.      * @param match field匹配模式 
  481.      * @return 
  482.      */  
  483.     public List<Map.Entry<String, String>> scanHSet(String domain, String match) {  
  484.         ShardedJedis shardedJedis = null;  
  485.         try {  
  486.             int cursor = 0;  
  487.             shardedJedis = shardedJedisPool.getResource();  
  488.             ScanParams scanParams = new ScanParams();  
  489.             scanParams.match(match);  
  490.             Jedis jedis = shardedJedis.getShard(domain);  
  491.             ScanResult<Map.Entry<String, String>> scanResult;  
  492.             List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();  
  493.             do {  
  494.                 scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams);  
  495.                 list.addAll(scanResult.getResult());  
  496.                 cursor = Integer.parseInt(scanResult.getStringCursor());  
  497.             } while (cursor > 0);  
  498.             return list;  
  499.         } catch (Exception ex) {  
  500.             logger.error("scanHSet error.", ex);  
  501.             returnBrokenResource(shardedJedis);  
  502.         } finally {  
  503.             returnResource(shardedJedis);  
  504.         }  
  505.         return null;  
  506.     }  
  507.   
  508.   
  509.     /** 
  510.      * 返回 domain 指定的哈希集中所有字段的value值 
  511.      * 
  512.      * @param domain 
  513.      * @return 
  514.      */  
  515.   
  516.     public List<String> hvals(String domain) {  
  517.         ShardedJedis shardedJedis = null;  
  518.         List<String> retList = null;  
  519.         try {  
  520.             shardedJedis = shardedJedisPool.getResource();  
  521.             retList = shardedJedis.hvals(domain);  
  522.         } catch (Exception ex) {  
  523.             logger.error("hvals error.", ex);  
  524.             returnBrokenResource(shardedJedis);  
  525.         } finally {  
  526.             returnResource(shardedJedis);  
  527.         }  
  528.         return retList;  
  529.     }  
  530.   
  531.     /** 
  532.      * 返回 domain 指定的哈希集中所有字段的key值 
  533.      * 
  534.      * @param domain 
  535.      * @return 
  536.      */  
  537.   
  538.     public Set<String> hkeys(String domain) {  
  539.         ShardedJedis shardedJedis = null;  
  540.         Set<String> retList = null;  
  541.         try {  
  542.             shardedJedis = shardedJedisPool.getResource();  
  543.             retList = shardedJedis.hkeys(domain);  
  544.         } catch (Exception ex) {  
  545.             logger.error("hkeys error.", ex);  
  546.             returnBrokenResource(shardedJedis);  
  547.         } finally {  
  548.             returnResource(shardedJedis);  
  549.         }  
  550.         return retList;  
  551.     }  
  552.   
  553.     /** 
  554.      * 返回 domain 指定的哈希key值总数 
  555.      * 
  556.      * @param domain 
  557.      * @return 
  558.      */  
  559.     public long lenHset(String domain) {  
  560.         ShardedJedis shardedJedis = null;  
  561.         long retList = 0;  
  562.         try {  
  563.             shardedJedis = shardedJedisPool.getResource();  
  564.             retList = shardedJedis.hlen(domain);  
  565.         } catch (Exception ex) {  
  566.             logger.error("hkeys error.", ex);  
  567.             returnBrokenResource(shardedJedis);  
  568.         } finally {  
  569.             returnResource(shardedJedis);  
  570.         }  
  571.         return retList;  
  572.     }  
  573.   
  574.     /** 
  575.      * 设置排序集合 
  576.      * 
  577.      * @param key 
  578.      * @param score 
  579.      * @param value 
  580.      * @return 
  581.      */  
  582.     public boolean setSortedSet(String key, long score, String value) {  
  583.         ShardedJedis shardedJedis = null;  
  584.         try {  
  585.             shardedJedis = shardedJedisPool.getResource();  
  586.             shardedJedis.zadd(key, score, value);  
  587.             return true;  
  588.         } catch (Exception ex) {  
  589.             logger.error("setSortedSet error.", ex);  
  590.             returnBrokenResource(shardedJedis);  
  591.         } finally {  
  592.             returnResource(shardedJedis);  
  593.         }  
  594.         return false;  
  595.     }  
  596.   
  597.     /** 
  598.      * 获得排序集合 
  599.      * 
  600.      * @param key 
  601.      * @param startScore 
  602.      * @param endScore 
  603.      * @param orderByDesc 
  604.      * @return 
  605.      */  
  606.     public Set<String> getSoredSet(String key, long startScore, long endScore, boolean orderByDesc) {  
  607.         ShardedJedis shardedJedis = null;  
  608.         try {  
  609.             shardedJedis = shardedJedisPool.getResource();  
  610.             if (orderByDesc) {  
  611.                 return shardedJedis.zrevrangeByScore(key, endScore, startScore);  
  612.             } else {  
  613.                 return shardedJedis.zrangeByScore(key, startScore, endScore);  
  614.             }  
  615.         } catch (Exception ex) {  
  616.             logger.error("getSoredSet error.", ex);  
  617.             returnBrokenResource(shardedJedis);  
  618.         } finally {  
  619.             returnResource(shardedJedis);  
  620.         }  
  621.         return null;  
  622.     }  
  623.   
  624.     /** 
  625.      * 计算排序长度 
  626.      * 
  627.      * @param key 
  628.      * @param startScore 
  629.      * @param endScore 
  630.      * @return 
  631.      */  
  632.     public long countSoredSet(String key, long startScore, long endScore) {  
  633.         ShardedJedis shardedJedis = null;  
  634.         try {  
  635.             shardedJedis = shardedJedisPool.getResource();  
  636.             Long count = shardedJedis.zcount(key, startScore, endScore);  
  637.             return count == null ? 0L : count;  
  638.         } catch (Exception ex) {  
  639.             logger.error("countSoredSet error.", ex);  
  640.             returnBrokenResource(shardedJedis);  
  641.         } finally {  
  642.             returnResource(shardedJedis);  
  643.         }  
  644.         return 0L;  
  645.     }  
  646.   
  647.     /** 
  648.      * 删除排序集合 
  649.      * 
  650.      * @param key 
  651.      * @param value 
  652.      * @return 
  653.      */  
  654.     public boolean delSortedSet(String key, String value) {  
  655.         ShardedJedis shardedJedis = null;  
  656.         try {  
  657.             shardedJedis = shardedJedisPool.getResource();  
  658.             long count = shardedJedis.zrem(key, value);  
  659.             return count > 0;  
  660.         } catch (Exception ex) {  
  661.             logger.error("delSortedSet error.", ex);  
  662.             returnBrokenResource(shardedJedis);  
  663.         } finally {  
  664.             returnResource(shardedJedis);  
  665.         }  
  666.         return false;  
  667.     }  
  668.   
  669.     /** 
  670.      * 获得排序集合 
  671.      * 
  672.      * @param key 
  673.      * @param startRange 
  674.      * @param endRange 
  675.      * @param orderByDesc 
  676.      * @return 
  677.      */  
  678.     public Set<String> getSoredSetByRange(String key, int startRange, int endRange, boolean orderByDesc) {  
  679.         ShardedJedis shardedJedis = null;  
  680.         try {  
  681.             shardedJedis = shardedJedisPool.getResource();  
  682.             if (orderByDesc) {  
  683.                 return shardedJedis.zrevrange(key, startRange, endRange);  
  684.             } else {  
  685.                 return shardedJedis.zrange(key, startRange, endRange);  
  686.             }  
  687.         } catch (Exception ex) {  
  688.             logger.error("getSoredSetByRange error.", ex);  
  689.             returnBrokenResource(shardedJedis);  
  690.         } finally {  
  691.             returnResource(shardedJedis);  
  692.         }  
  693.         return null;  
  694.     }  
  695.   
  696.     /** 
  697.      * 获得排序打分 
  698.      * 
  699.      * @param key 
  700.      * @return 
  701.      */  
  702.     public Double getScore(String key, String member) {  
  703.         ShardedJedis shardedJedis = null;  
  704.         try {  
  705.             shardedJedis = shardedJedisPool.getResource();  
  706.             return shardedJedis.zscore(key, member);  
  707.         } catch (Exception ex) {  
  708.             logger.error("getSoredSet error.", ex);  
  709.             returnBrokenResource(shardedJedis);  
  710.         } finally {  
  711.             returnResource(shardedJedis);  
  712.         }  
  713.         return null;  
  714.     }  
  715.   
  716.     public boolean set(String key, String value, int second) {  
  717.         ShardedJedis shardedJedis = null;  
  718.         try {  
  719.             shardedJedis = shardedJedisPool.getResource();  
  720.             shardedJedis.setex(key, second, value);  
  721.             return true;  
  722.         } catch (Exception ex) {  
  723.             logger.error("set error.", ex);  
  724.             returnBrokenResource(shardedJedis);  
  725.         } finally {  
  726.             returnResource(shardedJedis);  
  727.         }  
  728.         return false;  
  729.     }  
  730.   
  731.     public boolean set(String key, String value) {  
  732.         ShardedJedis shardedJedis = null;  
  733.         try {  
  734.             shardedJedis = shardedJedisPool.getResource();  
  735.             shardedJedis.set(key, value);  
  736.             return true;  
  737.         } catch (Exception ex) {  
  738.             logger.error("set error.", ex);  
  739.             returnBrokenResource(shardedJedis);  
  740.         } finally {  
  741.             returnResource(shardedJedis);  
  742.         }  
  743.         return false;  
  744.     }  
  745.   
  746.     public String get(String key, String defaultValue) {  
  747.         ShardedJedis shardedJedis = null;  
  748.         try {  
  749.             shardedJedis = shardedJedisPool.getResource();  
  750.             return shardedJedis.get(key) == null?defaultValue:shardedJedis.get(key);  
  751.         } catch (Exception ex) {  
  752.             logger.error("get error.", ex);  
  753.             returnBrokenResource(shardedJedis);  
  754.         } finally {  
  755.             returnResource(shardedJedis);  
  756.         }  
  757.         return defaultValue;  
  758.     }  
  759.   
  760.     public boolean del(String key) {  
  761.         ShardedJedis shardedJedis = null;  
  762.         try {  
  763.             shardedJedis = shardedJedisPool.getResource();  
  764.             shardedJedis.del(key);  
  765.             return true;  
  766.         } catch (Exception ex) {  
  767.             logger.error("del error.", ex);  
  768.             returnBrokenResource(shardedJedis);  
  769.         } finally {  
  770.             returnResource(shardedJedis);  
  771.         }  
  772.         return false;  
  773.     }  
  774.   
  775.     public long incr(String key) {  
  776.         ShardedJedis shardedJedis = null;  
  777.         try {  
  778.             shardedJedis = shardedJedisPool.getResource();  
  779.             return shardedJedis.incr(key);  
  780.         } catch (Exception ex) {  
  781.             logger.error("incr error.", ex);  
  782.             returnBrokenResource(shardedJedis);  
  783.         } finally {  
  784.             returnResource(shardedJedis);  
  785.         }  
  786.         return 0;  
  787.     }  
  788.   
  789.     public long decr(String key) {  
  790.         ShardedJedis shardedJedis = null;  
  791.         try {  
  792.             shardedJedis = shardedJedisPool.getResource();  
  793.             return shardedJedis.decr(key);  
  794.         } catch (Exception ex) {  
  795.             logger.error("incr error.", ex);  
  796.             returnBrokenResource(shardedJedis);  
  797.         } finally {  
  798.             returnResource(shardedJedis);  
  799.         }  
  800.         return 0;  
  801.     }  
  802.   
  803.   
  804.   
  805.     private void returnBrokenResource(ShardedJedis shardedJedis) {  
  806.         try {  
  807.             shardedJedisPool.returnBrokenResource(shardedJedis);  
  808.         } catch (Exception e) {  
  809.             logger.error("returnBrokenResource error.", e);  
  810.         }  
  811.     }  
  812.   
  813.     private void returnResource(ShardedJedis shardedJedis) {  
  814.         try {  
  815.             shardedJedisPool.returnResource(shardedJedis);  
  816.         } catch (Exception e) {  
  817.             logger.error("returnResource error.", e);  
  818.         }  
  819.     } 






原标题:Redis客户端之Spring整合Jedis

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流