Class RedisOpsImpl

java.lang.Object
top.tangyh.basic.cache.repository.impl.RedisOpsImpl
All Implemented Interfaces:
CacheOps, CachePlusOps

public class RedisOpsImpl extends Object implements CacheOps, CachePlusOps
Redis Repository redis 基本操作 可扩展,基本够用了
Author:
zuihou
  • Constructor Details

    • RedisOpsImpl

      public RedisOpsImpl(RedisOps redisOps)
  • Method Details

    • getRedisTemplate

      public org.springframework.data.redis.core.RedisTemplate<String,Object> getRedisTemplate()
      获取 RedisTemplate对象
    • del

      public Long del(@NonNull CacheKey... keys)
      Description copied from interface: CacheOps
      删除指定的key
      Specified by:
      del in interface CacheOps
      Parameters:
      keys - 多个key
      Returns:
      删除个数
    • del

      public Long del(@NonNull Collection<CacheKey> keys)
      Description copied from interface: CacheOps
      删除指定的key
      Specified by:
      del in interface CacheOps
      Parameters:
      keys - 多个key
      Returns:
      删除个数
    • del

      public Long del(String... keys)
      Description copied from interface: CacheOps
      删除指定的key
      Specified by:
      del in interface CacheOps
      Parameters:
      keys - 多个key
      Returns:
      删除个数
    • exists

      public Boolean exists(@NonNull CacheKey key)
      Description copied from interface: CacheOps
      判断指定的key 是否存在
      Specified by:
      exists in interface CacheOps
      Parameters:
      key - key
      Returns:
      是否存在
    • set

      public void set(@NonNull CacheKey key, Object value, boolean... cacheNullValues)
      Description copied from interface: CacheOps
      添加到带有 过期时间的 缓存
      Specified by:
      set in interface CacheOps
      Parameters:
      key - redis主键
      value - 值
      cacheNullValues - 是否缓存null对象
    • get

      public <T> CacheResult<T> get(@NonNull CacheKey key, boolean... cacheNullValues)
      Description copied from interface: CacheOps
      根据key获取对象
      Specified by:
      get in interface CacheOps
      Parameters:
      key - redis主键
      cacheNullValues - 是否缓存null对象
      Returns:
      值 不存在时,返回null
    • get

      public <T> CacheResult<T> get(@NonNull String key, boolean... cacheNullValues)
      Description copied from interface: CacheOps
      根据key获取对象
      Specified by:
      get in interface CacheOps
      Parameters:
      key - redis主键
      cacheNullValues - 是否缓存null对象
      Returns:
      值 不存在时,返回null
    • find

      public <T> List<CacheResult<T>> find(@NonNull Collection<CacheKey> keys)
      Description copied from interface: CacheOps
      根据keys获取对象
      Specified by:
      find in interface CacheOps
      Parameters:
      keys - redis主键
      Returns:
      值 不存在时,返回空集合
    • get

      public <T> CacheResult<T> get(@NonNull CacheKey key, Function<CacheKey,? extends T> loader, boolean... cacheNullValues)
      Description copied from interface: CacheOps
      根据key获取对象 不存在时,调用function回调获取数据,并set进入,然后返回
      Specified by:
      get in interface CacheOps
      Parameters:
      key - redis主键
      loader - 加载器
      cacheNullValues - 是否缓存null对象
      Returns:
    • flushDb

      public void flushDb()
      清空redis存储的数据
      Specified by:
      flushDb in interface CacheOps
    • incr

      public Long incr(@NonNull CacheKey key)
      Description copied from interface: CacheOps
      为键 key 储存的数字值加上一。
      Specified by:
      incr in interface CacheOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      返回键 key 在执行加一操作之后的值。
    • getCounter

      public Long getCounter(CacheKey key, Function<CacheKey,Long> loader)
      Description copied from interface: CacheOps
      获取key中存放的Long值
      Specified by:
      getCounter in interface CacheOps
      Parameters:
      key - 一定不能为 null.
      loader - 加载
      Returns:
      key中存储的的数字
    • incrBy

      public Long incrBy(@NonNull CacheKey key, long increment)
      Description copied from interface: CacheOps
      为键 key 储存的数字值加上increment。
      Specified by:
      incrBy in interface CacheOps
      Parameters:
      key - 一定不能为 null.
      increment - 增量值
      Returns:
      返回键 key 在执行加一操作之后的值。
    • incrByFloat

      public Double incrByFloat(@NonNull CacheKey key, double increment)
      Description copied from interface: CacheOps
      为键 key 储存的数字值加上一。
      Specified by:
      incrByFloat in interface CacheOps
      Parameters:
      key - 一定不能为 null.
      increment - 增量值
      Returns:
      返回键 key 在执行加一操作之后的值。
    • decr

      public Long decr(@NonNull CacheKey key)
      Description copied from interface: CacheOps
      为键 key 储存的数字值减去一。
      Specified by:
      decr in interface CacheOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      在减去增量 1 之后, 键 key 的值。
    • decrBy

      public Long decrBy(@NonNull CacheKey key, long decrement)
      Description copied from interface: CacheOps
      将 key 所储存的值减去减量 decrement 。
      Specified by:
      decrBy in interface CacheOps
      Parameters:
      key - 一定不能为 null.
      decrement - 增量值
      Returns:
      在减去增量 decrement 之后, 键 key 的值。
    • keys

      public Set<String> keys(@NonNull String pattern)
      Description copied from interface: CachePlusOps
      查找所有符合给定模式 pattern 的 key 。

      例子: KEYS * 匹配数据库中所有 key 。 KEYS h?llo 匹配 hello , hallo 和 hxllo 等。 KEYS a*cde 匹配 acde 和 aeeeeecde 等。 KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。

      特殊符号用 \ 隔开

      Specified by:
      keys in interface CachePlusOps
      Parameters:
      pattern - 表达式
      Returns:
      符合给定模式的 key 列表
    • scan

      public List<String> scan(@NonNull String pattern)
      Description copied from interface: CachePlusOps
      查找所有符合给定模式 pattern 的 key 。

      例子: KEYS * 匹配数据库中所有 key 。 KEYS h?llo 匹配 hello , hallo 和 hxllo 等。 KEYS a*cde 匹配 acde 和 aeeeeecde 等。 KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。

      特殊符号用 \ 隔开

      Specified by:
      scan in interface CachePlusOps
      Parameters:
      pattern - 表达式
      Returns:
      符合给定模式的 key 列表
    • scanUnlink

      public void scanUnlink(@NonNull String pattern)
      Description copied from interface: CachePlusOps
      查找所有符合给定模式 pattern 的 key ,并将其删除

      例子: KEYS * 匹配数据库中所有 key 。 KEYS h?llo 匹配 hello , hallo 和 hxllo 等。 KEYS a*cde 匹配 acde 和 aeeeeecde 等。 KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。

      特殊符号用 \ 隔开

      Specified by:
      scanUnlink in interface CachePlusOps
      Parameters:
      pattern - 表达式
    • expire

      public Boolean expire(@NonNull CacheKey key)
      Description copied from interface: CachePlusOps
      为给定 key 设置生存时间,当 key 过期时(生存时间为 0 ),它会被自动删除。
      Specified by:
      expire in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      是否成功
    • persist

      public Boolean persist(@NonNull CacheKey key)
      Description copied from interface: CachePlusOps
      移除给定 key 的生存时间,将这个 key 从『易失的』(带生存时间 key )转换成『持久的』(一个不带生存时间、永不过期的 key )。
      Specified by:
      persist in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      是否成功
    • type

      public String type(@NonNull CacheKey key)
      Description copied from interface: CachePlusOps
      返回 key 所储存的值的类型。
      Specified by:
      type in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      none (key不存在)、string (字符串)、list (列表)、set (集合)、zset (有序集)、hash (哈希表) 、stream (流)、caffeine(内存)
    • ttl

      public Long ttl(@NonNull CacheKey key)
      Description copied from interface: CachePlusOps
      以秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
      Specified by:
      ttl in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,以秒为单位,返回 key 的剩余生存时间。
    • pTtl

      public Long pTtl(@NonNull CacheKey key)
      Description copied from interface: CachePlusOps
      以毫秒为单位,返回给定 key 的剩余生存时间(TTL, time to live)。
      Specified by:
      pTtl in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      当 key 不存在时,返回 -2 。当 key 存在但没有设置剩余生存时间时,返回 -1 。否则,以毫秒为单位,返回 key 的剩余生存时间
    • hSet

      public void hSet(@NonNull CacheHashKey key, Object value, boolean... cacheNullValues)
      Description copied from interface: CachePlusOps
      将哈希表 key 中的域 field 的值设为 value 。
      Specified by:
      hSet in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      value - 值
      cacheNullValues - 是否缓存空对象
    • hGet

      public <T> CacheResult<T> hGet(@NonNull CacheHashKey key, boolean... cacheNullValues)
      Description copied from interface: CachePlusOps
      返回哈希表 key 中给定域 field 的值。
      Specified by:
      hGet in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      cacheNullValues - 是否缓存空值
      Returns:
      默认情况下返回给定域的值, 如果给定域不存在于哈希表中, 又或者给定的哈希表并不存在, 那么命令返回 nil
    • hGet

      public <T> CacheResult<T> hGet(@NonNull CacheHashKey key, Function<CacheHashKey,T> loader, boolean... cacheNullValues)
      Description copied from interface: CachePlusOps
      返回哈希表 key 中给定域 field 的值。
      Specified by:
      hGet in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      loader - 加载器
      cacheNullValues - 是否缓存空值
      Returns:
      默认情况下返回给定域的值, 如果给定域不存在于哈希表中, 又或者给定的哈希表并不存在, 那么命令返回 nil
    • hExists

      public Boolean hExists(@NonNull CacheHashKey cacheHashKey)
      Description copied from interface: CachePlusOps
      检查给定域 field 是否存在于哈希表 hash 当中
      Specified by:
      hExists in interface CachePlusOps
      Parameters:
      cacheHashKey - 一定不能为 null.
      Returns:
      是否存在
    • hDel

      public Long hDel(@NonNull String key, Object... fields)
      Description copied from interface: CachePlusOps
      删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
      Specified by:
      hDel in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      fields - 一定不能为 null.
      Returns:
      删除的数量
    • hDel

      public Long hDel(@NonNull CacheHashKey cacheHashKey)
      Description copied from interface: CachePlusOps
      删除哈希表 key 中的指定域,不存在的域将被忽略。
      Specified by:
      hDel in interface CachePlusOps
      Parameters:
      cacheHashKey - 一定不能为 null.
      Returns:
      删除的数量
    • hLen

      public Long hLen(@NonNull CacheHashKey key)
      Description copied from interface: CachePlusOps
      返回哈希表 key 中域的数量。
      Specified by:
      hLen in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      哈希表中域的数量。
    • hIncrBy

      public Long hIncrBy(@NonNull CacheHashKey key, long increment)
      Description copied from interface: CachePlusOps
      为哈希表 key 中的域 field 的值加上增量 increment 。
      Specified by:
      hIncrBy in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      increment - 增量
      Returns:
      执行 HINCRBY 命令之后,哈希表 key 中域 field 的值
    • hIncrBy

      public Double hIncrBy(@NonNull CacheHashKey key, double increment)
      Description copied from interface: CachePlusOps
      为哈希表 key 中的域 field 的值加上增量 increment 。
      Specified by:
      hIncrBy in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      increment - 增量
      Returns:
      执行 HINCRBY 命令之后,哈希表 key 中域 field 的值
    • hKeys

      public <HK> Set<HK> hKeys(@NonNull CacheHashKey key)
      Description copied from interface: CachePlusOps
      返回哈希表 key 中的所有域。
      Specified by:
      hKeys in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      所有的 filed
    • hVals

      public <HV> List<CacheResult<HV>> hVals(@NonNull CacheHashKey key)
      Description copied from interface: CachePlusOps
      返回哈希表 key 中所有域的值。
      Specified by:
      hVals in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      一个包含哈希表中所有值的表。
    • hGetAll

      public <K, V> Map<K,CacheResult<V>> hGetAll(@NonNull CacheHashKey key)
      Description copied from interface: CachePlusOps
      返回哈希表 key 中,所有的域和值。 在返回值里,紧跟每个域名(field name)之后是域的值(value),所以返回值的长度是哈希表大小的两倍。
      Specified by:
      hGetAll in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      以列表形式返回哈希表的域和域的值
    • hGetAll

      public <K, V> Map<K,CacheResult<V>> hGetAll(@NonNull CacheHashKey key, Function<CacheHashKey,Map<K,V>> loader, boolean... cacheNullValues)
      Description copied from interface: CachePlusOps
      返回哈希表 key 中,所有的域和值。 在返回值里,紧跟每个域名(field name)之后是域的值(value),所以返回值的长度是哈希表大小的两倍。
      Specified by:
      hGetAll in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      loader - 加载回调
      cacheNullValues - 缓存空值
      Returns:
      以列表形式返回哈希表的域和域的值
    • sAdd

      public Long sAdd(@NonNull CacheKey key, Object value)
      Description copied from interface: CachePlusOps
      将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略。 假如 key 不存在,则创建一个只包含 member 元素作成员的集合。 当 key 不是集合类型时,返回一个错误。
      Specified by:
      sAdd in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      value - 值
      Returns:
      被添加到集合中的新元素的数量,不包括被忽略的元素。
    • sRem

      public Long sRem(@NonNull CacheKey key, Object... members)
      Description copied from interface: CachePlusOps
      移除集合 key 中的一个或多个 member 元素,不存在的 member 元素会被忽略。 当 key 不是集合类型,返回一个错误。
      Specified by:
      sRem in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      members - 元素
      Returns:
      被成功移除的元素的数量,不包括被忽略的元素
    • sMembers

      public Set<Object> sMembers(@NonNull CacheKey key)
      Description copied from interface: CachePlusOps
      返回集合 key 中的所有成员。 不存在的 key 被视为空集合。
      Specified by:
      sMembers in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      集合中的所有成员。
    • sPop

      public <T> T sPop(@NonNull CacheKey key)
      Description copied from interface: CachePlusOps
      移除并返回集合中的一个随机元素。 如果只想获取一个随机元素,但不想该元素从集合中被移除的话,可以使用 SRANDMEMBER 命令。
      Specified by:
      sPop in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      被移除的随机元素。 当 key 不存在或 key 是空集时,返回 nil 。
    • sCard

      public Long sCard(@NonNull CacheKey key)
      Description copied from interface: CachePlusOps
      返回集合 key 的基数(集合中元素的数量)。
      Specified by:
      sCard in interface CachePlusOps
      Parameters:
      key - 一定不能为 null.
      Returns:
      集合的基数。 当 key 不存在时,返回 0 。