Class RedisUtil

java.lang.Object
cn.sinozg.applet.common.utils.RedisUtil

public class RedisUtil extends Object
redis 工具类
  • Field Details

    • REDIS_TEMPLATE

      protected static final org.springframework.data.redis.core.RedisTemplate<String,Object> REDIS_TEMPLATE
    • STRING_REDIS_TEMPLATE

      protected static final org.springframework.data.redis.core.RedisTemplate<String,String> STRING_REDIS_TEMPLATE
  • Constructor Details

    • RedisUtil

      public RedisUtil()
  • Method Details

    • setCacheObject

      public static <T> void setCacheObject(String key, T value)
      缓存基本的对象,Integer、String、实体类等
      Parameters:
      key - 缓存的键值
      value - 缓存的值
    • setCacheObject

      public static <T> void setCacheObject(String key, T value, long timeout, TimeUnit timeUnit)
      缓存基本的对象,Integer、String、实体类等
      Parameters:
      key - 缓存的键值
      value - 缓存的值
      timeout - 时间
      timeUnit - 时间颗粒度
    • setCacheObject

      public static <T> void setCacheObject(String key, T value, long timeout)
      设置缓存
      Type Parameters:
      T - value的类型
      Parameters:
      key - key
      value - 值
      timeout - 有效时间
    • setCacheObject

      public static <T> void setCacheObject(String key, T value, Duration duration)
      Type Parameters:
      T - value的类型
      Parameters:
      key - key
      value - 值
      duration - 有效时间
    • setCacheObjectBatch

      public static <T> void setCacheObjectBatch(Map<String,T> data, Function<String,String> fun, Duration duration)
      通过管道模式写入数据
      Type Parameters:
      T - 数据类型
      Parameters:
      data - 数据
      fun - 处理key值
      duration - 过期时间
    • setIfAbsentObject

      public static <T> boolean setIfAbsentObject(String key, T value, Duration duration)
      设置值 如果不存在就设置

      并且key存在返回false,key不存在就返回ture

      Type Parameters:
      T - 类型
      Parameters:
      key - key
      value - value
      duration - 有效期
      Returns:
      key是否存在
    • expire

      public static boolean expire(String key, long timeout)
      设置有效时间
      Parameters:
      key - Redis键
      timeout - 超时时间
      Returns:
      true=设置成功;false=设置失败
    • expire

      public static boolean expire(String key, Duration duration)
      设置有效期
      Parameters:
      key - Redis键
      duration - 时间
      Returns:
      true=设置成功;false=设置失败
    • expire

      public static boolean expire(String key, long timeout, TimeUnit unit)
      设置有效时间
      Parameters:
      key - Redis键
      timeout - 超时时间
      unit - 时间单位
      Returns:
      true=设置成功;false=设置失败
    • objExpireTime

      public static Long objExpireTime(String key)
      判断是否有效
      Parameters:
      key - redis的key
      Returns:
      有效期
    • getCacheObject

      public static <T> T getCacheObject(String key)
      获得缓存的基本对象。
      Parameters:
      key - 缓存键值
      Returns:
      缓存键值对应的数据
    • cacheByPipelined

      public static <T> List<T> cacheByPipelined(List<String> keyList)
      管道模式获取到数据
      Type Parameters:
      T - 返回类型
      Parameters:
      keyList - key集合
      Returns:
      所有的value
    • deleteObject

      public static boolean deleteObject(String key)
      删除单个对象
      Parameters:
      key - redis的key
    • deleteKeys

      public static boolean deleteKeys(String key)
      根据key 批量删除
      Parameters:
      key - key
      Returns:
      是否成功
    • deleteObject

      public static long deleteObject(Collection<String> collection)
      删除集合对象
      Parameters:
      collection - 多个对象
      Returns:
      删除是否成功
    • getIncr

      public static long getIncr(String key, Duration duration)
      获取自增长的数值
      Parameters:
      key - key值
      duration - 过期时间 秒为单位
      Returns:
      返回当前的值
    • getIncr

      public static long getIncr(String key, Duration duration, Supplier<Long> supplier)
      获取自增长的数值
      Parameters:
      key - key值
      duration - 过期时间 秒为单位
      supplier - 初始化值
      Returns:
      返回当前的值
    • setIncr

      public static long setIncr(String key, long value)
      设置自增长的值
      Parameters:
      key - redis key
      value - 值
      Returns:
      返回当前值
    • setCacheMap

      public static <T> void setCacheMap(String key, Map<String,T> dataMap)
      缓存Map
      Parameters:
      key - key值
      dataMap - 数据map
    • getCacheMap

      public static <T> Map<String,T> getCacheMap(String key)
      获得缓存的Map
      Parameters:
      key - key值
      Returns:
      对应的map
    • mapPut

      public static void mapPut(String key, String hashKey, String value)
      添加map
      Parameters:
      key - key
      hashKey - map key
      value - 值
    • mapRemove

      public static void mapRemove(String key, Object... hashKey)
      删除map里的某些
      Parameters:
      key - key
      hashKey - map key
    • keys

      public static Collection<String> keys(String pattern)
      获得缓存的基本对象列表
      Parameters:
      pattern - 字符串前缀
      Returns:
      对象列表
    • setCacheSet

      public static Boolean setCacheSet(String key, String value, double score)
      添加一个元素, zset与set最大的区别就是每个元素都有一个score,因此有个排序的辅助功能;
      Parameters:
      key - key值
      value - value值
      score - 权重
    • removeRangeCacheSet

      public static Long removeRangeCacheSet(String key, long start, long end)
      删除元素 zrem
      Parameters:
      key - key值
      start - 开始
      end - 结束
    • removeCacheSet

      public static Long removeCacheSet(String key, Object... value)
      删除元素 zrem
      Parameters:
      key - key值
      value - value值
    • rangeWithScore

      public static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> rangeWithScore(String key, long start, long end)
      查询集合中指定顺序的值和score,0, -1 表示获取全部的集合内容
      Parameters:
      key - key值
      start - 开始
      end - 结束
      Returns:
      结果集
    • sortRangeWithScoresCacheSet

      public static Set<org.springframework.data.redis.core.ZSetOperations.TypedTuple<String>> sortRangeWithScoresCacheSet(String key, double min, double max)
      根据score的值,来获取满足条件的集合
      Parameters:
      key - key值
      min - 最小值
      max - 最大值
      Returns:
      结果集