springboot项目中 jedis对redis缓存的操作

1:导入依赖

    

<!--jedis-->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.5.2</version>
</dependency>

2:配置文件

# Jedis
jedis.max.total = 100
jedis.max.idle = 10
jedis.max.waitmillis = 10000
jedis.testOnBorrow = true
jedis.max.timeout = 10000
jedis.host = localhost

3:实例bean

**
 *
 * @Author : Wukn
 * @Date : 2018/6/1
 */
@Configuration
public class JedisConfig {

    Logger logger = LoggerFactory.getLogger( JedisConfig.class );


    /**
     * 端口号
     */
    @Value( "${jedis.port}" )
    private Integer port;


    /**
     * local
     */
    @Value( "${jedis.host}" )
    private String host;


    /**
     * 超时时间
     */
    @Value( "${jedis.max.timeout}" )
    private Integer timeout;






    @Bean
    public JedisPoolConfig getRedisConfig() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        return poolConfig;
    }




    @Bean
    public JedisPool getJedisPool() {
        JedisPoolConfig config = getRedisConfig();
        JedisPool pool = new JedisPool(config,host,port,timeout);
        logger.info("init JredisPool ...");
        return pool;
    }



    @Bean
    public Jedis getJedis() {
        return getJedisPool().getResource();
    }

4:封装jedisDao

  1. JedisUtils工具类如下:包含各种类型存储和删除  
  2.   
  3.   
  4. package com.telit.common.utils;  
  5.   
  6. import java.util.List;  
  7. import java.util.Map;  
  8. import java.util.Set;  
  9.   
  10. import org.slf4j.Logger;  
  11. import org.slf4j.LoggerFactory;  
  12.   
  13. import com.google.common.collect.Lists;  
  14. import com.google.common.collect.Maps;  
  15. import com.google.common.collect.Sets;  
  16. import com.telit.common.config.Global;  
  17.   
  18. import redis.clients.jedis.Jedis;  
  19. import redis.clients.jedis.JedisPool;  
  20. import redis.clients.jedis.exceptions.JedisException;  
  21.   
  22. /** 
  23.  * Jedis Cache 工具类 
  24.  *  
  25.  * @author zhoujielun 
  26.  * @version 2017-6-19 
  27.  */  
  28. public class JedisUtils {  
  29.   
  30.     private static Logger logger = LoggerFactory.getLogger(JedisUtils.class);  
  31.       
  32.     private static JedisPool jedisPool = SpringContextHolder.getBean(JedisPool.class);  
  33.   
  34.     public static final String KEY_PREFIX = Global.getConfig("redis.keyPrefix");  
  35.       
  36.     /** 
  37.      * 获取缓存 
  38.      * @param key 键 
  39.      * @return 值 
  40.      */  
  41.     public static String get(String key) {  
  42.         String value = null;  
  43.         Jedis jedis = null;  
  44.         try {  
  45.             jedis = getResource();  
  46.             if (jedis.exists(key)) {  
  47.                 value = jedis.get(key);  
  48.                 value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value) ? value : null;  
  49.                 logger.debug("get {} = {}", key, value);  
  50.             }  
  51.         } catch (Exception e) {  
  52.             logger.warn("get {} = {}", key, value, e);  
  53.         } finally {  
  54.             returnResource(jedis);  
  55.         }  
  56.         return value;  
  57.     }  
  58.       
  59.     /** 
  60.      * 获取缓存 
  61.      * @param key 键 
  62.      * @return 值 
  63.      */  
  64.     public static Object getObject(String key) {  
  65.         Object value = null;  
  66.         Jedis jedis = null;  
  67.         try {  
  68.             jedis = getResource();  
  69.             if (jedis.exists(getBytesKey(key))) {  
  70.                 value = toObject(jedis.get(getBytesKey(key)));  
  71.                 logger.debug("getObject {} = {}", key, value);  
  72.             }  
  73.         } catch (Exception e) {  
  74.             logger.warn("getObject {} = {}", key, value, e);  
  75.         } finally {  
  76.             returnResource(jedis);  
  77.         }  
  78.         return value;  
  79.     }  
  80.       
  81.     /** 
  82.      * 设置缓存 
  83.      * @param key 键 
  84.      * @param value 值 
  85.      * @param cacheSeconds 超时时间,0为不超时 
  86.      * @return 
  87.      */  
  88.     public static String set(String key, String value, int cacheSeconds) {  
  89.         String result = null;  
  90.         Jedis jedis = null;  
  91.         try {  
  92.             jedis = getResource();  
  93.             result = jedis.set(key, value);  
  94.             if (cacheSeconds != 0) {  
  95.                 jedis.expire(key, cacheSeconds);  
  96.             }  
  97.             logger.debug("set {} = {}", key, value);  
  98.         } catch (Exception e) {  
  99.             logger.warn("set {} = {}", key, value, e);  
  100.         } finally {  
  101.             returnResource(jedis);  
  102.         }  
  103.         return result;  
  104.     }  
  105.       
  106.     /** 
  107.      * 设置缓存 
  108.      * @param key 键 
  109.      * @param value 值 
  110.      * @param cacheSeconds 超时时间,0为不超时 
  111.      * @return 
  112.      */  
  113.     public static String setObject(String key, Object value, int cacheSeconds) {  
  114.         String result = null;  
  115.         Jedis jedis = null;  
  116.         try {  
  117.             jedis = getResource();  
  118.             result = jedis.set(getBytesKey(key), toBytes(value));  
  119.             if (cacheSeconds != 0) {  
  120.                 jedis.expire(key, cacheSeconds);  
  121.             }  
  122.             logger.debug("setObject {} = {}", key, value);  
  123.         } catch (Exception e) {  
  124.             logger.warn("setObject {} = {}", key, value, e);  
  125.         } finally {  
  126.             returnResource(jedis);  
  127.         }  
  128.         return result;  
  129.     }  
  130.       
  131.     /** 
  132.      * 获取List缓存 
  133.      * @param key 键 
  134.      * @return 值 
  135.      */  
  136.     public static List<String> getList(String key) {  
  137.         List<String> value = null;  
  138.         Jedis jedis = null;  
  139.         try {  
  140.             jedis = getResource();  
  141.             if (jedis.exists(key)) {  
  142.                 value = jedis.lrange(key, 0, -1);  
  143.                 logger.debug("getList {} = {}", key, value);  
  144.             }  
  145.         } catch (Exception e) {  
  146.             logger.warn("getList {} = {}", key, value, e);  
  147.         } finally {  
  148.             returnResource(jedis);  
  149.         }  
  150.         return value;  
  151.     }  
  152.       
  153.     /** 
  154.      * 获取List缓存 
  155.      * @param key 键 
  156.      * @return 值 
  157.      */  
  158.     public static List<Object> getObjectList(String key) {  
  159.         List<Object> value = null;  
  160.         Jedis jedis = null;  
  161.         try {  
  162.             jedis = getResource();  
  163.             if (jedis.exists(getBytesKey(key))) {  
  164.                 List<byte[]> list = jedis.lrange(getBytesKey(key), 0, -1);  
  165.                 value = Lists.newArrayList();  
  166.                 for (byte[] bs : list){  
  167.                     value.add(toObject(bs));  
  168.                 }  
  169.                 logger.debug("getObjectList {} = {}", key, value);  
  170.             }  
  171.         } catch (Exception e) {  
  172.             logger.warn("getObjectList {} = {}", key, value, e);  
  173.         } finally {  
  174.             returnResource(jedis);  
  175.         }  
  176.         return value;  
  177.     }  
  178.       
  179.     /** 
  180.      * 设置List缓存 
  181.      * @param key 键 
  182.      * @param value 值 
  183.      * @param cacheSeconds 超时时间,0为不超时 
  184.      * @return 
  185.      */  
  186.     public static long setList(String key, List<String> value, int cacheSeconds) {  
  187.         long result = 0;  
  188.         Jedis jedis = null;  
  189.         try {  
  190.             jedis = getResource();  
  191.             if (jedis.exists(key)) {  
  192.                 jedis.del(key);  
  193.             }  
  194.             result = jedis.rpush(key, (String[])value.toArray());  
  195.             if (cacheSeconds != 0) {  
  196.                 jedis.expire(key, cacheSeconds);  
  197.             }  
  198.             logger.debug("setList {} = {}", key, value);  
  199.         } catch (Exception e) {  
  200.             logger.warn("setList {} = {}", key, value, e);  
  201.         } finally {  
  202.             returnResource(jedis);  
  203.         }  
  204.         return result;  
  205.     }  
  206.       
  207.     /** 
  208.      * 设置List缓存 
  209.      * @param key 键 
  210.      * @param value 值 
  211.      * @param cacheSeconds 超时时间,0为不超时 
  212.      * @return 
  213.      */  
  214.     public static long setObjectList(String key, List<Object> value, int cacheSeconds) {  
  215.         long result = 0;  
  216.         Jedis jedis = null;  
  217.         try {  
  218.             jedis = getResource();  
  219.             if (jedis.exists(getBytesKey(key))) {  
  220.                 jedis.del(key);  
  221.             }  
  222.             List<byte[]> list = Lists.newArrayList();  
  223.             for (Object o : value){  
  224.                 list.add(toBytes(o));  
  225.             }  
  226.             result = jedis.rpush(getBytesKey(key), (byte[][])list.toArray());  
  227.             if (cacheSeconds != 0) {  
  228.                 jedis.expire(key, cacheSeconds);  
  229.             }  
  230.             logger.debug("setObjectList {} = {}", key, value);  
  231.         } catch (Exception e) {  
  232.             logger.warn("setObjectList {} = {}", key, value, e);  
  233.         } finally {  
  234.             returnResource(jedis);  
  235.         }  
  236.         return result;  
  237.     }  
  238.       
  239.     /** 
  240.      * 向List缓存中添加值 
  241.      * @param key 键 
  242.      * @param value 值 
  243.      * @return 
  244.      */  
  245.     public static long listAdd(String key, String... value) {  
  246.         long result = 0;  
  247.         Jedis jedis = null;  
  248.         try {  
  249.             jedis = getResource();  
  250.             result = jedis.rpush(key, value);  
  251.             logger.debug("listAdd {} = {}", key, value);  
  252.         } catch (Exception e) {  
  253.             logger.warn("listAdd {} = {}", key, value, e);  
  254.         } finally {  
  255.             returnResource(jedis);  
  256.         }  
  257.         return result;  
  258.     }  
  259.       
  260.     /** 
  261.      * 向List缓存中添加值 
  262.      * @param key 键 
  263.      * @param value 值 
  264.      * @return 
  265.      */  
  266.     public static long listObjectAdd(String key, Object... value) {  
  267.         long result = 0;  
  268.         Jedis jedis = null;  
  269.         try {  
  270.             jedis = getResource();  
  271.             List<byte[]> list = Lists.newArrayList();  
  272.             for (Object o : value){  
  273.                 list.add(toBytes(o));  
  274.             }  
  275.             result = jedis.rpush(getBytesKey(key), (byte[][])list.toArray());  
  276.             logger.debug("listObjectAdd {} = {}", key, value);  
  277.         } catch (Exception e) {  
  278.             logger.warn("listObjectAdd {} = {}", key, value, e);  
  279.         } finally {  
  280.             returnResource(jedis);  
  281.         }  
  282.         return result;  
  283.     }  
  284.   
  285.     /** 
  286.      * 获取缓存 
  287.      * @param key 键 
  288.      * @return 值 
  289.      */  
  290.     public static Set<String> getSet(String key) {  
  291.         Set<String> value = null;  
  292.         Jedis jedis = null;  
  293.         try {  
  294.             jedis = getResource();  
  295.             if (jedis.exists(key)) {  
  296.                 value = jedis.smembers(key);  
  297.                 logger.debug("getSet {} = {}", key, value);  
  298.             }  
  299.         } catch (Exception e) {  
  300.             logger.warn("getSet {} = {}", key, value, e);  
  301.         } finally {  
  302.             returnResource(jedis);  
  303.         }  
  304.         return value;  
  305.     }  
  306.       
  307.     /** 
  308.      * 获取缓存 
  309.      * @param key 键 
  310.      * @return 值 
  311.      */  
  312.     public static Set<Object> getObjectSet(String key) {  
  313.         Set<Object> value = null;  
  314.         Jedis jedis = null;  
  315.         try {  
  316.             jedis = getResource();  
  317.             if (jedis.exists(getBytesKey(key))) {  
  318.                 value = Sets.newHashSet();  
  319.                 Set<byte[]> set = jedis.smembers(getBytesKey(key));  
  320.                 for (byte[] bs : set){  
  321.                     value.add(toObject(bs));  
  322.                 }  
  323.                 logger.debug("getObjectSet {} = {}", key, value);  
  324.             }  
  325.         } catch (Exception e) {  
  326.             logger.warn("getObjectSet {} = {}", key, value, e);  
  327.         } finally {  
  328.             returnResource(jedis);  
  329.         }  
  330.         return value;  
  331.     }  
  332.       
  333.     /** 
  334.      * 设置Set缓存 
  335.      * @param key 键 
  336.      * @param value 值 
  337.      * @param cacheSeconds 超时时间,0为不超时 
  338.      * @return 
  339.      */  
  340.     public static long setSet(String key, Set<String> value, int cacheSeconds) {  
  341.         long result = 0;  
  342.         Jedis jedis = null;  
  343.         try {  
  344.             jedis = getResource();  
  345.             if (jedis.exists(key)) {  
  346.                 jedis.del(key);  
  347.             }  
  348.             result = jedis.sadd(key, (String[])value.toArray());  
  349.             if (cacheSeconds != 0) {  
  350.                 jedis.expire(key, cacheSeconds);  
  351.             }  
  352.             logger.debug("setSet {} = {}", key, value);  
  353.         } catch (Exception e) {  
  354.             logger.warn("setSet {} = {}", key, value, e);  
  355.         } finally {  
  356.             returnResource(jedis);  
  357.         }  
  358.         return result;  
  359.     }  
  360.       
  361.     /** 
  362.      * 设置Set缓存 
  363.      * @param key 键 
  364.      * @param value 值 
  365.      * @param cacheSeconds 超时时间,0为不超时 
  366.      * @return 
  367.      */  
  368.     public static long setObjectSet(String key, Set<Object> value, int cacheSeconds) {  
  369.         long result = 0;  
  370.         Jedis jedis = null;  
  371.         try {  
  372.             jedis = getResource();  
  373.             if (jedis.exists(getBytesKey(key))) {  
  374.                 jedis.del(key);  
  375.             }  
  376.             Set<byte[]> set = Sets.newHashSet();  
  377.             for (Object o : value){  
  378.                 set.add(toBytes(o));  
  379.             }  
  380.             result = jedis.sadd(getBytesKey(key), (byte[][])set.toArray());  
  381.             if (cacheSeconds != 0) {  
  382.                 jedis.expire(key, cacheSeconds);  
  383.             }  
  384.             logger.debug("setObjectSet {} = {}", key, value);  
  385.         } catch (Exception e) {  
  386.             logger.warn("setObjectSet {} = {}", key, value, e);  
  387.         } finally {  
  388.             returnResource(jedis);  
  389.         }  
  390.         return result;  
  391.     }  
  392.       
  393.     /** 
  394.      * 向Set缓存中添加值 
  395.      * @param key 键 
  396.      * @param value 值 
  397.      * @return 
  398.      */  
  399.     public static long setSetAdd(String key, String... value) {  
  400.         long result = 0;  
  401.         Jedis jedis = null;  
  402.         try {  
  403.             jedis = getResource();  
  404.             result = jedis.sadd(key, value);  
  405.             logger.debug("setSetAdd {} = {}", key, value);  
  406.         } catch (Exception e) {  
  407.             logger.warn("setSetAdd {} = {}", key, value, e);  
  408.         } finally {  
  409.             returnResource(jedis);  
  410.         }  
  411.         return result;  
  412.     }  
  413.   
  414.     /** 
  415.      * 向Set缓存中添加值 
  416.      * @param key 键 
  417.      * @param value 值 
  418.      * @return 
  419.      */  
  420.     public static long setSetObjectAdd(String key, Object... value) {  
  421.         long result = 0;  
  422.         Jedis jedis = null;  
  423.         try {  
  424.             jedis = getResource();  
  425.             Set<byte[]> set = Sets.newHashSet();  
  426.             for (Object o : value){  
  427.                 set.add(toBytes(o));  
  428.             }  
  429.             result = jedis.rpush(getBytesKey(key), (byte[][])set.toArray());  
  430.             logger.debug("setSetObjectAdd {} = {}", key, value);  
  431.         } catch (Exception e) {  
  432.             logger.warn("setSetObjectAdd {} = {}", key, value, e);  
  433.         } finally {  
  434.             returnResource(jedis);  
  435.         }  
  436.         return result;  
  437.     }  
  438.       
  439.     /** 
  440.      * 获取Map缓存 
  441.      * @param key 键 
  442.      * @return 值 
  443.      */  
  444.     public static Map<String, String> getMap(String key) {  
  445.         Map<String, String> value = null;  
  446.         Jedis jedis = null;  
  447.         try {  
  448.             jedis = getResource();  
  449.             if (jedis.exists(key)) {  
  450.                 value = jedis.hgetAll(key);  
  451.                 logger.debug("getMap {} = {}", key, value);  
  452.             }  
  453.         } catch (Exception e) {  
  454.             logger.warn("getMap {} = {}", key, value, e);  
  455.         } finally {  
  456.             returnResource(jedis);  
  457.         }  
  458.         return value;  
  459.     }  
  460.       
  461.     /** 
  462.      * 获取Map缓存 
  463.      * @param key 键 
  464.      * @return 值 
  465.      */  
  466.     public static Map<String, Object> getObjectMap(String key) {  
  467.         Map<String, Object> value = null;  
  468.         Jedis jedis = null;  
  469.         try {  
  470.             jedis = getResource();  
  471.             if (jedis.exists(getBytesKey(key))) {  
  472.                 value = Maps.newHashMap();  
  473.                 Map<byte[], byte[]> map = jedis.hgetAll(getBytesKey(key));  
  474.                 for (Map.Entry<byte[], byte[]> e : map.entrySet()){  
  475.                     value.put(StringUtils.toString(e.getKey()), toObject(e.getValue()));  
  476.                 }  
  477.                 logger.debug("getObjectMap {} = {}", key, value);  
  478.             }  
  479.         } catch (Exception e) {  
  480.             logger.warn("getObjectMap {} = {}", key, value, e);  
  481.         } finally {  
  482.             returnResource(jedis);  
  483.         }  
  484.         return value;  
  485.     }  
  486.       
  487.     /** 
  488.      * 设置Map缓存 
  489.      * @param key 键 
  490.      * @param value 值 
  491.      * @param cacheSeconds 超时时间,0为不超时 
  492.      * @return 
  493.      */  
  494.     public static String setMap(String key, Map<String, String> value, int cacheSeconds) {  
  495.         String result = null;  
  496.         Jedis jedis = null;  
  497.         try {  
  498.             jedis = getResource();  
  499.             if (jedis.exists(key)) {  
  500.                 jedis.del(key);  
  501.             }  
  502.             result = jedis.hmset(key, value);  
  503.             if (cacheSeconds != 0) {  
  504.                 jedis.expire(key, cacheSeconds);  
  505.             }  
  506.             logger.debug("setMap {} = {}", key, value);  
  507.         } catch (Exception e) {  
  508.             logger.warn("setMap {} = {}", key, value, e);  
  509.         } finally {  
  510.             returnResource(jedis);  
  511.         }  
  512.         return result;  
  513.     }  
  514.       
  515.     /** 
  516.      * 设置Map缓存 
  517.      * @param key 键 
  518.      * @param value 值 
  519.      * @param cacheSeconds 超时时间,0为不超时 
  520.      * @return 
  521.      */  
  522.     public static String setObjectMap(String key, Map<String, Object> value, int cacheSeconds) {  
  523.         String result = null;  
  524.         Jedis jedis = null;  
  525.         try {  
  526.             jedis = getResource();  
  527.             if (jedis.exists(getBytesKey(key))) {  
  528.                 jedis.del(key);  
  529.             }  
  530.             Map<byte[], byte[]> map = Maps.newHashMap();  
  531.             for (Map.Entry<String, Object> e : value.entrySet()){  
  532.                 map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));  
  533.             }  
  534.             result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>)map);  
  535.             if (cacheSeconds != 0) {  
  536.                 jedis.expire(key, cacheSeconds);  
  537.             }  
  538.             logger.debug("setObjectMap {} = {}", key, value);  
  539.         } catch (Exception e) {  
  540.             logger.warn("setObjectMap {} = {}", key, value, e);  
  541.         } finally {  
  542.             returnResource(jedis);  
  543.         }  
  544.         return result;  
  545.     }  
  546.       
  547.     /** 
  548.      * 向Map缓存中添加值 
  549.      * @param key 键 
  550.      * @param value 值 
  551.      * @return 
  552.      */  
  553.     public static String mapPut(String key, Map<String, String> value) {  
  554.         String result = null;  
  555.         Jedis jedis = null;  
  556.         try {  
  557.             jedis = getResource();  
  558.             result = jedis.hmset(key, value);  
  559.             logger.debug("mapPut {} = {}", key, value);  
  560.         } catch (Exception e) {  
  561.             logger.warn("mapPut {} = {}", key, value, e);  
  562.         } finally {  
  563.             returnResource(jedis);  
  564.         }  
  565.         return result;  
  566.     }  
  567.       
  568.     /** 
  569.      * 向Map缓存中添加值 
  570.      * @param key 键 
  571.      * @param value 值 
  572.      * @return 
  573.      */  
  574.     public static String mapObjectPut(String key, Map<String, Object> value) {  
  575.         String result = null;  
  576.         Jedis jedis = null;  
  577.         try {  
  578.             jedis = getResource();  
  579.             Map<byte[], byte[]> map = Maps.newHashMap();  
  580.             for (Map.Entry<String, Object> e : value.entrySet()){  
  581.                 map.put(getBytesKey(e.getKey()), toBytes(e.getValue()));  
  582.             }  
  583.             result = jedis.hmset(getBytesKey(key), (Map<byte[], byte[]>)map);  
  584.             logger.debug("mapObjectPut {} = {}", key, value);  
  585.         } catch (Exception e) {  
  586.             logger.warn("mapObjectPut {} = {}", key, value, e);  
  587.         } finally {  
  588.             returnResource(jedis);  
  589.         }  
  590.         return result;  
  591.     }  
  592.       
  593.     /** 
  594.      * 移除Map缓存中的值 
  595.      * @param key 键 
  596.      * @param value 值 
  597.      * @return 
  598.      */  
  599.     public static long mapRemove(String key, String mapKey) {  
  600.         long result = 0;  
  601.         Jedis jedis = null;  
  602.         try {  
  603.             jedis = getResource();  
  604.             result = jedis.hdel(key, mapKey);  
  605.             logger.debug("mapRemove {}  {}", key, mapKey);  
  606.         } catch (Exception e) {  
  607.             logger.warn("mapRemove {}  {}", key, mapKey, e);  
  608.         } finally {  
  609.             returnResource(jedis);  
  610.         }  
  611.         return result;  
  612.     }  
  613.       
  614.     /** 
  615.      * 移除Map缓存中的值 
  616.      * @param key 键 
  617.      * @param value 值 
  618.      * @return 
  619.      */  
  620.     public static long mapObjectRemove(String key, String mapKey) {  
  621.         long result = 0;  
  622.         Jedis jedis = null;  
  623.         try {  
  624.             jedis = getResource();  
  625.             result = jedis.hdel(getBytesKey(key), getBytesKey(mapKey));  
  626.             logger.debug("mapObjectRemove {}  {}", key, mapKey);  
  627.         } catch (Exception e) {  
  628.             logger.warn("mapObjectRemove {}  {}", key, mapKey, e);  
  629.         } finally {  
  630.             returnResource(jedis);  
  631.         }  
  632.         return result;  
  633.     }  
  634.       
  635.     /** 
  636.      * 判断Map缓存中的Key是否存在 
  637.      * @param key 键 
  638.      * @param value 值 
  639.      * @return 
  640.      */  
  641.     public static boolean mapExists(String key, String mapKey) {  
  642.         boolean result = false;  
  643.         Jedis jedis = null;  
  644.         try {  
  645.             jedis = getResource();  
  646.             result = jedis.hexists(key, mapKey);  
  647.             logger.debug("mapExists {}  {}", key, mapKey);  
  648.         } catch (Exception e) {  
  649.             logger.warn("mapExists {}  {}", key, mapKey, e);  
  650.         } finally {  
  651.             returnResource(jedis);  
  652.         }  
  653.         return result;  
  654.     }  
  655.       
  656.     /** 
  657.      * 判断Map缓存中的Key是否存在 
  658.      * @param key 键 
  659.      * @param value 值 
  660.      * @return 
  661.      */  
  662.     public static boolean mapObjectExists(String key, String mapKey) {  
  663.         boolean result = false;  
  664.         Jedis jedis = null;  
  665.         try {  
  666.             jedis = getResource();  
  667.             result = jedis.hexists(getBytesKey(key), getBytesKey(mapKey));  
  668.             logger.debug("mapObjectExists {}  {}", key, mapKey);  
  669.         } catch (Exception e) {  
  670.             logger.warn("mapObjectExists {}  {}", key, mapKey, e);  
  671.         } finally {  
  672.             returnResource(jedis);  
  673.         }  
  674.         return result;  
  675.     }  
  676.       
  677.     /** 
  678.      * 删除缓存 
  679.      * @param key 键 
  680.      * @return 
  681.      */  
  682.     public static long del(String key) {  
  683.         long result = 0;  
  684.         Jedis jedis = null;  
  685.         try {  
  686.             jedis = getResource();  
  687.             if (jedis.exists(key)){  
  688.                 result = jedis.del(key);  
  689.                 logger.debug("del {}", key);  
  690.             }else{  
  691.                 logger.debug("del {} not exists", key);  
  692.             }  
  693.         } catch (Exception e) {  
  694.             logger.warn("del {}", key, e);  
  695.         } finally {  
  696.             returnResource(jedis);  
  697.         }  
  698.         return result;  
  699.     }  
  700.   
  701.     /** 
  702.      * 删除缓存 
  703.      * @param key 键 
  704.      * @return 
  705.      */  
  706.     public static long delObject(String key) {  
  707.         long result = 0;  
  708.         Jedis jedis = null;  
  709.         try {  
  710.             jedis = getResource();  
  711.             if (jedis.exists(getBytesKey(key))){  
  712.                 result = jedis.del(getBytesKey(key));  
  713.                 logger.debug("delObject {}", key);  
  714.             }else{  
  715.                 logger.debug("delObject {} not exists", key);  
  716.             }  
  717.         } catch (Exception e) {  
  718.             logger.warn("delObject {}", key, e);  
  719.         } finally {  
  720.             returnResource(jedis);  
  721.         }  
  722.         return result;  
  723.     }  
  724.       
  725.     /** 
  726.      * 缓存是否存在 
  727.      * @param key 键 
  728.      * @return 
  729.      */  
  730.     public static boolean exists(String key) {  
  731.         boolean result = false;  
  732.         Jedis jedis = null;  
  733.         try {  
  734.             jedis = getResource();  
  735.             result = jedis.exists(key);  
  736.             logger.debug("exists {}", key);  
  737.         } catch (Exception e) {  
  738.             logger.warn("exists {}", key, e);  
  739.         } finally {  
  740.             returnResource(jedis);  
  741.         }  
  742.         return result;  
  743.     }  
  744.       
  745.     /** 
  746.      * 缓存是否存在 
  747.      * @param key 键 
  748.      * @return 
  749.      */  
  750.     public static boolean existsObject(String key) {  
  751.         boolean result = false;  
  752.         Jedis jedis = null;  
  753.         try {  
  754.             jedis = getResource();  
  755.             result = jedis.exists(getBytesKey(key));  
  756.             logger.debug("existsObject {}", key);  
  757.         } catch (Exception e) {  
  758.             logger.warn("existsObject {}", key, e);  
  759.         } finally {  
  760.             returnResource(jedis);  
  761.         }  
  762.         return result;  
  763.     }  
  764.   
  765.     /** 
  766.      * 获取资源 
  767.      * @return 
  768.      * @throws JedisException 
  769.      */  
  770.     public static Jedis getResource() throws JedisException {  
  771.         Jedis jedis = null;  
  772.         try {  
  773.             jedis = jedisPool.getResource();  
  774. //          logger.debug("getResource.", jedis);  
  775.         } catch (JedisException e) {  
  776.             logger.warn("getResource.", e);  
  777.             returnBrokenResource(jedis);  
  778.             throw e;  
  779.         }  
  780.         return jedis;  
  781.     }  
  782.   
  783.     /** 
  784.      * 归还资源 
  785.      * @param jedis 
  786.      * @param isBroken 
  787.      */  
  788.     public static void returnBrokenResource(Jedis jedis) {  
  789.         if (jedis != null) {  
  790.             jedisPool.returnBrokenResource(jedis);  
  791.         }  
  792.     }  
  793.       
  794.     /** 
  795.      * 释放资源 
  796.      * @param jedis 
  797.      * @param isBroken 
  798.      */  
  799.     public static void returnResource(Jedis jedis) {  
  800.         if (jedis != null) {  
  801.             jedisPool.returnResource(jedis);  
  802.         }  
  803.     }  
  804.   
  805.     /** 
  806.      * 获取byte[]类型Key 
  807.      * @param key 
  808.      * @return 
  809.      */  
  810.     public static byte[] getBytesKey(Object object){  
  811.         if(object instanceof String){  
  812.             return StringUtils.getBytes((String)object);  
  813.         }else{  
  814.             return ObjectUtils.serialize(object);  
  815.         }  
  816.     }  
  817.       
  818.     /** 
  819.      * Object转换byte[]类型 
  820.      * @param key 
  821.      * @return 
  822.      */  
  823.     public static byte[] toBytes(Object object){  
  824.         return ObjectUtils.serialize(object);  
  825.     }  
  826.   
  827.     /** 
  828.      * byte[]型转换Object 
  829.      * @param key 
  830.      * @return 
  831.      */  
  832.     public static Object toObject(byte[] bytes){  
  833.         return ObjectUtils.unserialize(bytes);  
  834.     }  
  835.   
  836. }  

5:测试

    @Test
    public void dfg() {
//        List<String> list = new ArrayList <>(  );
//        list.add( "1" );
//        list.add( "2" );
//        list.add( "3" );
//        list.add( "4" );
//        jedisDao.setList( "key05",list,5000 );
        List<String> list = jedisDao.getList("key05");
        for (String s:list) {
            System.out.println(s);
        }
        System.out.println(list.size());

        //System.out.println(jedisDao.listAdd( "key05","5" ));
    }






整个目录结构








猜你喜欢

转载自blog.csdn.net/weixin_41404773/article/details/80546679