Class RedisServiceImpl

java.lang.Object
net.wenzuo.atom.redis.service.impl.RedisServiceImpl
All Implemented Interfaces:
RedisService

@ConditionalOnProperty(value="atom.redis.redis-service", matchIfMissing=true) public class RedisServiceImpl extends Object implements RedisService
Since:
2023-06-15
Author:
Catch
  • Constructor Details

    • RedisServiceImpl

      public RedisServiceImpl()
  • Method Details

    • set

      public void set(String key, Object value)
      Description copied from interface: RedisService
      保存 value
      Specified by:
      set in interface RedisService
      Parameters:
      key - 键
      value - 值
    • set

      public void set(String key, Object value, long timeout, TimeUnit unit)
      Description copied from interface: RedisService
      保存 value 并设置过期时间
      Specified by:
      set in interface RedisService
      Parameters:
      key - 键
      value - 值
      timeout - 超时时间
      unit - 时间单位
    • set

      public void set(String key, Object value, Duration duration)
      Description copied from interface: RedisService
      保存 value 并设置过期时间
      Specified by:
      set in interface RedisService
      Parameters:
      key - 键
      value - 值
      duration - 超时时间
    • setIfAbsent

      public Boolean setIfAbsent(String key, Object value)
      Description copied from interface: RedisService
      如果 key 不存在则保存 value, 反之不保存
      Specified by:
      setIfAbsent in interface RedisService
      Parameters:
      key - 键
      value - 值
      Returns:
      是否保存成功
    • setIfAbsent

      public Boolean setIfAbsent(String key, Object value, long timeout, TimeUnit unit)
      Description copied from interface: RedisService
      如果 key 不存在则保存 value 并设置过期时间, 反之不保存
      Specified by:
      setIfAbsent in interface RedisService
      Parameters:
      key - 键
      value - 值
      timeout - 超时时间
      unit - 时间单位
      Returns:
      是否保存成功
    • setIfAbsent

      public Boolean setIfAbsent(String key, Object value, Duration timeout)
      Description copied from interface: RedisService
      如果 key 不存在则保存 value 并设置过期时间, 反之不保存
      Specified by:
      setIfAbsent in interface RedisService
      Parameters:
      key - 键
      value - 值
      timeout - 超时时间
      Returns:
      是否保存成功
    • setIfPresent

      public Boolean setIfPresent(String key, Object value)
      Description copied from interface: RedisService
      如果 key 存在则保存 value, 反之不保存
      Specified by:
      setIfPresent in interface RedisService
      Parameters:
      key - 键
      value - 值
      Returns:
      是否保存成功
    • setIfPresent

      public Boolean setIfPresent(String key, Object value, long timeout, TimeUnit unit)
      Description copied from interface: RedisService
      如果 key 存在则保存 value 并设置过期时间, 反之不保存
      Specified by:
      setIfPresent in interface RedisService
      Parameters:
      key - 键
      value - 值
      timeout - 超时时间
      unit - 时间单位
      Returns:
      是否保存成功
    • setIfPresent

      public Boolean setIfPresent(String key, Object value, Duration timeout)
      Description copied from interface: RedisService
      如果 key 存在则保存 value 并设置过期时间, 反之不保存
      Specified by:
      setIfPresent in interface RedisService
      Parameters:
      key - 键
      value - 值
      timeout - 超时时间
      Returns:
      是否保存成功
    • multiSet

      public void multiSet(Map<String,?> map)
      Description copied from interface: RedisService
      批量设置 key-value ,使用 redis mset 命令
      Specified by:
      multiSet in interface RedisService
      Parameters:
      map - key-value
    • multiSetIfAbsent

      public Boolean multiSetIfAbsent(Map<String,?> map)
      Description copied from interface: RedisService
      批量设置 key-value ,使用 redis mset 命令, 如果 key 不存在则保存 value, 反之不保存
      Specified by:
      multiSetIfAbsent in interface RedisService
      Parameters:
      map - key-value
    • get

      public <T> T get(String key, Class<T> clazz)
      Description copied from interface: RedisService
      获取 value
      Specified by:
      get in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      clazz - 类型
      Returns:
    • get

      public <T> T get(String key, Class<?> wrapper, Class<?>... inners)
      Description copied from interface: RedisService
      获取泛型 value

      如:List<Object> list = redisService.get(key, List.class, Object.class)

      Specified by:
      get in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      wrapper - 包装类
      inners - 内部类
      Returns:
    • getAndDelete

      public <T> T getAndDelete(String key, Class<T> clazz)
      Description copied from interface: RedisService
      获取 value 并删除 key
      Specified by:
      getAndDelete in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      clazz - 类型
      Returns:
    • getAndDelete

      public <T> T getAndDelete(String key, Class<?> wrapper, Class<?>... inners)
      Description copied from interface: RedisService
      获取泛型 value 并删除 key

      如:List<Object> list = redisService.getAndDelete(key, List.class, Object.class)

      Specified by:
      getAndDelete in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      wrapper - 包装类
      inners - 内部类
      Returns:
    • getAndExpire

      public <T> T getAndExpire(String key, long timeout, TimeUnit unit, Class<T> clazz)
      Description copied from interface: RedisService
      获取 value 并设置过期时间
      Specified by:
      getAndExpire in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      timeout - 超时时间
      unit - 时间单位
      clazz - 类型
      Returns:
    • getAndExpire

      public <T> T getAndExpire(String key, long timeout, TimeUnit unit, Class<?> wrapper, Class<?>... inners)
      Description copied from interface: RedisService
      获取泛型 value 并设置过期时间

      如:List<Object> list = redisService.getAndExpire(key, 1, TimeUnit.MINUTES, List.class, Object.class)

      Specified by:
      getAndExpire in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      timeout - 超时时间
      unit - 时间单位
      wrapper - 包装类
      inners - 内部类
      Returns:
    • getAndExpire

      public <T> T getAndExpire(String key, Duration timeout, Class<T> clazz)
      Description copied from interface: RedisService
      获取 value 并设置过期时间
      Specified by:
      getAndExpire in interface RedisService
      Parameters:
      key - 键
      timeout - 超时时间
      clazz - 类型
      Returns:
    • getAndExpire

      public <T> T getAndExpire(String key, Duration timeout, Class<?> wrapper, Class<?>... inners)
      Description copied from interface: RedisService
      获取泛型 value 并设置过期时间

      如:List<Object> list = redisService.getAndExpire(key, Duration.ofMinutes(1), List.class, Object.class)

      Specified by:
      getAndExpire in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      timeout - 超时时间
      wrapper - 包装类
      inners - 内部类
      Returns:
    • getAndPersist

      public <T> T getAndPersist(String key, Class<T> clazz)
      Description copied from interface: RedisService
      获取 value 并持久化 key, 此操作会移除 key 的过期时间
      Specified by:
      getAndPersist in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      clazz - 类型
      Returns:
    • getAndPersist

      public <T> T getAndPersist(String key, Class<?> wrapper, Class<?>... inners)
      Description copied from interface: RedisService
      获取泛型 value 并持久化 key, 此操作会移除 key 的过期时间

      如:List<Object> list = redisService.getAndPersist(key, List.class, Object.class)

      Specified by:
      getAndPersist in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      wrapper - 包装类
      inners - 内部类
      Returns:
    • getAndSet

      public <T> T getAndSet(String key, Object value, Class<T> clazz)
      Description copied from interface: RedisService
      保存 value 并返回旧的 value
      Specified by:
      getAndSet in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      value - 新值
      clazz - 类型
      Returns:
      旧值
    • getAndSet

      public <T> T getAndSet(String key, Object value, Class<?> wrapper, Class<?>... inners)
      Description copied from interface: RedisService
      保存泛型 value 并返回旧的 value

      如:List<Object> list = redisService.getAndSet(key, value, List.class, Object.class)

      Specified by:
      getAndSet in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      value - 新值
      wrapper - 包装类
      inners - 内部类
      Returns:
      旧值
    • multiGet

      public <T> List<T> multiGet(Collection<String> keys, Class<T> clazz)
      Description copied from interface: RedisService
      批量获取 value, 按键的顺序排列,不存在则用 null 填充
      Specified by:
      multiGet in interface RedisService
      Parameters:
      keys - 键集合
      Returns:
      值集合
    • increment

      public Long increment(String key)
      Description copied from interface: RedisService
      按步长为 1 递增 value
      Specified by:
      increment in interface RedisService
      Parameters:
      key - 键
      Returns:
      递增后的值
    • increment

      public Long increment(String key, long delta)
      Description copied from interface: RedisService
      按步长为 delta 递增 value
      Specified by:
      increment in interface RedisService
      Parameters:
      key - 键
      Returns:
      递增后的值
    • increment

      public Double increment(String key, double delta)
      Description copied from interface: RedisService
      按步长为 delta 递增 value
      Specified by:
      increment in interface RedisService
      Parameters:
      key - 键
      Returns:
      递增后的值
    • decrement

      public Long decrement(String key)
      Description copied from interface: RedisService
      按步长为 delta 递减 value
      Specified by:
      decrement in interface RedisService
      Parameters:
      key - 键
      Returns:
      递减后的值
    • decrement

      public Long decrement(String key, long delta)
      Description copied from interface: RedisService
      按步长为 delta 递减 value
      Specified by:
      decrement in interface RedisService
      Parameters:
      key - 键
      Returns:
      递减后的值
    • hasKey

      public Boolean hasKey(String key)
      Description copied from interface: RedisService
      是否存在 key
      Specified by:
      hasKey in interface RedisService
      Parameters:
      key - 键
      Returns:
      是否存在
    • countExistingKeys

      public Long countExistingKeys(Collection<String> keys)
      Description copied from interface: RedisService
      获取 key 集合中存在的 key 数量
      Specified by:
      countExistingKeys in interface RedisService
      Parameters:
      keys - 键集合
      Returns:
      存在的 key 数量
    • delete

      public Boolean delete(String key)
      Description copied from interface: RedisService
      删除 key
      Specified by:
      delete in interface RedisService
      Parameters:
      key - 键
      Returns:
      是否删除成功
    • delete

      public Long delete(Collection<String> keys)
      Description copied from interface: RedisService
      批量删除 key
      Specified by:
      delete in interface RedisService
      Parameters:
      keys - 键集合
      Returns:
      删除的数量
    • unlink

      public Boolean unlink(String key)
      Description copied from interface: RedisService
      从 key 空间中取消链接. 与 delete 不同,这里的内存回收是异步发生的
      Specified by:
      unlink in interface RedisService
      Parameters:
      key - 键
      Returns:
      是否成功
    • unlink

      public Long unlink(Collection<String> keys)
      Description copied from interface: RedisService
      批量从 key 空间中取消链接. 与 delete 不同,这里的内存回收是异步发生的
      Specified by:
      unlink in interface RedisService
      Parameters:
      keys - 键集合
      Returns:
      取消链接的数量
    • keys

      public Set<String> keys(String pattern)
      Description copied from interface: RedisService
      找到与模式匹配的所有键

      匹配示例:

      h?llo matches hello, hallo and hxllo

      h*llo matches hllo and heeeello

      h[ae]llo matches hello and hallo, but not hillo

      h[^e]llo matches hallo, hbllo, ... but not hello

      h[a-b]llo matches hallo and hbllo

      Specified by:
      keys in interface RedisService
      Parameters:
      pattern - 模式
      Returns:
      匹配的所有键
    • rename

      public void rename(String oldKey, String newKey)
      Description copied from interface: RedisService
      将 key 重命名为 newKey
      Specified by:
      rename in interface RedisService
      Parameters:
      oldKey - 旧键
      newKey - 新键
    • renameIfAbsent

      public Boolean renameIfAbsent(String oldKey, String newKey)
      Description copied from interface: RedisService
      当 newKey 不存在时,将 key 重命名为 newKey
      Specified by:
      renameIfAbsent in interface RedisService
      Parameters:
      oldKey - 旧键
      newKey - 新键
      Returns:
      是否重命名成功
    • expire

      public Boolean expire(String key, long timeout, TimeUnit unit)
      Description copied from interface: RedisService
      设置 key 过期时间
      Specified by:
      expire in interface RedisService
      Parameters:
      key - 键
      timeout - 超时时间
      unit - 时间单位
      Returns:
      是否设置成功
    • expire

      public Boolean expire(String key, Duration duration)
      Description copied from interface: RedisService
      设置 key 过期时间
      Specified by:
      expire in interface RedisService
      Parameters:
      key - 键
      duration - 超时时间
      Returns:
      是否设置成功
    • expireAt

      public Boolean expireAt(String key, Date date)
      Description copied from interface: RedisService
      设置 key 在指定的时间过期
      Specified by:
      expireAt in interface RedisService
      Parameters:
      key - 键
      date - 指定的时间过期
      Returns:
      是否设置成功
    • expireAt

      public Boolean expireAt(String key, Instant expireAt)
      Description copied from interface: RedisService
      设置 key 在指定的时间过期
      Specified by:
      expireAt in interface RedisService
      Parameters:
      key - 键
      expireAt - 指定的时间过期
      Returns:
      是否设置成功
    • persist

      public Boolean persist(String key)
      Description copied from interface: RedisService
      设置 key 永不过期
      Specified by:
      persist in interface RedisService
      Parameters:
      key - 键
      Returns:
      是否设置成功
    • getExpire

      public Long getExpire(String key)
      Description copied from interface: RedisService
      获取过期时间, 单位: 秒
      Specified by:
      getExpire in interface RedisService
      Parameters:
      key - 键
      Returns:
      过期时间, 单位: 秒
    • getExpire

      public Long getExpire(String key, TimeUnit timeUnit)
      Description copied from interface: RedisService
      获取过期时间
      Specified by:
      getExpire in interface RedisService
      Parameters:
      key - 键
      timeUnit - 时间单位
      Returns:
      过期时间
    • hDelete

      public Long hDelete(String key, String... hashKeys)
      Description copied from interface: RedisService
      Hash 操作, 批量删除 hashKey
      Specified by:
      hDelete in interface RedisService
      Parameters:
      key - 键
      hashKeys - hash 键集合
      Returns:
      删除的数量
    • hHasKey

      public Boolean hHasKey(String key, String hashKey)
      Description copied from interface: RedisService
      Hash 操作, 判断 hash 键, 是否存在
      Specified by:
      hHasKey in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
      Returns:
      是否存在
    • hGet

      public <T> T hGet(String key, String hashKey, Class<T> clazz)
      Description copied from interface: RedisService
      Hash 操作, 获取 value
      Specified by:
      hGet in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      hashKey - hash 键
      clazz - 值类型
      Returns:
    • hMultiGet

      public List<String> hMultiGet(String key, Collection<String> hashKeys)
      Description copied from interface: RedisService
      Hash 操作, 批量获取 value 返回的 value 顺序与 hashKeys 顺序一致, 不存在则用 null 填充, 存在则返回的是 JsonUtils.toJson() 的结果, 需要自行转换
      Specified by:
      hMultiGet in interface RedisService
      Parameters:
      key - 键
      hashKeys - hash 键集合
      Returns:
      值集合
    • hIncrement

      public void hIncrement(String key, String hashKey)
      Description copied from interface: RedisService
      Hash 操作, value + 1
      Specified by:
      hIncrement in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
    • hIncrement

      public void hIncrement(String key, String hashKey, long delta)
      Description copied from interface: RedisService
      Hash 操作, value + delta
      Specified by:
      hIncrement in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
      delta - 步长
    • hIncrement

      public void hIncrement(String key, String hashKey, double delta)
      Description copied from interface: RedisService
      Hash 操作, value + delta
      Specified by:
      hIncrement in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
      delta - 步长
    • hDecrement

      public void hDecrement(String key, String hashKey)
      Description copied from interface: RedisService
      Hash 操作, value - 1
      Specified by:
      hDecrement in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
    • hDecrement

      public void hDecrement(String key, String hashKey, long delta)
      Description copied from interface: RedisService
      Hash 操作, value - delta
      Specified by:
      hDecrement in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
      delta - 步长
    • hDecrement

      public void hDecrement(String key, String hashKey, double delta)
      Description copied from interface: RedisService
      Hash 操作, value - delta
      Specified by:
      hDecrement in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
      delta - 步长
    • hRandomKey

      public String hRandomKey(String key)
      Description copied from interface: RedisService
      Hash 操作, 随机获取 hash 键
      Specified by:
      hRandomKey in interface RedisService
      Parameters:
      key - 键
      Returns:
      随机 hash 键
    • hRandomEntry

      public Map.Entry<String,String> hRandomEntry(String key)
      Description copied from interface: RedisService
      Hash 操作, 随机获取 hash 键值对, hash 值返回的是 JsonUtils.toJson() 的结果, 需要自行转换
      Specified by:
      hRandomEntry in interface RedisService
      Parameters:
      key - 键
      Returns:
      随机 hash 键值对
    • hRandomKeys

      public List<String> hRandomKeys(String key, long count)
      Description copied from interface: RedisService
      Hash 操作, 随机获取指定数量的 hash 键
      Specified by:
      hRandomKeys in interface RedisService
      Parameters:
      key - 键
      count - hash 键数量
      Returns:
      hash 键集合
    • hRandomEntries

      public Map<String,String> hRandomEntries(String key, long count)
      Description copied from interface: RedisService
      Hash 操作, 随机获取指定数量的 hash 键值对, hash 值返回的是 JsonUtils.toJson() 的结果, 需要自行转换
      Specified by:
      hRandomEntries in interface RedisService
      Parameters:
      key - 键
      count - hash 键值对数量
      Returns:
      hash 键值对集合
    • hKeys

      public Set<String> hKeys(String key)
      Description copied from interface: RedisService
      Hash 操作, 获取 hash 键集合
      Specified by:
      hKeys in interface RedisService
      Parameters:
      key - 键
      Returns:
      hash 键集合
    • hLengthOfValue

      public Long hLengthOfValue(String key, String hashKey)
      Description copied from interface: RedisService
      Hash 操作, 获取 value 长度
      Specified by:
      hLengthOfValue in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
      Returns:
      value 长度
    • hSize

      public Long hSize(String key)
      Description copied from interface: RedisService
      Hash 操作, 获取 hash 键数量
      Specified by:
      hSize in interface RedisService
      Parameters:
      key - 键
      Returns:
      hash 键数量
    • hPutAll

      public void hPutAll(String key, Map<String,Object> kv)
      Description copied from interface: RedisService
      Hash 操作, 批量保存 hash 键值对
      Specified by:
      hPutAll in interface RedisService
      Parameters:
      key - 键
      kv - hash 键值对
    • hPut

      public <T> void hPut(String key, String hashKey, Object value)
      Description copied from interface: RedisService
      Hash 操作, 保存 value
      Specified by:
      hPut in interface RedisService
      Type Parameters:
      T - 泛型
      Parameters:
      key - 键
      hashKey - hash 键
      value - 值
    • hPutIfAbsent

      public Boolean hPutIfAbsent(String key, String hashKey, Object value)
      Description copied from interface: RedisService
      Hash 操作, 仅当 hashKey 不存在时,才设置 hashKey 的值
      Specified by:
      hPutIfAbsent in interface RedisService
      Parameters:
      key - 键
      hashKey - hash 键
      value - 值
      Returns:
      是否保存成功
    • hValues

      public List<String> hValues(String key)
      Description copied from interface: RedisService
      Hash 操作, 获取所有 hash 值, hash 值返回的是 JsonUtils.toJson() 的结果, 需要自行转换
      Specified by:
      hValues in interface RedisService
      Parameters:
      key - 键
      Returns:
      hash 值集合
    • hEntries

      public Map<String,String> hEntries(String key)
      Description copied from interface: RedisService
      Hash 操作, 获取所有 hash 键值对 返回的 Map 的 value 是 JsonUtils.toJson() 的结果, 需要自行转换
      Specified by:
      hEntries in interface RedisService
      Parameters:
      key - 键
      Returns:
      hash 键值对