redis客户端之jedis 操作工具类

本文详细介绍了一种基于Java的Redis客户端工具——Jedis的操作方法,涵盖了连接池配置、基本命令如增删查改,以及高级特性如排序、哈希等的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

redis客户端之jedis 操作工具类

转载自http://blog.youkuaiyun.com/huahuagongzi99999/article/details/13659849


包括连接池的使用及 jedis对string 、set、sortset、list、hash的所有常规操作。

redis.properties文件:

[html]  view plain copy
  1. #\u63A7\u5236\u4E00\u4E2Apool\u53EF\u5206\u914D\u591A\u5C11\u4E2Ajedis\u5B9E\u4F8B\uFF0C\u901A\u8FC7pool.getResource()\u6765\u83B7\u53D6\uFF1B\u5982\u679C\u8D4B\u503C\u4E3A-1\uFF0C\u5219\u8868\u793A\u4E0D\u9650\u5236\uFF1B\u5982\u679Cpool\u5DF2\u7ECF\u5206\u914D\u4E86maxActive\u4E2Ajedis\u5B9E\u4F8B\uFF0C\u5219\u6B64\u65F6pool\u7684\u72B6\u6001\u5C31\u6210exhausted\u4E86\uFF0C\u5728JedisPoolConfig  
  2. redis.pool.maxActive=1024  
  3. #\u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle\u7684jedis\u5B9E\u4F8B\uFF1B  
  4. redis.pool.maxIdle=200  
  5. #\u8868\u793A\u5F53borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B  
  6. redis.pool.maxWait=1000  
  7. #\u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Calidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684\uFF1B  
  8. redis.pool.testOnBorrow=true  
  9. #\u5728return\u7ED9pool\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B  
  10. redis.pool.testOnReturn=true  
  11. #ip  
  12. redis.ip=yourip  
  13. #port  
  14. redis.port=6379  
  15. #password  
  16. redis.password=yourword  

[java]  view plain copy
  1. package com.test.util;  
  2.   
  3. import java.util.List;  
  4. import java.util.Map;  
  5. import java.util.Set;  
  6.   
  7. import org.apache.log4j.Logger;  
  8.   
  9. import com.test.constants.JRedisPoolConfig;    
  10.   
  11. import redis.clients.jedis.Jedis;  
  12. import redis.clients.jedis.JedisPool;  
  13. import redis.clients.jedis.JedisPoolConfig;   
  14. import redis.clients.jedis.SortingParams;  
  15. import redis.clients.jedis.BinaryClient.LIST_POSITION;  
  16. import redis.clients.util.SafeEncoder;  
  17.   
  18. /** 
  19.  * @author Mr.hu 
  20.  * @version crateTime:2013-10-30 下午5:41:30 
  21.  * Class Explain:JedisUtil   
  22.  */  
  23. public class JedisUtil {   
  24.       
  25.      private Logger log = Logger.getLogger(this.getClass());    
  26.      /**缓存生存时间 */  
  27.      private final int expire = 60000;  
  28.      /** 操作Key的方法 */  
  29.      public Keys KEYS;  
  30.      /** 对存储结构为String类型的操作 */  
  31.      public Strings STRINGS;  
  32.      /** 对存储结构为List类型的操作 */  
  33.      public Lists LISTS;  
  34.      /** 对存储结构为Set类型的操作 */  
  35.      public Sets SETS;  
  36.      /** 对存储结构为HashMap类型的操作 */  
  37.      public Hash HASH;  
  38.      /** 对存储结构为Set(排序的)类型的操作 */  
  39.      public SortSet SORTSET;  
  40.      private static JedisPool jedisPool = null;    
  41.            
  42.      private JedisUtil() {     
  43.           
  44.      }   
  45.      static {    
  46.             JedisPoolConfig config = new JedisPoolConfig();  
  47.             config.setMaxActive(JRedisPoolConfig.MAX_ACTIVE);     
  48.             config.setMaxIdle(JRedisPoolConfig.MAX_IDLE);  
  49.             config.setMaxWait(JRedisPoolConfig.MAX_WAIT);   
  50.             config.setTestOnBorrow(JRedisPoolConfig.TEST_ON_BORROW);  
  51.             config.setTestOnReturn(JRedisPoolConfig.TEST_ON_RETURN);   
  52.             //redis如果设置了密码:  
  53.             jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP,   
  54.                     JRedisPoolConfig.REDIS_PORT,  
  55.                     10000,JRedisPoolConfig.REDIS_PASSWORD);      
  56.             //redis未设置了密码:  
  57.            // jedisPool = new JedisPool(config, JRedisPoolConfig.REDIS_IP,   
  58.                 //  JRedisPoolConfig.REDIS_PORT);   
  59.        }  
  60.        
  61.     public JedisPool getPool() {    
  62.         return jedisPool;   
  63.     }  
  64.       
  65.      /** 
  66.       * 从jedis连接池中获取获取jedis对象   
  67.       * @return 
  68.       */  
  69.      public Jedis getJedis() {    
  70.          return jedisPool.getResource();   
  71.     }  
  72.        
  73.        
  74.      private static final JedisUtil jedisUtil = new JedisUtil();  
  75.        
  76.    
  77.     /** 
  78.      * 获取JedisUtil实例 
  79.      * @return 
  80.      */  
  81.     public static JedisUtil getInstance() {  
  82.         return jedisUtil;   
  83.     }  
  84.   
  85.     /** 
  86.      * 回收jedis 
  87.      * @param jedis 
  88.      */  
  89.     public void returnJedis(Jedis jedis) {  
  90.         jedisPool.returnResource(jedis);  
  91.     }   
  92.   
  93.       
  94.     /** 
  95.      * 设置过期时间 
  96.      * @author ruan 2013-4-11 
  97.      * @param key 
  98.      * @param seconds 
  99.      */  
  100.     public void expire(String key, int seconds) {  
  101.         if (seconds <= 0) {   
  102.             return;  
  103.         }  
  104.         Jedis jedis = getJedis();  
  105.         jedis.expire(key, seconds);  
  106.         returnJedis(jedis);  
  107.     }  
  108.   
  109.     /** 
  110.      * 设置默认过期时间 
  111.      * @author ruan 2013-4-11 
  112.      * @param key 
  113.      */  
  114.     public void expire(String key) {  
  115.         expire(key, expire);  
  116.     }  
  117.       
  118.       
  119.     //*******************************************Keys*******************************************//  
  120.     public class Keys {  
  121.   
  122.         /** 
  123.          * 清空所有key 
  124.          */  
  125.         public String flushAll() {  
  126.             Jedis jedis = getJedis();  
  127.             String stata = jedis.flushAll();  
  128.             returnJedis(jedis);  
  129.             return stata;  
  130.         }  
  131.   
  132.         /** 
  133.          * 更改key 
  134.          * @param String oldkey 
  135.          * @param String  newkey 
  136.          * @return 状态码 
  137.          * */  
  138.         public String rename(String oldkey, String newkey) {   
  139.             return rename(SafeEncoder.encode(oldkey),  
  140.                     SafeEncoder.encode(newkey));  
  141.         }  
  142.   
  143.         /** 
  144.          * 更改key,仅当新key不存在时才执行 
  145.          * @param String oldkey 
  146.          * @param String newkey  
  147.          * @return 状态码 
  148.          * */  
  149.         public long renamenx(String oldkey, String newkey) {  
  150.             Jedis jedis = getJedis();  
  151.             long status = jedis.renamenx(oldkey, newkey);  
  152.             returnJedis(jedis);  
  153.             return status;  
  154.         }  
  155.   
  156.         /** 
  157.          * 更改key 
  158.          * @param String oldkey 
  159.          * @param String newkey 
  160.          * @return 状态码 
  161.          * */  
  162.         public String rename(byte[] oldkey, byte[] newkey) {  
  163.             Jedis jedis = getJedis();  
  164.             String status = jedis.rename(oldkey, newkey);  
  165.             returnJedis(jedis);  
  166.             return status;  
  167.         }  
  168.   
  169.         /** 
  170.          * 设置key的过期时间,以秒为单位 
  171.          * @param String key 
  172.          * @param 时间,已秒为单位 
  173.          * @return 影响的记录数 
  174.          * */  
  175.         public long expired(String key, int seconds) {  
  176.             Jedis jedis = getJedis();  
  177.             long count = jedis.expire(key, seconds);  
  178.             returnJedis(jedis);  
  179.             return count;  
  180.         }  
  181.   
  182.         /** 
  183.          * 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。 
  184.          * @param String key 
  185.          * @param 时间,已秒为单位 
  186.          * @return 影响的记录数 
  187.          * */  
  188.         public long expireAt(String key, long timestamp) {  
  189.             Jedis jedis = getJedis();  
  190.             long count = jedis.expireAt(key, timestamp);  
  191.             returnJedis(jedis);  
  192.             return count;  
  193.         }  
  194.   
  195.         /** 
  196.          * 查询key的过期时间 
  197.          * @param String key 
  198.          * @return 以秒为单位的时间表示 
  199.          * */  
  200.         public long ttl(String key) {  
  201.             //ShardedJedis sjedis = getShardedJedis();  
  202.             Jedis sjedis=getJedis();   
  203.             long len = sjedis.ttl(key);  
  204.             returnJedis(sjedis);  
  205.             return len;  
  206.         }  
  207.   
  208.         /** 
  209.          * 取消对key过期时间的设置 
  210.          * @param key 
  211.          * @return 影响的记录数 
  212.          * */  
  213.         public long persist(String key) {  
  214.             Jedis jedis = getJedis();  
  215.             long count = jedis.persist(key);  
  216.             returnJedis(jedis);  
  217.             return count;  
  218.         }  
  219.   
  220.         /** 
  221.          * 删除keys对应的记录,可以是多个key 
  222.          * @param String  ... keys 
  223.          * @return 删除的记录数 
  224.          * */  
  225.         public long del(String... keys) {  
  226.             Jedis jedis = getJedis();  
  227.             long count = jedis.del(keys);  
  228.             returnJedis(jedis);  
  229.             return count;  
  230.         }  
  231.   
  232.         /** 
  233.          * 删除keys对应的记录,可以是多个key 
  234.          * @param String .. keys 
  235.          * @return 删除的记录数 
  236.          * */  
  237.         public long del(byte[]... keys) {  
  238.             Jedis jedis = getJedis();  
  239.             long count = jedis.del(keys);  
  240.             returnJedis(jedis);  
  241.             return count;  
  242.         }  
  243.   
  244.         /** 
  245.          * 判断key是否存在 
  246.          * @param String key 
  247.          * @return boolean 
  248.          * */  
  249.         public boolean exists(String key) {  
  250.             //ShardedJedis sjedis = getShardedJedis();  
  251.             Jedis sjedis=getJedis();    
  252.             boolean exis = sjedis.exists(key);  
  253.             returnJedis(sjedis);  
  254.             return exis;  
  255.         }  
  256.   
  257.         /** 
  258.          * 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法 
  259.          * @param String key 
  260.          * @return List<String> 集合的全部记录 
  261.          * **/  
  262.         public List<String> sort(String key) {  
  263.             //ShardedJedis sjedis = getShardedJedis();  
  264.             Jedis sjedis=getJedis();    
  265.             List<String> list = sjedis.sort(key);  
  266.             returnJedis(sjedis);  
  267.             return list;  
  268.         }  
  269.   
  270.         /** 
  271.          * 对List,Set,SortSet进行排序或limit 
  272.          * @param String key 
  273.          * @param SortingParams parame 定义排序类型或limit的起止位置. 
  274.          * @return List<String> 全部或部分记录 
  275.          * **/  
  276.         public List<String> sort(String key, SortingParams parame) {  
  277.             //ShardedJedis sjedis = getShardedJedis();   
  278.             Jedis sjedis=getJedis();   
  279.             List<String> list = sjedis.sort(key, parame);  
  280.             returnJedis(sjedis);  
  281.             return list;  
  282.         }  
  283.   
  284.         /** 
  285.          * 返回指定key存储的类型 
  286.          * @param String key 
  287.          * @return String string|list|set|zset|hash 
  288.          * **/  
  289.         public String type(String key) {  
  290.             //ShardedJedis sjedis = getShardedJedis();   
  291.             Jedis sjedis=getJedis();    
  292.             String type = sjedis.type(key);   
  293.             returnJedis(sjedis);  
  294.             return type;  
  295.         }  
  296.   
  297.         /** 
  298.          * 查找所有匹配给定的模式的键 
  299.          * @param String  key的表达式,*表示多个,?表示一个 
  300.          * */  
  301.         public Set<String> keys(String pattern) {  
  302.             Jedis jedis = getJedis();  
  303.             Set<String> set = jedis.keys(pattern);  
  304.             returnJedis(jedis);  
  305.             return set;  
  306.         }  
  307.     }  
  308.   
  309.     //*******************************************Sets*******************************************//  
  310.     public class Sets {  
  311.   
  312.         /** 
  313.          * 向Set添加一条记录,如果member已存在返回0,否则返回1 
  314.          * @param String  key 
  315.          * @param String member 
  316.          * @return 操作码,0或1 
  317.          * */  
  318.         public long sadd(String key, String member) {  
  319.             Jedis jedis = getJedis();  
  320.             long s = jedis.sadd(key, member);  
  321.             returnJedis(jedis);  
  322.             return s;  
  323.         }  
  324.   
  325.         public long sadd(byte[] key, byte[] member) {  
  326.             Jedis jedis = getJedis();  
  327.             long s = jedis.sadd(key, member);  
  328.             returnJedis(jedis);  
  329.             return s;  
  330.         }  
  331.   
  332.         /** 
  333.          * 获取给定key中元素个数 
  334.          * @param String key 
  335.          * @return 元素个数 
  336.          * */  
  337.         public long scard(String key) {  
  338.             //ShardedJedis sjedis = getShardedJedis();  
  339.             Jedis sjedis = getJedis();   
  340.             long len = sjedis.scard(key);  
  341.             returnJedis(sjedis);  
  342.             return len;  
  343.         }  
  344.   
  345.         /** 
  346.          * 返回从第一组和所有的给定集合之间的差异的成员 
  347.          * @param String ... keys 
  348.          * @return 差异的成员集合 
  349.          * */  
  350.         public Set<String> sdiff(String... keys) {  
  351.             Jedis jedis = getJedis();  
  352.             Set<String> set = jedis.sdiff(keys);  
  353.             returnJedis(jedis);  
  354.             return set;  
  355.         }  
  356.   
  357.         /** 
  358.          * 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。 
  359.          * @param String newkey 新结果集的key 
  360.          * @param String ... keys 比较的集合 
  361.          * @return 新集合中的记录数 
  362.          * **/  
  363.         public long sdiffstore(String newkey, String... keys) {  
  364.             Jedis jedis = getJedis();  
  365.             long s = jedis.sdiffstore(newkey, keys);  
  366.             returnJedis(jedis);  
  367.             return s;  
  368.         }  
  369.   
  370.         /** 
  371.          * 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set 
  372.          * @param String ... keys 
  373.          * @return 交集成员的集合 
  374.          * **/  
  375.         public Set<String> sinter(String... keys) {  
  376.             Jedis jedis = getJedis();  
  377.             Set<String> set = jedis.sinter(keys);  
  378.             returnJedis(jedis);  
  379.             return set;  
  380.         }  
  381.   
  382.         /** 
  383.          * 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。 
  384.          * @param String  newkey 新结果集的key 
  385.          * @param String ... keys 比较的集合 
  386.          * @return 新集合中的记录数 
  387.          * **/  
  388.         public long sinterstore(String newkey, String... keys) {  
  389.             Jedis jedis = getJedis();  
  390.             long s = jedis.sinterstore(newkey, keys);  
  391.             returnJedis(jedis);  
  392.             return s;  
  393.         }  
  394.   
  395.         /** 
  396.          * 确定一个给定的值是否存在 
  397.          * @param String  key 
  398.          * @param String member 要判断的值 
  399.          * @return 存在返回1,不存在返回0 
  400.          * **/  
  401.         public boolean sismember(String key, String member) {  
  402.             //ShardedJedis sjedis = getShardedJedis();  
  403.             Jedis sjedis = getJedis();   
  404.             boolean s = sjedis.sismember(key, member);  
  405.             returnJedis(sjedis);  
  406.             return s;  
  407.         }  
  408.   
  409.         /** 
  410.          * 返回集合中的所有成员 
  411.          * @param String  key 
  412.          * @return 成员集合 
  413.          * */  
  414.         public Set<String> smembers(String key) {  
  415.             //ShardedJedis sjedis = getShardedJedis();  
  416.             Jedis sjedis = getJedis();   
  417.             Set<String> set = sjedis.smembers(key);  
  418.             returnJedis(sjedis);  
  419.             return set;  
  420.         }  
  421.   
  422.         public Set<byte[]> smembers(byte[] key) {  
  423.             //ShardedJedis sjedis = getShardedJedis();  
  424.             Jedis sjedis = getJedis();    
  425.             Set<byte[]> set = sjedis.smembers(key);  
  426.             returnJedis(sjedis);  
  427.             return set;  
  428.         }  
  429.   
  430.         /** 
  431.          * 将成员从源集合移出放入目标集合 <br/> 
  432.          * 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/> 
  433.          * 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除 
  434.          * @param String  srckey 源集合 
  435.          * @param String dstkey 目标集合 
  436.          * @param String member 源集合中的成员 
  437.          * @return 状态码,1成功,0失败 
  438.          * */  
  439.         public long smove(String srckey, String dstkey, String member) {  
  440.             Jedis jedis = getJedis();  
  441.             long s = jedis.smove(srckey, dstkey, member);  
  442.             returnJedis(jedis);  
  443.             return s;  
  444.         }  
  445.   
  446.         /** 
  447.          * 从集合中删除成员 
  448.          * @param String  key 
  449.          * @return 被删除的成员 
  450.          * */  
  451.         public String spop(String key) {  
  452.             Jedis jedis = getJedis();  
  453.             String s = jedis.spop(key);  
  454.             returnJedis(jedis);  
  455.             return s;  
  456.         }  
  457.   
  458.         /** 
  459.          * 从集合中删除指定成员 
  460.          * @param String key 
  461.          * @param String  member 要删除的成员 
  462.          * @return 状态码,成功返回1,成员不存在返回0 
  463.          * */  
  464.         public long srem(String key, String member) {  
  465.             Jedis jedis = getJedis();  
  466.             long s = jedis.srem(key, member);  
  467.             returnJedis(jedis);  
  468.             return s;  
  469.         }  
  470.   
  471.         /** 
  472.          * 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/> 
  473.          * @param String  ... keys 
  474.          * @return 合并后的结果集合 
  475.          * @see sunionstore 
  476.          * */  
  477.         public Set<String> sunion(String... keys) {  
  478.             Jedis jedis = getJedis();  
  479.             Set<String> set = jedis.sunion(keys);  
  480.             returnJedis(jedis);  
  481.             return set;  
  482.         }  
  483.   
  484.         /** 
  485.          * 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖 
  486.          * @param String  newkey 新集合的key 
  487.          * @param String ... keys 要合并的集合 
  488.          * **/  
  489.         public long sunionstore(String newkey, String... keys) {  
  490.             Jedis jedis = getJedis();  
  491.             long s = jedis.sunionstore(newkey, keys);  
  492.             returnJedis(jedis);  
  493.             return s;  
  494.         }  
  495.     }  
  496.   
  497.     //*******************************************SortSet*******************************************//  
  498.     public class SortSet {  
  499.   
  500.         /** 
  501.          * 向集合中增加一条记录,如果这个值已存在,这个值对应的权重将被置为新的权重 
  502.          * @param String  key 
  503.          * @param double score 权重 
  504.          * @param String  member 要加入的值, 
  505.          * @return 状态码 1成功,0已存在member的值 
  506.          * */  
  507.         public long zadd(String key, double score, String member) {  
  508.             Jedis jedis = getJedis();  
  509.             long s = jedis.zadd(key, score, member);  
  510.             returnJedis(jedis);  
  511.             return s;  
  512.         }  
  513.   
  514.         public long zadd(String key, Map<Double, String> scoreMembers) {  
  515.             Jedis jedis = getJedis();  
  516.             long s = jedis.zadd(key, scoreMembers);  
  517.             returnJedis(jedis);  
  518.             return s;  
  519.         }  
  520.   
  521.         /** 
  522.          * 获取集合中元素的数量 
  523.          * @param String  key 
  524.          * @return 如果返回0则集合不存在 
  525.          * */  
  526.         public long zcard(String key) {  
  527.             //ShardedJedis sjedis = getShardedJedis();  
  528.             Jedis sjedis = getJedis();  
  529.             long len = sjedis.zcard(key);  
  530.             returnJedis(sjedis);  
  531.             return len;  
  532.         }  
  533.   
  534.         /** 
  535.          * 获取指定权重区间内集合的数量 
  536.          * @param String key 
  537.          * @param double min 最小排序位置 
  538.          * @param double max 最大排序位置 
  539.          * */  
  540.         public long zcount(String key, double min, double max) {  
  541.             //ShardedJedis sjedis = getShardedJedis();  
  542.             Jedis sjedis = getJedis();  
  543.             long len = sjedis.zcount(key, min, max);  
  544.             returnJedis(sjedis);  
  545.             return len;  
  546.         }  
  547.   
  548.         /** 
  549.          * 获得set的长度 
  550.          *  
  551.          * @param key 
  552.          * @return 
  553.          */  
  554.         public long zlength(String key) {  
  555.             long len = 0;  
  556.             Set<String> set = zrange(key, 0, -1);  
  557.             len = set.size();  
  558.             return len;  
  559.         }  
  560.   
  561.         /** 
  562.          * 权重增加给定值,如果给定的member已存在 
  563.          * @param String  key 
  564.          * @param double score 要增的权重 
  565.          * @param String  member 要插入的值 
  566.          * @return 增后的权重 
  567.          * */  
  568.         public double zincrby(String key, double score, String member) {  
  569.             Jedis jedis = getJedis();  
  570.             double s = jedis.zincrby(key, score, member);  
  571.             returnJedis(jedis);  
  572.             return s;  
  573.         }  
  574.   
  575.         /** 
  576.          * 返回指定位置的集合元素,0为第一个元素,-1为最后一个元素 
  577.          * @param String key 
  578.          * @param int start 开始位置(包含) 
  579.          * @param int end 结束位置(包含) 
  580.          * @return Set<String> 
  581.          * */  
  582.         public Set<String> zrange(String key, int start, int end) {  
  583.             //ShardedJedis sjedis = getShardedJedis();  
  584.             Jedis sjedis = getJedis();   
  585.             Set<String> set = sjedis.zrange(key, start, end);  
  586.             returnJedis(sjedis);  
  587.             return set;  
  588.         }  
  589.   
  590.         /** 
  591.          * 返回指定权重区间的元素集合 
  592.          * @param String key 
  593.          * @param double min 上限权重 
  594.          * @param double max 下限权重 
  595.          * @return Set<String> 
  596.          * */  
  597.         public Set<String> zrangeByScore(String key, double min, double max) {  
  598.             //ShardedJedis sjedis = getShardedJedis();  
  599.             Jedis sjedis = getJedis();   
  600.             Set<String> set = sjedis.zrangeByScore(key, min, max);  
  601.             returnJedis(sjedis);  
  602.             return set;  
  603.         }  
  604.   
  605.         /** 
  606.          * 获取指定值在集合中的位置,集合排序从低到高 
  607.          * @see zrevrank 
  608.          * @param String key 
  609.          * @param String member 
  610.          * @return long 位置 
  611.          * */  
  612.         public long zrank(String key, String member) {  
  613.             //ShardedJedis sjedis = getShardedJedis();  
  614.             Jedis sjedis = getJedis();   
  615.             long index = sjedis.zrank(key, member);  
  616.             returnJedis(sjedis);  
  617.             return index;  
  618.         }  
  619.   
  620.         /** 
  621.          * 获取指定值在集合中的位置,集合排序从高到低 
  622.          * @see zrank 
  623.          * @param String key 
  624.          * @param String member 
  625.          * @return long 位置 
  626.          * */  
  627.         public long zrevrank(String key, String member) {  
  628.             //ShardedJedis sjedis = getShardedJedis();  
  629.             Jedis sjedis = getJedis();   
  630.             long index = sjedis.zrevrank(key, member);  
  631.             returnJedis(sjedis);  
  632.             return index;  
  633.         }  
  634.   
  635.         /** 
  636.          * 从集合中删除成员 
  637.          * @param String key 
  638.          * @param String member  
  639.          * @return 返回1成功 
  640.          * */  
  641.         public long zrem(String key, String member) {  
  642.             Jedis jedis = getJedis();  
  643.             long s = jedis.zrem(key, member);  
  644.             returnJedis(jedis);  
  645.             return s;  
  646.         }  
  647.   
  648.         /** 
  649.          * 删除 
  650.          * @param key 
  651.          * @return 
  652.          */  
  653.         public long zrem(String key) {  
  654.             Jedis jedis = getJedis();  
  655.             long s = jedis.del(key);  
  656.             returnJedis(jedis);  
  657.             return s;  
  658.         }  
  659.   
  660.         /** 
  661.          * 删除给定位置区间的元素 
  662.          * @param String  key 
  663.          * @param int start 开始区间,从0开始(包含) 
  664.          * @param int end 结束区间,-1为最后一个元素(包含) 
  665.          * @return 删除的数量 
  666.          * */  
  667.         public long zremrangeByRank(String key, int start, int end) {  
  668.             Jedis jedis = getJedis();  
  669.             long s = jedis.zremrangeByRank(key, start, end);  
  670.             returnJedis(jedis);  
  671.             return s;  
  672.         }  
  673.   
  674.         /** 
  675.          * 删除给定权重区间的元素 
  676.          * @param String key 
  677.          * @param double min 下限权重(包含) 
  678.          * @param double max 上限权重(包含) 
  679.          * @return 删除的数量 
  680.          * */  
  681.         public long zremrangeByScore(String key, double min, double max) {  
  682.             Jedis jedis = getJedis();  
  683.             long s = jedis.zremrangeByScore(key, min, max);  
  684.             returnJedis(jedis);  
  685.             return s;  
  686.         }  
  687.   
  688.         /** 
  689.          * 获取给定区间的元素,原始按照权重由高到低排序 
  690.          * @param String  key 
  691.          * @param int start 
  692.          * @param int end 
  693.          * @return Set<String> 
  694.          * */  
  695.         public Set<String> zrevrange(String key, int start, int end) {  
  696.             //ShardedJedis sjedis = getShardedJedis();  
  697.             Jedis sjedis = getJedis();   
  698.             Set<String> set = sjedis.zrevrange(key, start, end);  
  699.             returnJedis(sjedis);  
  700.             return set;  
  701.         }  
  702.   
  703.         /** 
  704.          * 获取给定值在集合中的权重 
  705.          * @param String  key 
  706.          * @param memeber 
  707.          * @return double 权重 
  708.          * */  
  709.         public double zscore(String key, String memebr) {  
  710.             //ShardedJedis sjedis = getShardedJedis();  
  711.             Jedis sjedis = getJedis();   
  712.             Double score = sjedis.zscore(key, memebr);  
  713.             returnJedis(sjedis);  
  714.             if (score != null)  
  715.                 return score;  
  716.             return 0;  
  717.         }  
  718.     }  
  719.       
  720.     //*******************************************Hash*******************************************//  
  721.     public class Hash {  
  722.   
  723.         /** 
  724.          * 从hash中删除指定的存储 
  725.          * @param String key 
  726.          * @param String  fieid 存储的名字 
  727.          * @return 状态码,1成功,0失败 
  728.          * */  
  729.         public long hdel(String key, String fieid) {  
  730.             Jedis jedis = getJedis();  
  731.             long s = jedis.hdel(key, fieid);  
  732.             returnJedis(jedis);  
  733.             return s;  
  734.         }  
  735.   
  736.         public long hdel(String key) {  
  737.             Jedis jedis = getJedis();  
  738.             long s = jedis.del(key);  
  739.             returnJedis(jedis);  
  740.             return s;  
  741.         }  
  742.   
  743.         /** 
  744.          * 测试hash中指定的存储是否存在 
  745.          * @param String key 
  746.          * @param String  fieid 存储的名字 
  747.          * @return 1存在,0不存在 
  748.          * */  
  749.         public boolean hexists(String key, String fieid) {  
  750.             //ShardedJedis sjedis = getShardedJedis();  
  751.             Jedis sjedis = getJedis();   
  752.             boolean s = sjedis.hexists(key, fieid);  
  753.             returnJedis(sjedis);  
  754.             return s;  
  755.         }  
  756.   
  757.         /** 
  758.          * 返回hash中指定存储位置的值 
  759.          *  
  760.          * @param String key 
  761.          * @param String fieid 存储的名字 
  762.          * @return 存储对应的值 
  763.          * */  
  764.         public String hget(String key, String fieid) {  
  765.             //ShardedJedis sjedis = getShardedJedis();  
  766.             Jedis sjedis = getJedis();   
  767.             String s = sjedis.hget(key, fieid);  
  768.             returnJedis(sjedis);  
  769.             return s;  
  770.         }  
  771.   
  772.         public byte[] hget(byte[] key, byte[] fieid) {  
  773.             //ShardedJedis sjedis = getShardedJedis();  
  774.             Jedis sjedis = getJedis();   
  775.             byte[] s = sjedis.hget(key, fieid);  
  776.             returnJedis(sjedis);  
  777.             return s;  
  778.         }  
  779.   
  780.         /** 
  781.          * 以Map的形式返回hash中的存储和值 
  782.          * @param String    key 
  783.          * @return Map<Strinig,String> 
  784.          * */  
  785.         public Map<String, String> hgetAll(String key) {  
  786.             //ShardedJedis sjedis = getShardedJedis();  
  787.             Jedis sjedis = getJedis();   
  788.             Map<String, String> map = sjedis.hgetAll(key);  
  789.             returnJedis(sjedis);  
  790.             return map;  
  791.         }  
  792.   
  793.         /** 
  794.          * 添加一个对应关系 
  795.          * @param String  key 
  796.          * @param String fieid 
  797.          * @param String value 
  798.          * @return 状态码 1成功,0失败,fieid已存在将更新,也返回0 
  799.          * **/  
  800.         public long hset(String key, String fieid, String value) {  
  801.             Jedis jedis = getJedis();  
  802.             long s = jedis.hset(key, fieid, value);  
  803.             returnJedis(jedis);  
  804.             return s;  
  805.         }  
  806.   
  807.         public long hset(String key, String fieid, byte[] value) {  
  808.             Jedis jedis = getJedis();  
  809.             long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);  
  810.             returnJedis(jedis);  
  811.             return s;  
  812.         }  
  813.   
  814.         /** 
  815.          * 添加对应关系,只有在fieid不存在时才执行 
  816.          * @param String key 
  817.          * @param String fieid 
  818.          * @param String value 
  819.          * @return 状态码 1成功,0失败fieid已存 
  820.          * **/  
  821.         public long hsetnx(String key, String fieid, String value) {  
  822.             Jedis jedis = getJedis();  
  823.             long s = jedis.hsetnx(key, fieid, value);  
  824.             returnJedis(jedis);  
  825.             return s;  
  826.         }  
  827.   
  828.         /** 
  829.          * 获取hash中value的集合 
  830.          *  
  831.          * @param String 
  832.          *            key 
  833.          * @return List<String> 
  834.          * */  
  835.         public List<String> hvals(String key) {  
  836.             //ShardedJedis sjedis = getShardedJedis();  
  837.             Jedis sjedis = getJedis();   
  838.             List<String> list = sjedis.hvals(key);  
  839.             returnJedis(sjedis);  
  840.             return list;  
  841.         }  
  842.   
  843.         /** 
  844.          * 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型 
  845.          * @param String  key 
  846.          * @param String  fieid 存储位置 
  847.          * @param String long value 要增加的值,可以是负数 
  848.          * @return 增加指定数字后,存储位置的值 
  849.          * */  
  850.         public long hincrby(String key, String fieid, long value) {  
  851.             Jedis jedis = getJedis();  
  852.             long s = jedis.hincrBy(key, fieid, value);  
  853.             returnJedis(jedis);  
  854.             return s;  
  855.         }  
  856.   
  857.         /** 
  858.          * 返回指定hash中的所有存储名字,类似Map中的keySet方法 
  859.          * @param String key 
  860.          * @return Set<String> 存储名称的集合 
  861.          * */  
  862.         public Set<String> hkeys(String key) {  
  863.             //ShardedJedis sjedis = getShardedJedis();  
  864.             Jedis sjedis = getJedis();   
  865.             Set<String> set = sjedis.hkeys(key);  
  866.             returnJedis(sjedis);  
  867.             return set;  
  868.         }  
  869.   
  870.         /** 
  871.          * 获取hash中存储的个数,类似Map中size方法 
  872.          * @param String  key 
  873.          * @return long 存储的个数 
  874.          * */  
  875.         public long hlen(String key) {  
  876.             //ShardedJedis sjedis = getShardedJedis();  
  877.             Jedis sjedis = getJedis();    
  878.             long len = sjedis.hlen(key);  
  879.             returnJedis(sjedis);  
  880.             return len;  
  881.         }  
  882.   
  883.         /** 
  884.          * 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null 
  885.          * @param String  key 
  886.          * @param String ... fieids 存储位置 
  887.          * @return List<String> 
  888.          * */  
  889.         public List<String> hmget(String key, String... fieids) {  
  890.             //ShardedJedis sjedis = getShardedJedis();  
  891.             Jedis sjedis = getJedis();   
  892.             List<String> list = sjedis.hmget(key, fieids);  
  893.             returnJedis(sjedis);  
  894.             return list;  
  895.         }  
  896.   
  897.         public List<byte[]> hmget(byte[] key, byte[]... fieids) {  
  898.             //ShardedJedis sjedis = getShardedJedis();  
  899.             Jedis sjedis = getJedis();    
  900.             List<byte[]> list = sjedis.hmget(key, fieids);  
  901.             returnJedis(sjedis);  
  902.             return list;  
  903.         }  
  904.   
  905.         /** 
  906.          * 添加对应关系,如果对应关系已存在,则覆盖 
  907.          * @param Strin   key 
  908.          * @param Map <String,String> 对应关系 
  909.          * @return 状态,成功返回OK 
  910.          * */  
  911.         public String hmset(String key, Map<String, String> map) {  
  912.             Jedis jedis = getJedis();  
  913.             String s = jedis.hmset(key, map);  
  914.             returnJedis(jedis);  
  915.             return s;  
  916.         }  
  917.   
  918.         /** 
  919.          * 添加对应关系,如果对应关系已存在,则覆盖 
  920.          * @param Strin key 
  921.          * @param Map <String,String> 对应关系 
  922.          * @return 状态,成功返回OK 
  923.          * */  
  924.         public String hmset(byte[] key, Map<byte[], byte[]> map) {  
  925.             Jedis jedis = getJedis();  
  926.             String s = jedis.hmset(key, map);  
  927.             returnJedis(jedis);  
  928.             return s;  
  929.         }  
  930.   
  931.     }  
  932.       
  933.       
  934.     //*******************************************Strings*******************************************//  
  935.     public class Strings {  
  936.         /** 
  937.          * 根据key获取记录 
  938.          * @param String  key 
  939.          * @return 值 
  940.          * */  
  941.         public String get(String key) {  
  942.             //ShardedJedis sjedis = getShardedJedis();  
  943.             Jedis sjedis = getJedis();    
  944.             String value = sjedis.get(key);  
  945.             returnJedis(sjedis);  
  946.             return value;  
  947.         }  
  948.   
  949.         /** 
  950.          * 根据key获取记录 
  951.          * @param byte[] key 
  952.          * @return 值 
  953.          * */  
  954.         public byte[] get(byte[] key) {  
  955.             //ShardedJedis sjedis = getShardedJedis();  
  956.             Jedis sjedis = getJedis();    
  957.             byte[] value = sjedis.get(key);  
  958.             returnJedis(sjedis);  
  959.             return value;  
  960.         }  
  961.   
  962.         /** 
  963.          * 添加有过期时间的记录 
  964.          *  
  965.          * @param String  key 
  966.          * @param int seconds 过期时间,以秒为单位 
  967.          * @param String value 
  968.          * @return String 操作状态 
  969.          * */  
  970.         public String setEx(String key, int seconds, String value) {  
  971.             Jedis jedis = getJedis();  
  972.             String str = jedis.setex(key, seconds, value);  
  973.             returnJedis(jedis);  
  974.             return str;  
  975.         }  
  976.   
  977.         /** 
  978.          * 添加有过期时间的记录 
  979.          *  
  980.          * @param String key 
  981.          * @param int seconds 过期时间,以秒为单位 
  982.          * @param String  value 
  983.          * @return String 操作状态 
  984.          * */  
  985.         public String setEx(byte[] key, int seconds, byte[] value) {  
  986.             Jedis jedis = getJedis();  
  987.             String str = jedis.setex(key, seconds, value);  
  988.             returnJedis(jedis);  
  989.             return str;  
  990.         }  
  991.   
  992.         /** 
  993.          * 添加一条记录,仅当给定的key不存在时才插入 
  994.          * @param String key 
  995.          * @param String value 
  996.          * @return long 状态码,1插入成功且key不存在,0未插入,key存在 
  997.          * */  
  998.         public long setnx(String key, String value) {  
  999.             Jedis jedis = getJedis();  
  1000.             long str = jedis.setnx(key, value);  
  1001.             returnJedis(jedis);  
  1002.             return str;  
  1003.         }  
  1004.   
  1005.         /** 
  1006.          * 添加记录,如果记录已存在将覆盖原有的value 
  1007.          * @param String key 
  1008.          * @param String value 
  1009.          * @return 状态码 
  1010.          * */  
  1011.         public String set(String key, String value) {  
  1012.             return set(SafeEncoder.encode(key), SafeEncoder.encode(value));  
  1013.         }  
  1014.   
  1015.         /** 
  1016.          * 添加记录,如果记录已存在将覆盖原有的value 
  1017.          * @param String  key 
  1018.          * @param String value 
  1019.          * @return 状态码 
  1020.          * */  
  1021.         public String set(String key, byte[] value) {  
  1022.             return set(SafeEncoder.encode(key), value);  
  1023.         }  
  1024.   
  1025.         /** 
  1026.          * 添加记录,如果记录已存在将覆盖原有的value 
  1027.          * @param byte[] key 
  1028.          * @param byte[] value 
  1029.          * @return 状态码 
  1030.          * */  
  1031.         public String set(byte[] key, byte[] value) {  
  1032.             Jedis jedis = getJedis();  
  1033.             String status = jedis.set(key, value);  
  1034.             returnJedis(jedis);  
  1035.             return status;  
  1036.         }  
  1037.   
  1038.         /** 
  1039.          * 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/> 
  1040.          * 例:String str1="123456789";<br/> 
  1041.          * 对str1操作后setRange(key,4,0000),str1="123400009"; 
  1042.          * @param String  key 
  1043.          * @param long offset 
  1044.          * @param String  value 
  1045.          * @return long value的长度 
  1046.          * */  
  1047.         public long setRange(String key, long offset, String value) {  
  1048.             Jedis jedis = getJedis();  
  1049.             long len = jedis.setrange(key, offset, value);  
  1050.             returnJedis(jedis);  
  1051.             return len;  
  1052.         }  
  1053.   
  1054.         /** 
  1055.          * 在指定的key中追加value 
  1056.          * @param String  key 
  1057.          * @param String value 
  1058.          * @return long 追加后value的长度 
  1059.          * **/  
  1060.         public long append(String key, String value) {  
  1061.             Jedis jedis = getJedis();  
  1062.             long len = jedis.append(key, value);  
  1063.             returnJedis(jedis);  
  1064.             return len;  
  1065.         }  
  1066.   
  1067.         /** 
  1068.          * 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用 
  1069.          * @param String key 
  1070.          * @param long number 要减去的值 
  1071.          * @return long 减指定值后的值 
  1072.          * */  
  1073.         public long decrBy(String key, long number) {  
  1074.             Jedis jedis = getJedis();  
  1075.             long len = jedis.decrBy(key, number);  
  1076.             returnJedis(jedis);  
  1077.             return len;  
  1078.         }  
  1079.   
  1080.         /** 
  1081.          * <b>可以作为获取唯一id的方法</b><br/> 
  1082.          * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用 
  1083.          * @param String  key 
  1084.          * @param long number 要减去的值 
  1085.          * @return long 相加后的值 
  1086.          * */  
  1087.         public long incrBy(String key, long number) {  
  1088.             Jedis jedis = getJedis();  
  1089.             long len = jedis.incrBy(key, number);  
  1090.             returnJedis(jedis);  
  1091.             return len;  
  1092.         }  
  1093.   
  1094.         /** 
  1095.          * 对指定key对应的value进行截取  
  1096.          * @param String   key 
  1097.          * @param long startOffset 开始位置(包含) 
  1098.          * @param long endOffset 结束位置(包含) 
  1099.          * @return String 截取的值 
  1100.          * */  
  1101.         public String getrange(String key, long startOffset, long endOffset) {  
  1102.             //ShardedJedis sjedis = getShardedJedis();  
  1103.             Jedis sjedis = getJedis();    
  1104.             String value = sjedis.getrange(key, startOffset, endOffset);  
  1105.             returnJedis(sjedis);   
  1106.             return value;  
  1107.         }  
  1108.   
  1109.         /** 
  1110.          * 获取并设置指定key对应的value<br/> 
  1111.          * 如果key存在返回之前的value,否则返回null 
  1112.          * @param String  key 
  1113.          * @param String value 
  1114.          * @return String 原始value或null 
  1115.          * */  
  1116.         public String getSet(String key, String value) {  
  1117.             Jedis jedis = getJedis();  
  1118.             String str = jedis.getSet(key, value);  
  1119.             returnJedis(jedis);  
  1120.             return str;  
  1121.         }  
  1122.   
  1123.         /** 
  1124.          * 批量获取记录,如果指定的key不存在返回List的对应位置将是null 
  1125.          * @param String keys 
  1126.          * @return List<String> 值得集合 
  1127.          * */  
  1128.         public List<String> mget(String... keys) {  
  1129.             Jedis jedis = getJedis();  
  1130.             List<String> str = jedis.mget(keys);  
  1131.             returnJedis(jedis);  
  1132.             return str;  
  1133.         }  
  1134.   
  1135.         /** 
  1136.          * 批量存储记录 
  1137.          * @param String keysvalues 例:keysvalues="key1","value1","key2","value2"; 
  1138.          * @return String 状态码  
  1139.          * */  
  1140.         public String mset(String... keysvalues) {  
  1141.             Jedis jedis = getJedis();  
  1142.             String str = jedis.mset(keysvalues);  
  1143.             returnJedis(jedis);  
  1144.             return str;  
  1145.         }  
  1146.   
  1147.         /** 
  1148.          * 获取key对应的值的长度 
  1149.          * @param String key 
  1150.          * @return value值得长度 
  1151.          * */  
  1152.         public long strlen(String key) {  
  1153.             Jedis jedis = getJedis();  
  1154.             long len = jedis.strlen(key);  
  1155.             returnJedis(jedis);  
  1156.             return len;  
  1157.         }  
  1158.     }  
  1159.       
  1160.       
  1161.     //*******************************************Lists*******************************************//  
  1162.     public class Lists {  
  1163.         /** 
  1164.          * List长度 
  1165.          * @param String key 
  1166.          * @return 长度 
  1167.          * */  
  1168.         public long llen(String key) {  
  1169.             return llen(SafeEncoder.encode(key));  
  1170.         }  
  1171.   
  1172.         /** 
  1173.          * List长度 
  1174.          * @param byte[] key 
  1175.          * @return 长度 
  1176.          * */  
  1177.         public long llen(byte[] key) {  
  1178.             //ShardedJedis sjedis = getShardedJedis();  
  1179.             Jedis sjedis = getJedis();    
  1180.             long count = sjedis.llen(key);  
  1181.             returnJedis(sjedis);  
  1182.             return count;  
  1183.         }  
  1184.   
  1185.         /** 
  1186.          * 覆盖操作,将覆盖List中指定位置的值 
  1187.          * @param byte[] key 
  1188.          * @param int index 位置 
  1189.          * @param byte[] value 值 
  1190.          * @return 状态码 
  1191.          * */  
  1192.         public String lset(byte[] key, int index, byte[] value) {  
  1193.             Jedis jedis = getJedis();  
  1194.             String status = jedis.lset(key, index, value);  
  1195.             returnJedis(jedis);  
  1196.             return status;  
  1197.         }  
  1198.   
  1199.         /** 
  1200.          * 覆盖操作,将覆盖List中指定位置的值 
  1201.          * @param key 
  1202.          * @param int index 位置 
  1203.          * @param String  value 值 
  1204.          * @return 状态码 
  1205.          * */  
  1206.         public String lset(String key, int index, String value) {  
  1207.             return lset(SafeEncoder.encode(key), index,  
  1208.                     SafeEncoder.encode(value));  
  1209.         }  
  1210.   
  1211.         /** 
  1212.          * 在value的相对位置插入记录 
  1213.          * @param key 
  1214.          * @param LIST_POSITION   前面插入或后面插入 
  1215.          * @param String pivot 相对位置的内容 
  1216.          * @param String value 插入的内容 
  1217.          * @return 记录总数 
  1218.          * */  
  1219.         public long linsert(String key, LIST_POSITION where, String pivot,  
  1220.                 String value) {  
  1221.             return linsert(SafeEncoder.encode(key), where,  
  1222.                     SafeEncoder.encode(pivot), SafeEncoder.encode(value));  
  1223.         }  
  1224.   
  1225.         /** 
  1226.          * 在指定位置插入记录 
  1227.          * @param String key 
  1228.          * @param LIST_POSITION 前面插入或后面插入 
  1229.          * @param byte[] pivot 相对位置的内容 
  1230.          * @param byte[] value 插入的内容 
  1231.          * @return 记录总数 
  1232.          * */  
  1233.         public long linsert(byte[] key, LIST_POSITION where, byte[] pivot,  
  1234.                 byte[] value) {  
  1235.             Jedis jedis = getJedis();  
  1236.             long count = jedis.linsert(key, where, pivot, value);  
  1237.             returnJedis(jedis);  
  1238.             return count;  
  1239.         }  
  1240.   
  1241.         /** 
  1242.          * 获取List中指定位置的值 
  1243.          * @param String  key 
  1244.          * @param int index 位置  
  1245.          * @return 值 
  1246.          * **/  
  1247.         public String lindex(String key, int index) {  
  1248.             return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));  
  1249.         }  
  1250.   
  1251.         /** 
  1252.          * 获取List中指定位置的值  
  1253.          * @param byte[] key 
  1254.          * @param int index 位置 
  1255.          * @return 值 
  1256.          * **/  
  1257.         public byte[] lindex(byte[] key, int index) {   
  1258.             //ShardedJedis sjedis = getShardedJedis();  
  1259.             Jedis sjedis = getJedis();    
  1260.             byte[] value = sjedis.lindex(key, index);  
  1261.             returnJedis(sjedis);  
  1262.             return value;  
  1263.         }  
  1264.   
  1265.         /** 
  1266.          * 将List中的第一条记录移出List 
  1267.          * @param String key 
  1268.          * @return 移出的记录  
  1269.          * */  
  1270.         public String lpop(String key) {  
  1271.             return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));  
  1272.         }  
  1273.   
  1274.         /** 
  1275.          * 将List中的第一条记录移出List 
  1276.          * @param byte[] key 
  1277.          * @return 移出的记录 
  1278.          * */  
  1279.         public byte[] lpop(byte[] key) {  
  1280.             Jedis jedis = getJedis();  
  1281.             byte[] value = jedis.lpop(key);  
  1282.             returnJedis(jedis);  
  1283.             return value;  
  1284.         }  
  1285.   
  1286.         /** 
  1287.          * 将List中最后第一条记录移出List 
  1288.          *  
  1289.          * @param byte[] key 
  1290.          * @return 移出的记录 
  1291.          * */  
  1292.         public String rpop(String key) {  
  1293.             Jedis jedis = getJedis();  
  1294.             String value = jedis.rpop(key);  
  1295.             returnJedis(jedis);  
  1296.             return value;  
  1297.         }  
  1298.   
  1299.         /** 
  1300.          * 向List尾部追加记录 
  1301.          * @param String key 
  1302.          * @param String value 
  1303.          * @return 记录总数 
  1304.          * */  
  1305.         public long lpush(String key, String value) {  
  1306.             return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));  
  1307.         }  
  1308.   
  1309.         /** 
  1310.          * 向List头部追加记录 
  1311.          * @param String  key 
  1312.          * @param String  value 
  1313.          * @return 记录总数 
  1314.          * */  
  1315.         public long rpush(String key, String value) {  
  1316.             Jedis jedis = getJedis();  
  1317.             long count = jedis.rpush(key, value);  
  1318.             returnJedis(jedis);  
  1319.             return count;  
  1320.         }  
  1321.   
  1322.         /** 
  1323.          * 向List头部追加记录 
  1324.          * @param String key 
  1325.          * @param String value 
  1326.          * @return 记录总数 
  1327.          * */  
  1328.         public long rpush(byte[] key, byte[] value) {  
  1329.             Jedis jedis = getJedis();  
  1330.             long count = jedis.rpush(key, value);  
  1331.             returnJedis(jedis);  
  1332.             return count;  
  1333.         }  
  1334.   
  1335.         /** 
  1336.          * 向List中追加记录 
  1337.          * @param byte[] key 
  1338.          * @param byte[] value 
  1339.          * @return 记录总数 
  1340.          * */  
  1341.         public long lpush(byte[] key, byte[] value) {  
  1342.             Jedis jedis = getJedis();  
  1343.             long count = jedis.lpush(key, value);  
  1344.             returnJedis(jedis);  
  1345.             return count;  
  1346.         }  
  1347.   
  1348.         /** 
  1349.          * 获取指定范围的记录,可以做为分页使用 
  1350.          * @param String key 
  1351.          * @param long start 
  1352.          * @param long end 
  1353.          * @return List 
  1354.          * */  
  1355.         public List<String> lrange(String key, long start, long end) {  
  1356.             //ShardedJedis sjedis = getShardedJedis();  
  1357.             Jedis sjedis = getJedis();     
  1358.             List<String> list = sjedis.lrange(key, start, end);  
  1359.             returnJedis(sjedis);  
  1360.             return list;  
  1361.         }  
  1362.   
  1363.         /** 
  1364.          * 获取指定范围的记录,可以做为分页使用 
  1365.          * @param byte[] key 
  1366.          * @param int start 
  1367.          * @param int end 如果为负数,则尾部开始计算 
  1368.          * @return List 
  1369.          * */  
  1370.         public List<byte[]> lrange(byte[] key, int start, int end) {  
  1371.             //ShardedJedis sjedis = getShardedJedis();  
  1372.             Jedis sjedis = getJedis();     
  1373.             List<byte[]> list = sjedis.lrange(key, start, end);  
  1374.             returnJedis(sjedis);  
  1375.             return list;  
  1376.         }  
  1377.   
  1378.         /** 
  1379.          * 删除List中c条记录,被删除的记录值为value 
  1380.          * @param byte[] key 
  1381.          * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录 
  1382.          * @param byte[] value 要匹配的值 
  1383.          * @return 删除后的List中的记录数 
  1384.          * */  
  1385.         public long lrem(byte[] key, int c, byte[] value) {  
  1386.             Jedis jedis = getJedis();  
  1387.             long count = jedis.lrem(key, c, value);  
  1388.             returnJedis(jedis);  
  1389.             return count;  
  1390.         }  
  1391.   
  1392.         /** 
  1393.          * 删除List中c条记录,被删除的记录值为value 
  1394.          * @param String key 
  1395.          * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录 
  1396.          * @param String value 要匹配的值 
  1397.          * @return 删除后的List中的记录数 
  1398.          * */  
  1399.         public long lrem(String key, int c, String value) {  
  1400.             return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));  
  1401.         }  
  1402.   
  1403.         /** 
  1404.          * 算是删除吧,只保留start与end之间的记录 
  1405.          * @param byte[] key 
  1406.          * @param int start 记录的开始位置(0表示第一条记录) 
  1407.          * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推) 
  1408.          * @return 执行状态码 
  1409.          * */  
  1410.         public String ltrim(byte[] key, int start, int end) {  
  1411.             Jedis jedis = getJedis();  
  1412.             String str = jedis.ltrim(key, start, end);  
  1413.             returnJedis(jedis);  
  1414.             return str;  
  1415.         }  
  1416.   
  1417.         /**  
  1418.          * 算是删除吧,只保留start与end之间的记录 
  1419.          * @param String key  
  1420.          * @param int start 记录的开始位置(0表示第一条记录) 
  1421.          * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推) 
  1422.          * @return 执行状态码 
  1423.          * */  
  1424.         public String ltrim(String key, int start, int end) {  
  1425.             return ltrim(SafeEncoder.encode(key), start, end);  
  1426.         }  
  1427.     }   
  1428.       
  1429.     public static void main(String[] args) {  
  1430.         JedisUtil jedisUtil= JedisUtil.getInstance();    
  1431.         JedisUtil.Strings strings=jedisUtil.new Strings();  
  1432.         strings.set("nnn""nnnn");   
  1433.         System.out.println("-----"+strings.get("nnn"));     
  1434.           
  1435.         Jedis jedis=JedisUtil.getInstance().getJedis();   
  1436.         for (int i = 0; i < 10; i++) {   
  1437.             jedis.set("test""test");   
  1438.             System.out.println(i+"=="+jedis.get("test"));    
  1439.           
  1440.         }  
  1441.         JedisUtil.getInstance().returnJedis(jedis);     
  1442.     }  
  1443.           
  1444. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值