IT忍者神龟之Redis操作工具类封装

来源:互联网 发布:openstack的网络架构 编辑:程序博客网 时间:2024/04/28 18:24

Redis操作字符串工具类封装,Redis工具类封装

 

Java代码  收藏代码
  1. package com.lqy.spring.redis;  
  2.   
  3. import redis.clients.jedis.Jedis;  
  4. import redis.clients.jedis.JedisPool;  
  5.   
  6. public class RedisPoolUtils {  
  7.     private static final JedisPool jedisPool = new JedisPool("127.0.0.1"6379);  
  8.         //spring配置使用  
  9.     //private static final JedisPool jedisPool = (JedisPool) SpringUtils.getBeanById("jedisPool");  
  10.       
  11.     /**  
  12.      * 成功,"OK"  
  13.      */    
  14.     private static final String SUCCESS_OK = "OK";    
  15.     /**  
  16.      * 成功,1L  
  17.      */    
  18.     private static final Long SUCCESS_STATUS_LONG = 1L;    
  19.     /**  
  20.      * 只用key不存在时才设置。Only set the key if it does not already exist  
  21.      */    
  22.     private static final String NX = "NX";    
  23.     /**  
  24.      * XX -- 只有key存在时才设置。和NX相反。Only set the key if it already exist.  
  25.      */    
  26.     private static final String XX = "XX";    
  27.     /**  
  28.      * EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds  
  29.      */    
  30.     private static final String EX = "EX";    
  31.     /**  
  32.      * EX|PX, 时间单位,EX是秒,PX是毫秒。expire time units: EX = seconds; PX = milliseconds  
  33.      */    
  34.     //private static final String PX = "PX";    
  35.         
  36.     /**  
  37.      * 成功返回true  
  38.      * @param key  
  39.      * @param value  
  40.      * @return  
  41.      */    
  42.     public static boolean set(String key, String value){    
  43.         Jedis jedis = null;  
  44.         try {  
  45.             jedis = jedisPool.getResource();    
  46.             String statusCode = jedis.set(key, value);    
  47.             if(SUCCESS_OK.equalsIgnoreCase(statusCode)){    
  48.                 return true;    
  49.             }   
  50.         }catch (Exception e) {  
  51.             e.printStackTrace();  
  52.         }finally{  
  53.             if(jedis != null){  
  54.                 jedis.close();  
  55.             }  
  56.         }  
  57.         return false;    
  58.     }    
  59.         
  60.     /**  
  61.      * 返回值  
  62.      * @param key  
  63.      * @return  
  64.      */    
  65.     public static String get(String key){  
  66.         Jedis jedis = null;  
  67.         try {  
  68.             jedis = jedisPool.getResource();    
  69.             return jedis.get(key);    
  70.         }catch (Exception e) {  
  71.             e.printStackTrace();  
  72.         }finally{  
  73.             if(jedis != null){  
  74.                 jedis.close();  
  75.             }  
  76.         }  
  77.         return null;  
  78.     }    
  79.         
  80.     /**  
  81.      * 设置key值和过期时间  
  82.      * @param key  
  83.      * @param value  
  84.      * @param seconds 秒数,不能小于0  
  85.      * @return  
  86.      */    
  87.     public static boolean setByTime(String key, String value, int seconds){    
  88.         if(seconds < 0){    
  89.             return false;    
  90.         }   
  91.         Jedis jedis = null;  
  92.         try {  
  93.             jedis = jedisPool.getResource();    
  94.             String statusCode = jedis.setex(key, seconds, value);    
  95.             if(SUCCESS_OK.equalsIgnoreCase(statusCode)){    
  96.                 return true;    
  97.             }   
  98.         }catch (Exception e) {  
  99.             e.printStackTrace();  
  100.         }finally{  
  101.             if(jedis != null){  
  102.                 jedis.close();  
  103.             }  
  104.         }  
  105.         return false;    
  106.     }    
  107.         
  108.     /**  
  109.      *   
  110.      * @param key  
  111.      * @param value  
  112.      * @param nxxx NX|XX  是否存在  
  113.      * <li>NX -- Only set the key if it does not already exist.</li>  
  114.      * <li>XX -- Only set the key if it already exist.</li>  
  115.      * @param expx EX|PX, expire time units ,时间单位格式,秒或毫秒  
  116.      * <li>EX = seconds;</li>  
  117.      * <li>PX = milliseconds</li>  
  118.      * @param time expire time in the units of expx,时间(long型),不能小于0  
  119.      * @return  
  120.      */    
  121.     public static boolean set(String key, String value,     
  122.             String nxxx, String expx, long time){   
  123.         if(time < 0){    
  124.             return false;    
  125.         }  
  126.           
  127.         Jedis jedis = null;  
  128.         try {  
  129.             jedis = jedisPool.getResource();    
  130.             String statusCode = jedis.set(key, value, nxxx, expx, time);    
  131.             if(SUCCESS_OK.equalsIgnoreCase(statusCode)){    
  132.                 return true;    
  133.             }    
  134.         }catch (Exception e) {  
  135.             e.printStackTrace();  
  136.         }finally{  
  137.             if(jedis != null){  
  138.                 jedis.close();  
  139.             }  
  140.         }  
  141.         return false;  
  142.     }    
  143.         
  144.     /**  
  145.      * 设置key  
  146.      * @param key  
  147.      * @param value  
  148.      * @param nxxx NX|XX 是否需要存在  
  149.      * <li>NX -- Only set the key if it does not already exist.</li>   
  150.      * <li>XX -- Only set the key if it already exist.</li>   
  151.      * @return  
  152.      */    
  153.     public static boolean set(String key, String value,     
  154.             String nxxx){    
  155.         Jedis jedis = null;  
  156.         try {  
  157.             jedis = jedisPool.getResource();    
  158.             String statusCode = jedis.set(key, value, nxxx);    
  159.             if(SUCCESS_OK.equalsIgnoreCase(statusCode)){    
  160.                 return true;    
  161.             }   
  162.         }catch (Exception e) {  
  163.             e.printStackTrace();  
  164.         }finally{  
  165.             if(jedis != null){  
  166.                 jedis.close();  
  167.             }  
  168.         }  
  169.         return false;  
  170.     }    
  171.         
  172.     /**  
  173.      * 当key不存在时,设置值,成功返回true  
  174.      * @param key  
  175.      * @param value  
  176.      * @return  
  177.      */    
  178.     public static boolean setIfNotExists(String key, String value){    
  179.         Jedis jedis = null;  
  180.         try {  
  181.             jedis = jedisPool.getResource();    
  182.             Long statusCode = jedis.setnx(key, value);    
  183.             if(SUCCESS_STATUS_LONG == statusCode){    
  184.                 return true;    
  185.             }    
  186.         }catch (Exception e) {  
  187.             e.printStackTrace();  
  188.         }finally{  
  189.             if(jedis != null){  
  190.                 jedis.close();  
  191.             }  
  192.         }  
  193.         return false;  
  194.     }    
  195.         
  196.     /**  
  197.      * 当key不存在时,设置值,成功返回true,同setIfNotExists  
  198.      * @param key  
  199.      * @param value  
  200.      * @return  
  201.      */    
  202.     public static boolean setNX(String key, String value){    
  203.         return setIfNotExists(key, value);    
  204.     }    
  205.         
  206.     /**  
  207.      * 仅当key不存在时则设置值,成功返回true,存在时不设置值  
  208.      * @param key  
  209.      * @param value  
  210.      * @param seconds,秒数,不能小于0  
  211.      * @return  
  212.      */    
  213.     public static boolean setIfNotExists(String key, String value, long seconds){    
  214.         if(seconds < 0){    
  215.             return false;    
  216.         }    
  217.         return set(key, value, NX, EX, seconds);    
  218.     }    
  219.         
  220.     /**  
  221.      * 仅当key不存在时则设置值,成功返回true,存在时不设置值,同setIfNotExists(key, value, seconds)  
  222.      * @param key  
  223.      * @param value  
  224.      * @param seconds  
  225.      * @return  
  226.      */    
  227.     public static boolean setNX(String key, String value, Long seconds){    
  228.         return setIfNotExists(key, value, seconds);    
  229.     }    
  230.         
  231.     /**  
  232.      * 当key存在时则设置值,成功返回true,不存在不设置值  
  233.      * @param key  
  234.      * @param value  
  235.      * @return  
  236.      */    
  237.     public static boolean setIfExists(String key, String value){    
  238.         return set(key, value, XX);    
  239.     }    
  240.         
  241.     /**  
  242.      * 当key存在时则设置值,成功返回true,不存在不设置值,同setIfExists  
  243.      * @param key  
  244.      * @param value  
  245.      * @return  
  246.      */    
  247.     public static boolean setXX(String key, String value){    
  248.         return setIfExists(key, value);    
  249.     }    
  250.         
  251.     /**  
  252.      * 仅当key存在时则设置值,成功返回true,不存在不设置值  
  253.      * @param key  
  254.      * @param value  
  255.      * @param seconds,秒数,不能小于0  
  256.      * @return  
  257.      */    
  258.     public static boolean setIfExists(String key, String value, long seconds){    
  259.         if(seconds < 0){    
  260.             return false;    
  261.         }    
  262.         return set(key, value, XX, EX, seconds);    
  263.     }    
  264.         
  265.     /**  
  266.      * 仅当key存在时则设置值,成功返回true,不存在不设置值  
  267.      * @param key  
  268.      * @param value  
  269.      * @param seconds,秒数,不能小于0  
  270.      * @return  
  271.      */    
  272.     public static boolean setXX(String key, String value, long seconds){    
  273.         return setIfExists(key, value, seconds);    
  274.     }    
  275.         
  276.     /**  
  277.      * 设置超期时间  
  278.      * @param key  
  279.      * @param seconds 为Null时,将会马上过期。可以设置-1,0,表示马上过期  
  280.      * @return  
  281.      */    
  282.     public static boolean setTime(String key, Integer seconds){   
  283.         Jedis jedis = null;  
  284.         try {  
  285.             if(seconds == null){    
  286.                 seconds = -1;    
  287.             }  
  288.             jedis = jedisPool.getResource();  
  289.             Long statusCode = jedis.expire(key, seconds);    
  290.             if(SUCCESS_STATUS_LONG == statusCode){    
  291.                 return true;    
  292.             }   
  293.         }catch (Exception e) {  
  294.             e.printStackTrace();  
  295.         }finally{  
  296.             if(jedis != null){  
  297.                 jedis.close();  
  298.             }  
  299.         }  
  300.         return false;  
  301.     }    
  302.         
  303.     /**  
  304.      * 设置超期时间  
  305.      * @param key  
  306.      * @param seconds 为Null时,将会马上过期。可以设置-1,0,表示马上过期  
  307.      * @return  
  308.      */    
  309.     public static boolean setOutTime(String key, Integer seconds){    
  310.         return setTime(key, seconds);    
  311.     }    
  312.         
  313.     /**  
  314.      * 设置超期时间  
  315.      * @param key  
  316.      * @param seconds 秒数,为Null时,将会马上过期。可以设置-1,0,表示马上过期  
  317.      * @return  
  318.      */    
  319.     public static boolean expire(String key, Integer seconds){    
  320.         return setTime(key, seconds);    
  321.     }    
  322.         
  323.     /**  
  324.      * 判断key是否存在,存在返回true  
  325.      * @param key  
  326.      * @return  
  327.      */    
  328.     public static boolean exists(String key){    
  329.         Jedis jedis = null;  
  330.         try {  
  331.             jedis = jedisPool.getResource();  
  332.             return jedis.exists(key);  
  333.         }catch (Exception e) {  
  334.             e.printStackTrace();  
  335.         }finally{  
  336.             if(jedis != null){  
  337.                 jedis.close();  
  338.             }  
  339.         }  
  340.         return false;  
  341.     }    
  342.         
  343.     /**  
  344.      * 判断key是否存在,存在返回true  
  345.      * @param key  
  346.      * @return  
  347.      */    
  348.     public static boolean isExists(String key){    
  349.         return exists(key);    
  350.     }    
  351.         
  352.     /**  
  353.      * 将key设置为永久  
  354.      * @param key  
  355.      * @return  
  356.      */    
  357.     public static boolean persist(String key){  
  358.         Jedis jedis = null;  
  359.         try {  
  360.             jedis = jedisPool.getResource();  
  361.             long time = getTime(key);    
  362.             if(time == -1){    
  363.                 return true;    
  364.             }  
  365.             //已经是永久的,返回0    
  366.             Long statusCode = jedis.persist(key);    
  367.             jedis.close();    
  368.             if(SUCCESS_STATUS_LONG == statusCode){    
  369.                 return true;    
  370.             }   
  371.         }catch (Exception e) {  
  372.             e.printStackTrace();  
  373.         }finally{  
  374.             if(jedis != null){  
  375.                 jedis.close();  
  376.             }  
  377.         }  
  378.         return false;  
  379.     }    
  380.         
  381.     /**  
  382.      * 获取剩余时间(秒)  
  383.      * @param key  
  384.      * @return  
  385.      */    
  386.     public static Long getTime(String key){  
  387.         Jedis jedis = null;  
  388.         try {  
  389.             jedis = jedisPool.getResource();  
  390.             return jedis.ttl(key);  
  391.         }catch (Exception e) {  
  392.             e.printStackTrace();  
  393.         }finally{  
  394.             if(jedis != null){  
  395.                 jedis.close();  
  396.             }  
  397.         }  
  398.         return -1L;    
  399.     }    
  400.         
  401.     /**  
  402.      * 获取剩余时间(秒)  
  403.      * @param key  
  404.      * @return  
  405.      */    
  406.     public static Long Ttl(String key){    
  407.         return getTime(key);    
  408.     }    
  409.         
  410.     /**  
  411.      * 随机获取一个key  
  412.      * @return  
  413.      */    
  414.     public static String randomKey(){  
  415.         Jedis jedis = null;  
  416.         try {  
  417.             jedis = jedisPool.getResource();  
  418.             return jedis.randomKey();  
  419.         }catch (Exception e) {  
  420.             e.printStackTrace();  
  421.         }finally{  
  422.             if(jedis != null){  
  423.                 jedis.close();  
  424.             }  
  425.         }  
  426.         return null;  
  427.     }    
  428.         
  429.     /**  
  430.      * 随机获取一个key  
  431.      * @return  
  432.      */    
  433.     public static String random(){    
  434.         return randomKey();    
  435.     }    
  436.         
  437.     /**  
  438.      * 修改 key 的名称,成功返回true,如果不存在该key,则会抛错:ERR no such key  
  439.      * 注:如果newKey已经存在,则会进行覆盖。建议使用renameNX  
  440.      * @param oldkey 原来的key  
  441.      * @param newKey 新的key  
  442.      * @return  
  443.      */    
  444.     public static boolean rename(String oldkey, String newKey){  
  445.         Jedis jedis = null;  
  446.         try {  
  447.             jedis = jedisPool.getResource();  
  448.             String statusCode = jedis.rename(oldkey, newKey);    
  449.             if(SUCCESS_OK.equalsIgnoreCase(statusCode)){    
  450.                 return true;    
  451.             }   
  452.         }catch (Exception e) {  
  453.             e.printStackTrace();  
  454.         }finally{  
  455.             if(jedis != null){  
  456.                 jedis.close();  
  457.             }  
  458.         }  
  459.         return false;  
  460.     }    
  461.         
  462.     /**  
  463.      * 仅当 newkey 不存在时,将 key 改名为 newkey 。成功返回true  
  464.      * @param oldkey  
  465.      * @param newKey  
  466.      * @return  
  467.      */    
  468.     public static boolean renameNX(String oldkey, String newKey){   
  469.         Jedis jedis = null;  
  470.         try {  
  471.             jedis = jedisPool.getResource();  
  472.             Long statusCode = jedis.renamenx(oldkey, newKey);    
  473.             if(SUCCESS_STATUS_LONG == statusCode){    
  474.                 return true;    
  475.             }  
  476.         }catch (Exception e) {  
  477.             e.printStackTrace();  
  478.         }finally{  
  479.             if(jedis != null){  
  480.                 jedis.close();  
  481.             }  
  482.         }  
  483.         return false;  
  484.     }    
  485.         
  486.     /**  
  487.      * 仅当 newkey 不存在时,将 key 改名为 newkey 。成功返回true  
  488.      * @param oldkey  
  489.      * @param newKey  
  490.      * @return  
  491.      */    
  492.     public static boolean renameIfNotExists(String oldkey, String newKey){    
  493.         return renameNX(oldkey, newKey);    
  494.     }    
  495.         
  496.     /**  
  497.      * 返回 key 所储存的值的类型。  
  498.      * @param key  
  499.      * @return  
  500.      */    
  501.     public static String type(String key){    
  502.         Jedis jedis = null;  
  503.         try {  
  504.             jedis = jedisPool.getResource();  
  505.             return jedis.type(key);    
  506.         }catch (Exception e) {  
  507.             e.printStackTrace();  
  508.         }finally{  
  509.             if(jedis != null){  
  510.                 jedis.close();  
  511.             }  
  512.         }  
  513.         return null;  
  514.     }    
  515.         
  516.     /**  
  517.      * 返回 key 所储存的值的类型。  
  518.      * @param key  
  519.      * @return  
  520.      */    
  521.     public static String getType(String key){    
  522.         return type(key);    
  523.     }    
  524.         
  525.     /**  
  526.      * 删除key及值  
  527.      * @param key  
  528.      * @return  
  529.      */    
  530.     public static boolean del(String key){    
  531.         Jedis jedis = null;  
  532.         try {  
  533.             jedis = jedisPool.getResource();  
  534.             Long statusCode = jedis.del(key);    
  535.             if(SUCCESS_STATUS_LONG == statusCode){    
  536.                 return true;    
  537.             }  
  538.         }catch (Exception e) {  
  539.             e.printStackTrace();  
  540.         }finally{  
  541.             if(jedis != null){  
  542.                 jedis.close();  
  543.             }  
  544.         }  
  545.         return false;  
  546.     }    
  547.         
  548.     /**  
  549.      * 删除key及值  
  550.      * @param key  
  551.      * @return  
  552.      */    
  553.     public static boolean delete(String key){    
  554.         return del(key);    
  555.     }    
  556.         
  557.     /**  
  558.      * 删除key及值  
  559.      * @param key  
  560.      * @return  
  561.      */    
  562.     public static boolean remove(String key){    
  563.         return del(key);    
  564.     }    
  565.         
  566.     /**  
  567.      * 批量删除key及值  
  568.      * @param key  
  569.      * @return  
  570.      */    
  571.     public static boolean del(String[] keys){  
  572.         Jedis jedis = null;  
  573.         try {  
  574.             jedis = jedisPool.getResource();  
  575.             Long statusCode = jedis.del(keys);    
  576.             if(statusCode > 0){    
  577.                 return true;    
  578.             }  
  579.         }catch (Exception e) {  
  580.             e.printStackTrace();  
  581.         }finally{  
  582.             if(jedis != null){  
  583.                 jedis.close();  
  584.             }  
  585.         }  
  586.         return false;  
  587.     }    
  588.         
  589.     /**  
  590.      * 批量删除key及值  
  591.      * @param key  
  592.      * @return  
  593.      */    
  594.     public static boolean delete(String[] keys){    
  595.         return del(keys);    
  596.     }    
  597.         
  598.     /**  
  599.      * 批量删除key及值  
  600.      * @param key  
  601.      * @return  
  602.      */    
  603.     public static boolean remove(String[] keys){    
  604.         return del(keys);    
  605.     }   
  606.       
  607.       
  608.     public static void main(String[] args) {  
  609.         //System.out.println("statusCode="+statusCode);  
  610.         //System.out.println(set("wahaha1", "哇哈哈"));  
  611.         //System.out.println(setByTime("wahaha1", "哈哈", 1));  
  612.         //System.out.println(getTime("wahaha1"));  
  613.         /*System.out.println(set("wa", "哈哈", NX, EX, 10L)); 
  614.         System.out.println(set("wa", "哈哈60", XX, EX, 60L));*/  
  615.         //System.out.println(set("wa", "哈哈哈哈2", XX));  
  616.         //System.out.println(setIfNotExists("wa", "哈哈not"));  
  617.         //System.out.println(setIfNotExists("wa", "哈哈not", 30));  
  618.         //System.out.println(setIfExists("wahaha", "有就设置"));  
  619.         //System.out.println(setIfExists("wahaha", "有就设置", 60));  
  620.         //System.out.println(setTime("wa", -1));  
  621.         //System.out.println(exists("wa"));  
  622.         //System.out.println(isExists("wa"));  
  623.         //System.out.println(setByTime("wa", "30秒过期", 30));  
  624.         //System.out.println(persist("wa"));  
  625.         /*for(int i=0; i<30; i++){ 
  626.             System.out.println(randomKey()); 
  627.         }*/  
  628.         //System.out.println(rename("waa", "wa"));  
  629.         //System.out.println(renameNX("waa", "waa"));  
  630.         //System.out.println(getType("wa"));  
  631.         /*System.out.println(del("wa")); 
  632.         System.out.println(get("wa")); 
  633.         System.out.println(Ttl("wa"));*/  
  634.         System.out.println(del(new String[]{"a"}));  
  635.     }  
  636.       
  637.       
  638.   
  639.       
  640.       
  641. }  

 

Redis操作Hash工具类封装,Redis工具类封装


Java代码  收藏代码
  1. /**************************** redis Hash start***************************/  
  2.     /***Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象。***/  
  3.       
  4.     /** 
  5.      * 设置Hash的属性 
  6.      * @param key 
  7.      * @param field 
  8.      * @param value 
  9.      * @return 
  10.      */  
  11.     public static boolean hset(String key, String field, String value){  
  12.         if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){  
  13.             return false;  
  14.         }  
  15.         Jedis jedis = jedisPool.getResource();  
  16.         //If the field already exists, and the HSET just produced an update of the value, 0 is returned,   
  17.         //otherwise if a new field is created 1 is returned.  
  18.         Long statusCode = jedis.hset(key, field, value);  
  19.         jedis.close();  
  20.         if(statusCode > -1){  
  21.             return true;  
  22.         }  
  23.         return false;  
  24.     }  
  25.       
  26.     /** 
  27.      * 批量设置Hash的属性 
  28.      * @param key 
  29.      * @param fields 
  30.      * @param values 
  31.      * @return 
  32.      */  
  33.     public static boolean hmset(String key, String[] fields, String[] values){  
  34.         if(StrUtils.isBlank(key) || StrUtils.isEmptyArray(fields) || StrUtils.isEmptyArray(values)){  
  35.             return false;  
  36.         }  
  37.         Jedis jedis = jedisPool.getResource();  
  38.         Map<String, String> hash = new HashMap<String, String>();  
  39.         for (int i=0; i<fields.length; i++) {  
  40.             hash.put(fields[i], values[i]);  
  41.         }  
  42.         String statusCode = jedis.hmset(key, hash);  
  43.         jedis.close();  
  44.         if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
  45.             return true;  
  46.         }  
  47.         return false;  
  48.     }  
  49.       
  50.     /** 
  51.      * 批量设置Hash的属性 
  52.      * @param key 
  53.      * @param map Map<String, String> 
  54.      * @return 
  55.      */  
  56.     public static boolean hmset(String key, Map<String, String> map){  
  57.         if(StrUtils.isBlank(key) || StrUtils.isEmptyMap(map)){  
  58.             return false;  
  59.         }  
  60.         Jedis jedis = jedisPool.getResource();  
  61.         String statusCode = jedis.hmset(key, map);  
  62.         jedis.close();  
  63.         if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
  64.             return true;  
  65.         }  
  66.         return false;  
  67.     }  
  68.       
  69.     /** 
  70.      * 仅当field不存在时设置值,成功返回true 
  71.      * @param key 
  72.      * @param field 
  73.      * @param value 
  74.      * @return 
  75.      */  
  76.     public static boolean hsetNX(String key, String field, String value){  
  77.         if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){  
  78.             return false;  
  79.         }  
  80.         Jedis jedis = jedisPool.getResource();  
  81.         //If the field already exists, 0 is returned,    
  82.         //otherwise if a new field is created 1 is returned.  
  83.         Long statusCode = jedis.hsetnx(key, field, value);  
  84.         jedis.close();  
  85.         if(SUCCESS_STATUS_LONG == statusCode){  
  86.             return true;  
  87.         }  
  88.         return false;  
  89.     }  
  90.       
  91.     /** 
  92.      * 获取属性的值 
  93.      * @param key 
  94.      * @param field 
  95.      * @return 
  96.      */  
  97.     public static String hget(String key, String field){  
  98.         if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){  
  99.             return null;  
  100.         }  
  101.         Jedis jedis = jedisPool.getResource();  
  102.         String value = jedis.hget(key, field);  
  103.         jedis.close();  
  104.         return value;  
  105.     }  
  106.       
  107.     /** 
  108.      * 批量获取属性的值 
  109.      * @param key 
  110.      * @param fields String... 
  111.      * @return 
  112.      */  
  113.     public static List<String> hmget(String key, String... fields){  
  114.         if(StrUtils.isBlank(key) || StrUtils.isNull(fields)){  
  115.             return null;  
  116.         }  
  117.         Jedis jedis = jedisPool.getResource();  
  118.         List<String> values = jedis.hmget(key, fields);  
  119.         jedis.close();  
  120.         return values;  
  121.     }  
  122.       
  123.     /** 
  124.      * 获取在哈希表中指定 key 的所有字段和值 
  125.      * @param key 
  126.      * @return Map<String, String> 
  127.      */  
  128.     public static Map<String, String> hgetAll(String key){  
  129.         if(StrUtils.isBlank(key)){  
  130.             return null;  
  131.         }  
  132.         Jedis jedis = jedisPool.getResource();  
  133.         Map<String, String> map = jedis.hgetAll(key);  
  134.         jedis.close();  
  135.         return map;  
  136.     }  
  137.       
  138.     /** 
  139.      * 删除hash的属性 
  140.      * @param key 
  141.      * @param fields 
  142.      * @return 
  143.      */  
  144.     public static boolean hdel(String key, String... fields){  
  145.         if(StrUtils.isBlank(key) || StrUtils.isNull(fields)){  
  146.             return false;  
  147.         }  
  148.         Jedis jedis = jedisPool.getResource();  
  149.         jedis.hdel(key, fields);  
  150.         jedis.close();  
  151.         //System.out.println("statusCode="+statusCode);  
  152.         return true;  
  153.     }  
  154.       
  155.     /** 
  156.      * 查看哈希表 key 中,指定的字段是否存在。 
  157.      * @param key 
  158.      * @param field 
  159.      * @return 
  160.      */  
  161.     public static boolean hexists(String key, String field){  
  162.         if(StrUtils.isBlank(key) || StrUtils.isBlank(field)){  
  163.             return false;  
  164.         }  
  165.         Jedis jedis = jedisPool.getResource();  
  166.         boolean result = jedis.hexists(key, field);  
  167.         jedis.close();  
  168.         return result;  
  169.     }  
  170.       
  171.     /** 
  172.      * 为哈希表 key 中的指定字段的整数值加上增量 increment 。 
  173.      * @param key 
  174.      * @param field 
  175.      * @param increment 正负数、0、正整数 
  176.      * @return 
  177.      */  
  178.     public static long hincrBy(String key, String field, long increment){  
  179.         Jedis jedis = jedisPool.getResource();  
  180.         long result = jedis.hincrBy(key, field, increment);  
  181.         jedis.close();  
  182.         return result;  
  183.     }  
  184.       
  185.     /** 
  186.      * 为哈希表 key 中的指定字段的浮点数值加上增量 increment 。(注:如果field不存在时,会设置新的值) 
  187.      * @param key 
  188.      * @param field 
  189.      * @param increment,可以为负数、正数、0 
  190.      * @return 
  191.      */  
  192.     public static Double hincrByFloat(String key, String field, double increment){  
  193.         Jedis jedis = jedisPool.getResource();  
  194.         Double result = jedis.hincrByFloat(key, field, increment);  
  195.         jedis.close();  
  196.         return result;  
  197.     }  
  198.       
  199.     /** 
  200.      * 获取所有哈希表中的字段 
  201.      * @param key 
  202.      * @return Set<String> 
  203.      */  
  204.     public static Set<String> hkeys(String key){  
  205.         Jedis jedis = jedisPool.getResource();  
  206.         Set<String> result = jedis.hkeys(key);  
  207.         jedis.close();  
  208.         return result;  
  209.     }  
  210.       
  211.     /** 
  212.      * 获取哈希表中所有值 
  213.      * @param key 
  214.      * @return List<String> 
  215.      */  
  216.     public static List<String> hvals(String key){  
  217.         Jedis jedis = jedisPool.getResource();  
  218.         List<String> result = jedis.hvals(key);  
  219.         jedis.close();  
  220.         return result;  
  221.     }  
  222.       
  223.     /** 
  224.      * 获取哈希表中字段的数量,当key不存在时,返回0 
  225.      * @param key 
  226.      * @return 
  227.      */  
  228.     public static Long hlen(String key){  
  229.         Jedis jedis = jedisPool.getResource();  
  230.         Long result = jedis.hlen(key);  
  231.         jedis.close();  
  232.         return result;  
  233.     }  
  234.       
  235.     /** 
  236.      * 迭代哈希表中的键值对。 
  237.      * @param key 
  238.      * @param cursor 
  239.      * @return ScanResult<Entry<String, String>> 
  240.      */  
  241.     public static ScanResult<Entry<String, String>> hscan(String key, String cursor){  
  242.         Jedis jedis = jedisPool.getResource();  
  243.         ScanResult<Entry<String, String>> scanResult = jedis.hscan(key, cursor);   
  244.         jedis.close();  
  245.         //System.out.println(scanResult.getResult());  
  246.         return scanResult;  
  247.     }  
  248.       
  249.     /**************************** redis Hash end***************************/  

 

Redis操作List工具类封装,Java Redis List命令封装


Java代码  收藏代码
  1. /**************************** redis 列表List start***************************/  
  2.       
  3.     /** 
  4.      * 将一个值插入到列表头部,value可以重复,返回列表的长度 
  5.      * @param key 
  6.      * @param value String 
  7.      * @return 返回List的长度 
  8.      */  
  9.     public static Long lpush(String key, String value){  
  10.         Jedis jedis = jedisPool.getResource();  
  11.         Long length = jedis.lpush(key, value);  
  12.         jedis.close();  
  13.         return length;  
  14.     }  
  15.       
  16.     /** 
  17.      * 将多个值插入到列表头部,value可以重复 
  18.      * @param key 
  19.      * @param values String[] 
  20.      * @return 返回List的数量size 
  21.      */  
  22.     public static Long lpush(String key, String[] values){  
  23.         Jedis jedis = jedisPool.getResource();  
  24.         Long size = jedis.lpush(key, values);  
  25.         jedis.close();  
  26.         //System.out.println(result);  
  27.         return size;  
  28.     }  
  29.       
  30.     /** 
  31.      * 获取List列表 
  32.      * @param key 
  33.      * @param start long,开始索引 
  34.      * @param end long, 结束索引 
  35.      * @return List<String> 
  36.      */  
  37.     public static List<String> lrange(String key, long start, long end){  
  38.         Jedis jedis = jedisPool.getResource();  
  39.         List<String> list = jedis.lrange(key, start, end);  
  40.         jedis.close();  
  41.         return list;  
  42.     }  
  43.       
  44.     /** 
  45.      * 通过索引获取列表中的元素 
  46.      * @param key 
  47.      * @param index,索引,0表示最新的一个元素 
  48.      * @return String 
  49.      */  
  50.     public static String lindex(String key, long index){  
  51.         Jedis jedis = jedisPool.getResource();  
  52.         String str = jedis.lindex(key, index);  
  53.         jedis.close();  
  54.         return str;  
  55.     }  
  56.       
  57.     /** 
  58.      * 获取列表长度,key为空时返回0 
  59.      * @param key 
  60.      * @return Long 
  61.      */  
  62.     public static Long llen(String key){  
  63.         Jedis jedis = jedisPool.getResource();  
  64.         Long length = jedis.llen(key);  
  65.         jedis.close();  
  66.         return length;  
  67.     }  
  68.       
  69.     /** 
  70.      * 在列表的元素前或者后插入元素,返回List的长度 
  71.      * @param key 
  72.      * @param where LIST_POSITION 
  73.      * @param pivot 以该元素作为参照物,是在它之前,还是之后(pivot:枢轴;中心点,中枢;[物]支点,支枢;[体]回转运动。) 
  74.      * @param value 
  75.      * @return Long 
  76.      */  
  77.     public static Long linsert(String key, LIST_POSITION where, String pivot, String value){  
  78.         Jedis jedis = jedisPool.getResource();  
  79.         Long length = jedis.linsert(key, where, pivot, value);  
  80.         jedis.close();  
  81.         return length;  
  82.     }  
  83.       
  84.     /** 
  85.      * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0) 
  86.      * @param key 
  87.      * @param value String 
  88.      * @return Long 
  89.      */  
  90.     public static Long lpushx(String key, String value){  
  91.         Jedis jedis = jedisPool.getResource();  
  92.         Long length = jedis.lpushx(key, value);  
  93.         jedis.close();  
  94.         return length;  
  95.     }  
  96.       
  97.     /** 
  98.      * 将一个或多个值插入到已存在的列表头部,当成功时,返回List的长度;当不成功(即key不存在时,返回0) 
  99.      * @param key 
  100.      * @param values String[] 
  101.      * @return Long 
  102.      */  
  103.     public static Long lpushx(String key, String[] values){  
  104.         Jedis jedis = jedisPool.getResource();  
  105.         Long length = jedis.lpushx(key, values);  
  106.         jedis.close();  
  107.         return length;  
  108.     }  
  109.       
  110.     /** 
  111.      * 移除列表元素,返回移除的元素数量 
  112.      * @param key 
  113.      * @param count,标识,表示动作或者查找方向 
  114.      * <li>当count=0时,移除所有匹配的元素;</li> 
  115.      * <li>当count为负数时,移除方向是从尾到头;</li> 
  116.      * <li>当count为正数时,移除方向是从头到尾;</li> 
  117.      * @param value 匹配的元素 
  118.      * @return Long 
  119.      */  
  120.     public static Long lrem(String key, long count, String value){  
  121.         Jedis jedis = jedisPool.getResource();  
  122.         Long length = jedis.lrem(key, count, value);  
  123.         jedis.close();  
  124.         return length;  
  125.     }  
  126.       
  127.     /** 
  128.      * 通过索引设置列表元素的值,当超出索引时会抛错。成功设置返回true 
  129.      * @param key 
  130.      * @param index 索引 
  131.      * @param value 
  132.      * @return boolean 
  133.      */  
  134.     public static boolean lset(String key, long index, String value){  
  135.         Jedis jedis = jedisPool.getResource();  
  136.         String statusCode = jedis.lset(key, index, value);  
  137.         jedis.close();  
  138.         if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
  139.             return true;  
  140.         }else{  
  141.             return false;  
  142.         }  
  143.     }  
  144.       
  145.     /** 
  146.      * 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 
  147.      * @param key 
  148.      * @param start 
  149.      * <li>可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)</li> 
  150.      * <li>如果start大于end,则返回一个空的列表,即列表被清空</li> 
  151.      * @param end 
  152.      * <li>可以为负数(-1是列表的最后一个元素,-2是列表倒数第二的元素。)</li> 
  153.      * <li>可以超出索引,不影响结果</li> 
  154.      * @return boolean 
  155.      */  
  156.     public static boolean ltrim(String key, long start, long end){  
  157.         Jedis jedis = jedisPool.getResource();  
  158.         String statusCode = jedis.ltrim(key, start, end);  
  159.         jedis.close();  
  160.         if(SUCCESS_OK.equalsIgnoreCase(statusCode)){  
  161.             return true;  
  162.         }else{  
  163.             return false;  
  164.         }  
  165.     }  
  166.       
  167.     /** 
  168.      * 移出并获取列表的第一个元素,当列表不存在或者为空时,返回Null 
  169.      * @param key 
  170.      * @return String 
  171.      */  
  172.     public static String lpop(String key){  
  173.         Jedis jedis = jedisPool.getResource();  
  174.         String value = jedis.lpop(key);  
  175.         jedis.close();  
  176.         return value;  
  177.     }  
  178.       
  179.     /** 
  180.      * 移除并获取列表最后一个元素,当列表不存在或者为空时,返回Null 
  181.      * @param key 
  182.      * @return String 
  183.      */  
  184.     public static String rpop(String key){  
  185.         Jedis jedis = jedisPool.getResource();  
  186.         String value = jedis.rpop(key);  
  187.         jedis.close();  
  188.         return value;  
  189.     }  
  190.       
  191.     /** 
  192.      * 在列表中的尾部添加一个个值,返回列表的长度 
  193.      * @param key 
  194.      * @param value 
  195.      * @return Long 
  196.      */  
  197.     public static Long rpush(String key, String value){  
  198.         Jedis jedis = jedisPool.getResource();  
  199.         Long length = jedis.rpush(key, value);  
  200.         jedis.close();  
  201.         return length;  
  202.     }  
  203.       
  204.     /** 
  205.      * 在列表中的尾部添加多个值,返回列表的长度 
  206.      * @param key 
  207.      * @param values 
  208.      * @return Long 
  209.      */  
  210.     public static Long rpush(String key, String[] values){  
  211.         Jedis jedis = jedisPool.getResource();  
  212.         Long length = jedis.rpush(key, values);  
  213.         jedis.close();  
  214.         return length;  
  215.     }  
  216.       
  217.     /** 
  218.      * 仅当列表存在时,才会向列表中的尾部添加一个值,返回列表的长度 
  219.      * @param key 
  220.      * @param value 
  221.      * @return Long 
  222.      */  
  223.     public static Long rpushx(String key, String value){  
  224.         Jedis jedis = jedisPool.getResource();  
  225.         Long length = jedis.rpushx(key, value);  
  226.         jedis.close();  
  227.         return length;  
  228.     }  
  229.       
  230.     /** 
  231.      * 移除列表的最后一个元素,并将该元素添加到另一个列表并返回 
  232.      * @param sourceKey 源列表的key,当源key不存在时,结果返回Null 
  233.      * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的 
  234.      * @return String 
  235.      */  
  236.     public static String rpopLpush(String sourceKey, String targetKey){  
  237.         Jedis jedis = jedisPool.getResource();  
  238.         String value = jedis.rpoplpush(sourceKey, targetKey);  
  239.         jedis.close();  
  240.         return value;  
  241.     }  
  242.       
  243.     /** 
  244.      * 移出并获取列表的【第一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
  245.      * @param timeout 单位为秒 
  246.      * @param keys 
  247.      * <li>当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。</li> 
  248.      * <li>当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。</li> 
  249.      * <li>当超期时间到达时,keys列表仍然没有内容,则返回Null</li> 
  250.      * @return List<String> 
  251.      */  
  252.     public static List<String> blpop(int timeout, String... keys){  
  253.         Jedis jedis = jedisPool.getResource();  
  254.         List<String> values = jedis.blpop(timeout, keys);  
  255.         jedis.close();  
  256.         return values;  
  257.     }  
  258.       
  259.     /** 
  260.      * 移出并获取列表的【最后一个元素】, 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
  261.      * @param timeout 单位为秒 
  262.      * @param keys 
  263.      * <li>当有多个key时,只要某个key值的列表有内容,即马上返回,不再阻塞。</li> 
  264.      * <li>当所有key都没有内容或不存在时,则会阻塞,直到有值返回或者超时。</li> 
  265.      * <li>当超期时间到达时,keys列表仍然没有内容,则返回Null</li> 
  266.      * @return List<String> 
  267.      */  
  268.     public static List<String> brpop(int timeout, String... keys){  
  269.         Jedis jedis = jedisPool.getResource();  
  270.         List<String> values = jedis.brpop(timeout, keys);  
  271.         jedis.close();  
  272.         return values;  
  273.     }  
  274.       
  275.     /** 
  276.      * 从列表中弹出列表最后一个值,将弹出的元素插入到另外一个列表中并返回它;  
  277.      * 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。 
  278.      * @param sourceKey 源列表的key,当源key不存在时,则会进行阻塞 
  279.      * @param targetKey 目标列表的key,当目标key不存在时,会自动创建新的 
  280.      * @param timeout 单位为秒 
  281.      * @return String 
  282.      */  
  283.     public static String brpopLpush(String sourceKey, String targetKey, int timeout){  
  284.         Jedis jedis = jedisPool.getResource();  
  285.         String value = jedis.brpoplpush(sourceKey, targetKey, timeout);  
  286.         jedis.close();  
  287.         return value;  
  288.     }  
  289.       
  290.     /**************************** redis 列表List end***************************/  

 

Redis操作Set工具类封装,Java Redis Set命令封装

Java代码  收藏代码
  1. /**************************** redis 集合Set start***************************/  
  2.     /**Redis的Set是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。**/  
  3.       
  4.       
  5.     /** 
  6.      * 向集合添加一个或多个成员,返回添加成功的数量 
  7.      * @param key 
  8.      * @param members 
  9.      * @return Long 
  10.      */  
  11.     public static Long sadd(String key, String... members){  
  12.         Jedis jedis = null;  
  13.         try {  
  14.             jedis = jedisPool.getResource();  
  15.             return jedis.sadd(key, members);  
  16.         }catch (Exception e) {  
  17.             e.printStackTrace();  
  18.         }finally{  
  19.             if(jedis != null){  
  20.                 jedis.close();  
  21.             }  
  22.         }  
  23.         return null;  
  24.     }  
  25.       
  26.     /** 
  27.      * 获取集合的成员数 
  28.      * @param key 
  29.      * @return 
  30.      */  
  31.     public static Long scard(String key){  
  32.         Jedis jedis = null;  
  33.         try {  
  34.             jedis = jedisPool.getResource();  
  35.             return jedis.scard(key);    
  36.         }catch (Exception e) {  
  37.             e.printStackTrace();  
  38.         }finally{  
  39.             if(jedis != null){  
  40.                 jedis.close();  
  41.             }  
  42.         }  
  43.         return null;  
  44.     }  
  45.       
  46.     /** 
  47.      * 返回集合中的所有成员 
  48.      * @param key 
  49.      * @return Set<String> 
  50.      */  
  51.     public static Set<String> smembers(String key){  
  52.         Jedis jedis = null;  
  53.         try {  
  54.             jedis = jedisPool.getResource();  
  55.             return jedis.smembers(key);    
  56.         }catch (Exception e) {  
  57.             e.printStackTrace();  
  58.         }finally{  
  59.             if(jedis != null){  
  60.                 jedis.close();  
  61.             }  
  62.         }  
  63.         return null;  
  64.     }  
  65.       
  66.     /** 
  67.      * 判断 member 元素是否是集合 key 的成员,在集合中返回True 
  68.      * @param key 
  69.      * @param member 
  70.      * @return Boolean 
  71.      */  
  72.     public static Boolean sIsMember(String key, String member){  
  73.         Jedis jedis = null;  
  74.         try {  
  75.             jedis = jedisPool.getResource();  
  76.             return jedis.sismember(key, member);    
  77.         }catch (Exception e) {  
  78.             e.printStackTrace();  
  79.         }finally{  
  80.             if(jedis != null){  
  81.                 jedis.close();  
  82.             }  
  83.         }  
  84.         return null;  
  85.     }  
  86.       
  87.     /** 
  88.      * 返回给定所有集合的差集(获取第一个key中与其它key不相同的值,当只有一个key时,就返回这个key的所有值) 
  89.      * @param keys 
  90.      * @return Set<String> 
  91.      */  
  92.     public static Set<String> sdiff(String... keys){  
  93.         Jedis jedis = null;  
  94.         try {  
  95.             jedis = jedisPool.getResource();  
  96.             return jedis.sdiff(keys);    
  97.         }catch (Exception e) {  
  98.             e.printStackTrace();  
  99.         }finally{  
  100.             if(jedis != null){  
  101.                 jedis.close();  
  102.             }  
  103.         }  
  104.         return null;  
  105.     }  
  106.       
  107.     /** 
  108.      * 返回给定所有集合的差集并存储在 targetKey中,类似sdiff,只是该方法把返回的差集保存到targetKey中 
  109.      * <li>当有差集时,返回true</li> 
  110.      * <li>当没有差集时,返回false</li> 
  111.      * @param targetKey 
  112.      * @param keys 
  113.      * @return 
  114.      */  
  115.     public static boolean sdiffStore(String targetKey, String... keys){  
  116.         Jedis jedis = null;  
  117.         try {  
  118.             jedis = jedisPool.getResource();  
  119.             Long statusCode = jedis.sdiffstore(targetKey, keys);  
  120.             if(SUCCESS_STATUS_LONG == statusCode){  
  121.                 return true;  
  122.             }   
  123.         }catch (Exception e) {  
  124.             e.printStackTrace();  
  125.         }finally{  
  126.             if(jedis != null){  
  127.                 jedis.close();  
  128.             }  
  129.         }  
  130.         return false;  
  131.     }  
  132.       
  133.     /** 
  134.      * 返回给定所有集合的交集(获取第一个key中与其它key相同的值,要求所有key都要有相同的值,如果没有相同,返回Null。当只有一个key时,就返回这个key的所有值) 
  135.      * @param keys 
  136.      * @return Set<String> 
  137.      */  
  138.     public static Set<String> sinter(String... keys){  
  139.         Jedis jedis = null;  
  140.         try {  
  141.             jedis = jedisPool.getResource();  
  142.             return jedis.sinter(keys);  
  143.         }catch (Exception e) {  
  144.             e.printStackTrace();  
  145.         }finally{  
  146.             if(jedis != null){  
  147.                 jedis.close();  
  148.             }  
  149.         }  
  150.         return null;  
  151.     }  
  152.       
  153.     /** 
  154.      * 返回给定所有集合的交集并存储在 targetKey中,类似sinter 
  155.      * @param targetKey 
  156.      * @param keys 
  157.      * @return boolean 
  158.      */  
  159.     public static boolean sinterStore(String targetKey, String... keys){  
  160.         Jedis jedis = null;  
  161.         try {  
  162.             jedis = jedisPool.getResource();  
  163.             Long statusCode = jedis.sinterstore(targetKey, keys);  
  164.             if(SUCCESS_STATUS_LONG == statusCode){  
  165.                 return true;  
  166.             }  
  167.         }catch (Exception e) {  
  168.             e.printStackTrace();  
  169.         }finally{  
  170.             if(jedis != null){  
  171.                 jedis.close();  
  172.             }  
  173.         }  
  174.         return false;  
  175.     }  
  176.       
  177.     /** 
  178.      * 将 member 元素从 sourceKey 集合移动到 targetKey 集合 
  179.      * <li>成功返回true</li> 
  180.      * <li>当member不存在于sourceKey时,返回false</li> 
  181.      * <li>当sourceKey不存在时,也返回false</li> 
  182.      * @param sourceKey 
  183.      * @param targetKey 
  184.      * @param member 
  185.      * @return boolean 
  186.      */  
  187.     public static boolean smove(String sourceKey, String targetKey, String member){  
  188.         Jedis jedis = null;  
  189.         try {  
  190.             jedis = jedisPool.getResource();  
  191.             Long value = jedis.smove(sourceKey, targetKey, member);  
  192.             if(value > 0){  
  193.                 return true;  
  194.             }  
  195.         }catch (Exception e) {  
  196.             e.printStackTrace();  
  197.         }finally{  
  198.             if(jedis != null){  
  199.                 jedis.close();  
  200.             }  
  201.         }  
  202.         return false;  
  203.     }  
  204.       
  205.     /** 
  206.      * 移除并返回集合中的一个随机元素 
  207.      * <li>当set为空或者不存在时,返回Null</li> 
  208.      * @param key 
  209.      * @return String 
  210.      */  
  211.     public static String spop(String key){  
  212.         Jedis jedis = null;  
  213.         try {  
  214.             jedis = jedisPool.getResource();  
  215.             return jedis.spop(key);  
  216.         }catch (Exception e) {  
  217.             e.printStackTrace();  
  218.         }finally{  
  219.             if(jedis != null){  
  220.                 jedis.close();  
  221.             }  
  222.         }  
  223.         return null;  
  224.     }  
  225.       
  226.     /** 
  227.      * 返回集合中一个或多个随机数 
  228.      * <li>当count大于set的长度时,set所有值返回,不会抛错。</li> 
  229.      * <li>当count等于0时,返回[]</li> 
  230.      * <li>当count小于0时,也能返回。如-1返回一个,-2返回两个</li> 
  231.      * @param key 
  232.      * @param count 
  233.      * @return List<String> 
  234.      */  
  235.     public static List<String> srandMember(String key, int count){  
  236.         Jedis jedis = null;  
  237.         try {  
  238.             jedis = jedisPool.getResource();  
  239.             return jedis.srandmember(key, count);  
  240.         }catch (Exception e) {  
  241.             e.printStackTrace();  
  242.         }finally{  
  243.             if(jedis != null){  
  244.                 jedis.close();  
  245.             }  
  246.         }  
  247.         return null;  
  248.     }  
  249.       
  250.     /** 
  251.      * 移除集合中一个或多个成员 
  252.      * @param key 
  253.      * @param members 
  254.      * @return 
  255.      */  
  256.     public static boolean srem(String key, String... members){  
  257.         Jedis jedis = null;  
  258.         try {  
  259.             jedis = jedisPool.getResource();  
  260.             //Integer reply, specifically: 1 if the new element was removed   
  261.             //0 if the new element was not a member of the set  
  262.             Long value = jedis.srem(key, members);  
  263.             if(value > 0){  
  264.                 return true;  
  265.             }  
  266.         }catch (Exception e) {  
  267.             e.printStackTrace();  
  268.         }finally{  
  269.             if(jedis != null){  
  270.                 jedis.close();  
  271.             }  
  272.         }  
  273.         return false;  
  274.     }  
  275.       
  276.     /** 
  277.      * 返回所有给定集合的并集,相同的只会返回一个 
  278.      * @param keys 
  279.      * @return 
  280.      */  
  281.     public static Set<String> sunion(String... keys){  
  282.         Jedis jedis = null;  
  283.         try {  
  284.             jedis = jedisPool.getResource();  
  285.             return jedis.sunion(keys);  
  286.         }catch (Exception e) {  
  287.             e.printStackTrace();  
  288.         }finally{  
  289.             if(jedis != null){  
  290.                 jedis.close();  
  291.             }  
  292.         }  
  293.         return null;  
  294.     }  
  295.       
  296.     /** 
  297.      * 所有给定集合的并集存储在targetKey集合中 
  298.      * <li>注:合并时,只会把keys中的集合返回,不包括targetKey本身</li> 
  299.      * <li>如果targetKey本身是有值的,合并后原来的值是没有的,因为把keys的集合重新赋值给targetKey</li> 
  300.      * <li>要想保留targetKey本身的值,keys要包含原来的targetKey</li> 
  301.      * @param targetKey 
  302.      * @param keys 
  303.      * @return 
  304.      */  
  305.     public static boolean sunionStore(String targetKey, String... keys){  
  306.         Jedis jedis = null;  
  307.         try {  
  308.             jedis = jedisPool.getResource();  
  309.             //返回合并后的长度  
  310.             Long statusCode = jedis.sunionstore(targetKey, keys);  
  311.             if(statusCode > 0){  
  312.                 return true;  
  313.             }  
  314.         }catch (Exception e) {  
  315.             e.printStackTrace();  
  316.         }finally{  
  317.             if(jedis != null){  
  318.                 jedis.close();  
  319.             }  
  320.         }  
  321.         return false;  
  322.     }  
  323.       
  324.     /**************************** redis 集合Set end***************************/  

 



原创粉丝点击