自定义RedisUtils个工具类

打印 上一主题 下一主题

主题 906|帖子 906|积分 2718

网上有很多可以自行查询;
1、狂神的RedisUtil

点击查看代码
  1. //在我们真实的开发中,或者在公司,一般都可以看到一个公司自己封装的Utils工具~~
  2. @Component
  3. public class RedisUtil {
  4.     @Autowired
  5.     //@Qualifier("myRedisTemplate") //一定要保证是我们自己写的RedisTemplate
  6.     private RedisTemplate<String, Object> redisTemplate;
  7.     // =============================common============================
  8.     /**
  9.      * 指定缓存失效时间
  10.      *
  11.      * @param key  键
  12.      * @param time 时间(秒)
  13.      */
  14.     public boolean expire(String key, long time) {
  15.         try {
  16.             if (time > 0) {
  17.                 redisTemplate.expire(key, time, TimeUnit.SECONDS);
  18.             }
  19.             return true;
  20.         } catch (Exception e) {
  21.             e.printStackTrace();
  22.             return false;
  23.         }
  24.     }
  25.     /**
  26.      * 根据key 获取过期时间
  27.      *
  28.      * @param key 键 不能为null
  29.      * @return 时间(秒) 返回0代表为永久有效
  30.      */
  31.     public long getExpire(String key) {
  32.         return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  33.     }
  34.     /**
  35.      * 判断key是否存在
  36.      *
  37.      * @param key 键
  38.      * @return true 存在 false不存在
  39.      */
  40.     public boolean hasKey(String key) {
  41.         try {
  42.             return redisTemplate.hasKey(key);
  43.         } catch (Exception e) {
  44.             e.printStackTrace();
  45.             return false;
  46.         }
  47.     }
  48.     /**
  49.      * 删除缓存
  50.      *
  51.      * @param key 可以传一个值 或多个
  52.      */
  53.     @SuppressWarnings("unchecked")
  54.     public void del(String... key) {
  55.         if (key != null && key.length > 0) {
  56.             if (key.length == 1) {
  57.                 redisTemplate.delete(key[0]);
  58.             } else {
  59.                 redisTemplate.delete(String.valueOf(CollectionUtils.arrayToList(key)));
  60.             }
  61.         }
  62.     }
  63.     // ============================String=============================
  64.     /**
  65.      * 普通缓存获取
  66.      *
  67.      * @param key 键
  68.      * @return 值
  69.      */
  70.     public Object get(String key) {
  71.         return key == null ? null : redisTemplate.opsForValue().get(key);
  72.     }
  73.     /**
  74.      * 普通缓存放入
  75.      *
  76.      * @param key   键
  77.      * @param value 值
  78.      * @return true成功 false失败
  79.      */
  80.     public boolean set(String key, Object value) {
  81.         try {
  82.             redisTemplate.opsForValue().set(key, value);
  83.             return true;
  84.         } catch (Exception e) {
  85.             e.printStackTrace();
  86.             return false;
  87.         }
  88.     }
  89.     /**
  90.      * 普通缓存放入并设置时间
  91.      *
  92.      * @param key   键
  93.      * @param value 值
  94.      * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
  95.      * @return true成功 false 失败
  96.      */
  97.     public boolean set(String key, Object value, long time) {
  98.         try {
  99.             if (time > 0) {
  100.                 redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
  101.             } else {
  102.                 set(key, value);
  103.             }
  104.             return true;
  105.         } catch (Exception e) {
  106.             e.printStackTrace();
  107.             return false;
  108.         }
  109.     }
  110.     /**
  111.      * 递增
  112.      *
  113.      * @param key   键
  114.      * @param delta 要增加几(大于0)
  115.      */
  116.     public long incr(String key, long delta) {
  117.         if (delta < 0) {
  118.             throw new RuntimeException("递增因子必须大于0");
  119.         }
  120.         return redisTemplate.opsForValue().increment(key, delta);
  121.     }
  122.     /**
  123.      * 递减
  124.      *
  125.      * @param key   键
  126.      * @param delta 要减少几(小于0)
  127.      */
  128.     public long decr(String key, long delta) {
  129.         if (delta < 0) {
  130.             throw new RuntimeException("递减因子必须大于0");
  131.         }
  132.         return redisTemplate.opsForValue().increment(key, -delta);
  133.     }
  134.     // ================================Map=================================
  135.     /**
  136.      * HashGet
  137.      *
  138.      * @param key  键 不能为null
  139.      * @param item 项 不能为null
  140.      */
  141.     public Object hget(String key, String item) {
  142.         return redisTemplate.opsForHash().get(key, item);
  143.     }
  144.     /**
  145.      * 获取hashKey对应的所有键值
  146.      *
  147.      * @param key 键
  148.      * @return 对应的多个键值
  149.      */
  150.     public Map<Object, Object> hmget(String key) {
  151.         return redisTemplate.opsForHash().entries(key);
  152.     }
  153.     /**
  154.      * HashSet
  155.      *
  156.      * @param key 键
  157.      * @param map 对应多个键值
  158.      */
  159.     public boolean hmset(String key, Map<String, Object> map) {
  160.         try {
  161.             redisTemplate.opsForHash().putAll(key, map);
  162.             return true;
  163.         } catch (Exception e) {
  164.             e.printStackTrace();
  165.             return false;
  166.         }
  167.     }
  168.     /**
  169.      * HashSet 并设置时间
  170.      *
  171.      * @param key  键
  172.      * @param map  对应多个键值
  173.      * @param time 时间(秒)
  174.      * @return true成功 false失败
  175.      */
  176.     public boolean hmset(String key, Map<String, Object> map, long time) {
  177.         try {
  178.             redisTemplate.opsForHash().putAll(key, map);
  179.             if (time > 0) {
  180.                 expire(key, time);
  181.             }
  182.             return true;
  183.         } catch (Exception e) {
  184.             e.printStackTrace();
  185.             return false;
  186.         }
  187.     }
  188.     /**
  189.      * 向一张hash表中放入数据,如果不存在将创建
  190.      *
  191.      * @param key   键
  192.      * @param item  项
  193.      * @param value 值
  194.      * @return true 成功 false失败
  195.      */
  196.     public boolean hset(String key, String item, Object value) {
  197.         try {
  198.             redisTemplate.opsForHash().put(key, item, value);
  199.             return true;
  200.         } catch (Exception e) {
  201.             e.printStackTrace();
  202.             return false;
  203.         }
  204.     }
  205.     /**
  206.      * 向一张hash表中放入数据,如果不存在将创建
  207.      *
  208.      * @param key   键
  209.      * @param item  项
  210.      * @param value 值
  211.      * @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
  212.      * @return true 成功 false失败
  213.      */
  214.     public boolean hset(String key, String item, Object value, long time) {
  215.         try {
  216.             redisTemplate.opsForHash().put(key, item, value);
  217.             if (time > 0) {
  218.                 expire(key, time);
  219.             }
  220.             return true;
  221.         } catch (Exception e) {
  222.             e.printStackTrace();
  223.             return false;
  224.         }
  225.     }
  226.     /**
  227.      * 删除hash表中的值
  228.      *
  229.      * @param key  键 不能为null
  230.      * @param item 项 可以使多个 不能为null
  231.      */
  232.     public void hdel(String key, Object... item) {
  233.         redisTemplate.opsForHash().delete(key, item);
  234.     }
  235.     /**
  236.      * 判断hash表中是否有该项的值
  237.      *
  238.      * @param key  键 不能为null
  239.      * @param item 项 不能为null
  240.      * @return true 存在 false不存在
  241.      */
  242.     public boolean hHasKey(String key, String item) {
  243.         return redisTemplate.opsForHash().hasKey(key, item);
  244.     }
  245.     /**
  246.      * hash递增 如果不存在,就会创建一个 并把新增后的值返回
  247.      *
  248.      * @param key  键
  249.      * @param item 项
  250.      * @param by   要增加几(大于0)
  251.      */
  252.     public double hincr(String key, String item, double by) {
  253.         return redisTemplate.opsForHash().increment(key, item, by);
  254.     }
  255.     /**
  256.      * hash递减
  257.      *
  258.      * @param key  键
  259.      * @param item 项
  260.      * @param by   要减少记(小于0)
  261.      */
  262.     public double hdecr(String key, String item, double by) {
  263.         return redisTemplate.opsForHash().increment(key, item, -by);
  264.     }
  265.     // ============================set=============================
  266.     /**
  267.      * 根据key获取Set中的所有值
  268.      *
  269.      * @param key 键
  270.      */
  271.     public Set<Object> sGet(String key) {
  272.         try {
  273.             return redisTemplate.opsForSet().members(key);
  274.         } catch (Exception e) {
  275.             e.printStackTrace();
  276.             return null;
  277.         }
  278.     }
  279.     /**
  280.      * 根据value从一个set中查询,是否存在
  281.      *
  282.      * @param key   键
  283.      * @param value 值
  284.      * @return true 存在 false不存在
  285.      */
  286.     public boolean sHasKey(String key, Object value) {
  287.         try {
  288.             return redisTemplate.opsForSet().isMember(key, value);
  289.         } catch (Exception e) {
  290.             e.printStackTrace();
  291.             return false;
  292.         }
  293.     }
  294.     /**
  295.      * 将数据放入set缓存
  296.      *
  297.      * @param key    键
  298.      * @param values 值 可以是多个
  299.      * @return 成功个数
  300.      */
  301.     public long sSet(String key, Object... values) {
  302.         try {
  303.             return redisTemplate.opsForSet().add(key, values);
  304.         } catch (Exception e) {
  305.             e.printStackTrace();
  306.             return 0;
  307.         }
  308.     }
  309.     /**
  310.      * 将set数据放入缓存
  311.      *
  312.      * @param key    键
  313.      * @param time   时间(秒)
  314.      * @param values 值 可以是多个
  315.      * @return 成功个数
  316.      */
  317.     public long sSetAndTime(String key, long time, Object... values) {
  318.         try {
  319.             Long count = redisTemplate.opsForSet().add(key, values);
  320.             if (time > 0) {
  321.                 expire(key, time);
  322.             }
  323.             return count;
  324.         } catch (Exception e) {
  325.             e.printStackTrace();
  326.             return 0;
  327.         }
  328.     }
  329.     /**
  330.      * 获取set缓存的长度
  331.      *
  332.      * @param key 键
  333.      */
  334.     public long sGetSetSize(String key) {
  335.         try {
  336.             return redisTemplate.opsForSet().size(key);
  337.         } catch (Exception e) {
  338.             e.printStackTrace();
  339.             return 0;
  340.         }
  341.     }
  342.     /**
  343.      * 移除值为value的
  344.      *
  345.      * @param key    键
  346.      * @param values 值 可以是多个
  347.      * @return 移除的个数
  348.      */
  349.     public long setRemove(String key, Object... values) {
  350.         try {
  351.             Long count = redisTemplate.opsForSet().remove(key, values);
  352.             return count;
  353.         } catch (Exception e) {
  354.             e.printStackTrace();
  355.             return 0;
  356.         }
  357.     }
  358.     // ===============================list=================================
  359.     /**
  360.      * 获取list缓存的内容
  361.      *
  362.      * @param key   键
  363.      * @param start 开始
  364.      * @param end   结束 0 到 -1代表所有值
  365.      */
  366.     public List<Object> lGet(String key, long start, long end) {
  367.         try {
  368.             return redisTemplate.opsForList().range(key, start, end);
  369.         } catch (Exception e) {
  370.             e.printStackTrace();
  371.             return null;
  372.         }
  373.     }
  374.     /**
  375.      * 获取list缓存的长度
  376.      *
  377.      * @param key 键
  378.      */
  379.     public long lGetListSize(String key) {
  380.         try {
  381.             return redisTemplate.opsForList().size(key);
  382.         } catch (Exception e) {
  383.             e.printStackTrace();
  384.             return 0;
  385.         }
  386.     }
  387.     /**
  388.      * 通过索引 获取list中的值
  389.      *
  390.      * @param key   键
  391.      * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
  392.      */
  393.     public Object lGetIndex(String key, long index) {
  394.         try {
  395.             return redisTemplate.opsForList().index(key, index);
  396.         } catch (Exception e) {
  397.             e.printStackTrace();
  398.             return null;
  399.         }
  400.     }
  401.     /**
  402.      * 将list放入缓存
  403.      *
  404.      * @param key   键
  405.      * @param value 值
  406.      */
  407.     public boolean lSet(String key, Object value) {
  408.         try {
  409.             redisTemplate.opsForList().rightPush(key, value);
  410.             return true;
  411.         } catch (Exception e) {
  412.             e.printStackTrace();
  413.             return false;
  414.         }
  415.     }
  416.     /**
  417.      * 将list放入缓存
  418.      *
  419.      * @param key   键
  420.      * @param value 值
  421.      * @param time  时间(秒)
  422.      */
  423.     public boolean lSet(String key, Object value, long time) {
  424.         try {
  425.             redisTemplate.opsForList().rightPush(key, value);
  426.             if (time > 0) {
  427.                 expire(key, time);
  428.             }
  429.             return true;
  430.         } catch (Exception e) {
  431.             e.printStackTrace();
  432.             return false;
  433.         }
  434.     }
  435.     /**
  436.      * 将list放入缓存
  437.      *
  438.      * @param key   键
  439.      * @param value 值
  440.      * @return
  441.      */
  442.     public boolean lSet(String key, List<Object> value) {
  443.         try {
  444.             redisTemplate.opsForList().rightPushAll(key, value);
  445.             return true;
  446.         } catch (Exception e) {
  447.             e.printStackTrace();
  448.             return false;
  449.         }
  450.     }
  451.     /**
  452.      * 将list放入缓存
  453.      *
  454.      * @param key   键
  455.      * @param value 值
  456.      * @param time  时间(秒)
  457.      * @return
  458.      */
  459.     public boolean lSet(String key, List<Object> value, long time) {
  460.         try {
  461.             redisTemplate.opsForList().rightPushAll(key, value);
  462.             if (time > 0) {
  463.                 expire(key, time);
  464.             }
  465.             return true;
  466.         } catch (Exception e) {
  467.             e.printStackTrace();
  468.             return false;
  469.         }
  470.     }
  471.     /**
  472.      * 根据索引修改list中的某条数据
  473.      *
  474.      * @param key   键
  475.      * @param index 索引
  476.      * @param value 值
  477.      * @return
  478.      */
  479.     public boolean lUpdateIndex(String key, long index, Object value) {
  480.         try {
  481.             redisTemplate.opsForList().set(key, index, value);
  482.             return true;
  483.         } catch (Exception e) {
  484.             e.printStackTrace();
  485.             return false;
  486.         }
  487.     }
  488.     /**
  489.      * 移除N个值为value
  490.      *
  491.      * @param key   键
  492.      * @param count 移除多少个
  493.      * @param value 值
  494.      * @return 移除的个数
  495.      */
  496.     public long lRemove(String key, long count, Object value) {
  497.         try {
  498.             Long remove = redisTemplate.opsForList().remove(key, count, value);
  499.             return remove;
  500.         } catch (Exception e) {
  501.             e.printStackTrace();
  502.             return 0;
  503.         }
  504.     }
  505. }
复制代码
2、朱老师的RedisUtils

点击查看代码[code]@Componentpublic class RedisUtils {    @Autowired    private RedisTemplate redisTemplate;    /**     * 根据key 获取过期时间     *     * @param key 键 不能为null     * @return 时间(秒) 返回0代表为永久有效     */    public long getExpire(String key) {        return redisTemplate.getExpire(key, TimeUnit.SECONDS);    }    /**     * 判断key是否存在     *     * @param key 键     * @return true 存在 false不存在     */    public boolean hasKey(String key) {        try {            return redisTemplate.hasKey(key);        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 删除缓存     *     * @param key 可以传一个值 或多个     */    @SuppressWarnings("unchecked")    public void del(String... key) {        if (key != null && key.length > 0) {            if (key.length == 1) {                redisTemplate.delete(key[0]);            } else {                redisTemplate.delete(CollectionUtils.arrayToList(key));            }        }    }    // ============================String=============================    /**     * 普通缓存获取     *     * @param key 键     * @return 值     */    public Object get(String key) {        return key == null ? null : redisTemplate.opsForValue().get(key);    }    /**     * 普通缓存放入     *     * @param key   键     * @param value 值     * @return true成功 false失败     */    public boolean set(String key, Object value) {        try {            redisTemplate.opsForValue().set(key, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 普通缓存放入并设置时间     *     * @param key   键     * @param value 值     * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期     * @return true成功 false 失败     */    public boolean set(String key, Object value, long time) {        try {            if (time > 0) {                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);            } else {                set(key, value);            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 递增     *     * @param key   键     * @param delta 要增加几(大于0)     * @return     */    public long incr(String key, long delta) {        if (delta < 0) {            throw new RuntimeException("递增因子必须大于0");        }        return redisTemplate.opsForValue().increment(key, delta);    }    /**     * 递减     *     * @param key   键     * @param delta 要减少几(小于0)     * @return     */    public long decr(String key, long delta) {        if (delta < 0) {            throw new RuntimeException("递减因子必须大于0");        }        return redisTemplate.opsForValue().increment(key, -delta);    }    /**     * HashGet     *     * @param key  键 不能为null     * @param item 项 不能为null     * @return 值     */    public Object hget(String key, String item) {        return redisTemplate.opsForHash().get(key, item);    }    /**     * 获取hashKey对应的所有键值     *     * @param key 键     * @return 对应的多个键值     */    public Map hmget(String key) {        return redisTemplate.opsForHash().entries(key);    }    /**     * HashSet     *     * @param key 键     * @param map 对应多个键值     * @return true 成功 false 失败     */    public boolean hmset(String key, Map map) {        try {            redisTemplate.opsForHash().putAll(key, map);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 指定缓存失效时间     *     * @param key  键     * @param time 时间(秒)     * @return     */    public boolean expire(String key, long time) {        try {            if (time > 0) {                redisTemplate.expire(key, time, TimeUnit.SECONDS);            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * HashSet 并设置时间     *     * @param key  键     * @param map  对应多个键值     * @param time 时间(秒)     * @return true成功 false失败     */    public boolean hmset(String key, Map map, long time) {        try {            redisTemplate.opsForHash().putAll(key, map);            if (time > 0) {                expire(key, time);            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 向一张hash表中放入数据,如果不存在将创建     *     * @param key   键     * @param item  项     * @param value 值     * @return true 成功 false失败     */    public boolean hset(String key, String item, Object value) {        try {            redisTemplate.opsForHash().put(key, item, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 向一张hash表中放入数据,如果不存在将创建     *     * @param key   键     * @param item  项     * @param value 值     * @param time  时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间     * @return true 成功 false失败     */    public boolean hset(String key, String item, Object value, long time) {        try {            redisTemplate.opsForHash().put(key, item, value);            if (time > 0) {                expire(key, time);            }            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 删除hash表中的值     *     * @param key  键 不能为null     * @param item 项 可以使多个 不能为null     */    public void hdel(String key, Object... item) {        redisTemplate.opsForHash().delete(key, item);    }    /**     * 判断hash表中是否有该项的值     *     * @param key  键 不能为null     * @param item 项 不能为null     * @return true 存在 false不存在     */    public boolean hHasKey(String key, String item) {        return redisTemplate.opsForHash().hasKey(key, item);    }    /**     * hash递增 如果不存在,就会创建一个 并把新增后的值返回     *     * @param key  键     * @param item 项     * @param by   要增加几(大于0)     * @return     */    public double hincr(String key, String item, double by) {        return redisTemplate.opsForHash().increment(key, item, by);    }    /**     * hash递减     *     * @param key  键     * @param item 项     * @param by   要减少记(小于0)     * @return     */    public double hdecr(String key, String item, double by) {        return redisTemplate.opsForHash().increment(key, item, -by);    }    /**     * 根据key获取Set中的所有值     *     * @param key 键     * @return     */    public Set sGet(String key) {        try {            return redisTemplate.opsForSet().members(key);        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    /**     * 根据value从一个set中查询,是否存在     *     * @param key   键     * @param value 值     * @return true 存在 false不存在     */    public boolean sHasKey(String key, Object value) {        try {            return redisTemplate.opsForSet().isMember(key, value);        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 将数据放入set缓存     *     * @param key    键     * @param values 值 可以是多个     * @return 成功个数     */    public long sSet(String key, Object... values) {        try {            return redisTemplate.opsForSet().add(key, values);        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 将set数据放入缓存     *     * @param key    键     * @param time   时间(秒)     * @param values 值 可以是多个     * @return 成功个数     */    public long sSetAndTime(String key, long time, Object... values) {        try {            Long count = redisTemplate.opsForSet().add(key, values);            if (time > 0)                expire(key, time);            return count;        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 获取set缓存的长度     *     * @param key 键     * @return     */    public long sGetSetSize(String key) {        try {            return redisTemplate.opsForSet().size(key);        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 移除值为value的     *     * @param key    键     * @param values 值 可以是多个     * @return 移除的个数     */    public long setRemove(String key, Object... values) {        try {            Long count = redisTemplate.opsForSet().remove(key, values);            return count;        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 获取list缓存的内容     *     * @param key   键     * @param start 开始     * @param end   结束 0 到 -1代表所有值     * @return     */    public List lGet(String key, long start, long end) {        try {            return redisTemplate.opsForList().range(key, start, end);        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    /**     * 获取list缓存的长度     *     * @param key 键     * @return     */    public long lGetListSize(String key) {        try {            return redisTemplate.opsForList().size(key);        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 通过索引 获取list中的值     *     * @param key   键     * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index 0)                expire(key, time);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 将list放入缓存     *     * @param key   键     * @param value 值     * @return     */    public boolean lSet(String key, List value) {        try {            redisTemplate.opsForList().rightPushAll(key, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 将list放入缓存     *     * @param key   键     * @param value 值     * @param time  时间(秒)     * @return     */    public boolean lSet(String key, List value, long time) {        try {            redisTemplate.opsForList().rightPushAll(key, value);            if (time > 0)                expire(key, time);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 根据索引修改list中的某条数据     *     * @param key   键     * @param index 索引     * @param value 值     * @return     */    public boolean lUpdateIndex(String key, long index, Object value) {        try {            redisTemplate.opsForList().set(key, index, value);            return true;        } catch (Exception e) {            e.printStackTrace();            return false;        }    }    /**     * 移除N个值为value     *     * @param key   键     * @param count 移除多少个     * @param value 值     * @return 移除的个数     */    public long lRemove(String key, long count, Object value) {        try {            Long remove = redisTemplate.opsForList().remove(key, count, value);            return remove;        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    /**     * 加锁     *     * @param key     * @param value     * @param expire     * @return 是否加锁成功     */    public boolean lock(String key, Object value, long expire) {        if (null == value) {            value = new Byte[]{1};        }        if (redisTemplate.opsForValue().setIfAbsent(key, value)) {            expire(key, expire);            return true;        }        return false;    }    /**     * 解锁     *     * @param key     */    public void delLock(String key) {        redisTemplate.delete(key);    }    public Set keys(String nameSpace) {        return redisTemplate.keys("*" + nameSpace + "*");    }    /**     * 当前方法的主要作用是什么?     * boundValueOps主要用于操作Redis的字符串的,它可以先在boundValueOps(“key”)中写上key名,然后接方法名,     * 这样以后的操作就不需要写key的名称,比如redisTemplate.boundValueOps(“key”).set(“value”);     * 当我们需要对一个key同时做多个操作时,我们做如下操作:     *先设置为2然后自增1     *      BoundValueOperations operations = redisTemplate.boundValueOps("key");     *      operations.set("2");     *      operations.increment();     */    public boolean checkFreq(String key, long count, long ttl) {        boolean exists = redisTemplate.hasKey(key);        BoundValueOperations valueOps = redisTemplate.boundValueOps(key);        Long value = valueOps.increment(1);        if (value == null) value = count;        if (!exists) {            redisTemplate.expire(key, ttl, TimeUnit.SECONDS);        }        return value
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

络腮胡菲菲

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表