Redis缓存工具类

@Component
public class RedisCacheService {
    @Autowired
    public RedisTemplate redisTemplate;
    
      public <T> long setCacheList(String key, List<T> dataList) {
        Long count = this.redisTemplate.opsForList().rightPushAll(key, dataList);
        return count == null ? 0L : count;
    }

     public <T> List<T> getCacheList(String key) {
        return this.redisTemplate.opsForList().range(key, 0L, -1L);
    }

    public <T> List<T> getCacheList(String key, long start, long end) {
        return this.redisTemplate.opsForList().range(key, start, end);
    }

    public long countCacheList(String key) {
        Long count = this.redisTemplate.opsForList().size(key);
        return count == null ? 0L : count;
    }

  
    public <T> void setCacheObject(String key, T value) {
        this.redisTemplate.opsForValue().set(key, value);
    }

    public <T> void setCacheObject(String key, T value, Long timeout, TimeUnit timeUnit) {
        this.redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    public <T> Boolean setIfAbsentCacheObject(String key, T value, Long timeout, TimeUnit timeUnit) {
        return this.redisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit);
    }

    public <T> Boolean setIfAbsentCacheObjectWithoutExpire(String key, T value) {
        return this.redisTemplate.opsForValue().setIfAbsent(key, value);
    }

    public boolean expire(String key, long timeout) {
        return this.expire(key, timeout, TimeUnit.SECONDS);
    }

    public boolean expire(String key, long timeout, TimeUnit unit) {
        return this.redisTemplate.expire(key, timeout, unit);
    }

    public <T> T getCacheObject(String key) {
        ValueOperations<String, T> operation = this.redisTemplate.opsForValue();
        return operation.get(key);
    }

    public boolean deleteObject(String key) {
        return this.redisTemplate.delete(key);
    }

    public long deleteObject(Collection collection) {
        return this.redisTemplate.delete(collection);
    }


    /** @deprecated */
    public <T> long setCacheSet(String key, Set<T> dataSet) {
        Long count = this.redisTemplate.opsForSet().add(key, new Object[]{dataSet});
        return count == null ? 0L : count;
    }

    public <T> long setCacheSet(String key, T... dataSet) {
        Long count = this.redisTemplate.opsForSet().add(key, dataSet);
        return count == null ? 0L : count;
    }

    public <T> Set<T> getCacheSet(String key) {
        return this.redisTemplate.opsForSet().members(key);
    }

    public <T> long removeCacheSet(String key, T... dataSet) {
        Long count = this.redisTemplate.opsForSet().remove(key, dataSet);
        return count == null ? 0L : count;
    }

    public <T> boolean isMemberCacheSet(String key, T value) {
        return this.redisTemplate.opsForSet().isMember(key, value);
    }

    public long countCacheSet(String key) {
        Long count = this.redisTemplate.opsForSet().size(key);
        return count == null ? 0L : count;
    }

    public <T> boolean setCacheZSet(String key, T value, double score) {
        return this.redisTemplate.opsForZSet().add(key, value, score);
    }

    public <T> Set<T> getCacheZSet(String key, int min, int max) {
        return this.getCacheZSet(key, min, max, true);
    }

    public <T> Set<T> getCacheZSet(String key, int min, int max, boolean isAsc) {
        ZSetOperations zSetOps = this.redisTemplate.opsForZSet();
        new LinkedHashSet();
        Set dataZSet;
        if (isAsc) {
            dataZSet = zSetOps.range(key, (long)min, (long)max);
        } else {
            dataZSet = zSetOps.reverseRange(key, (long)min, (long)max);
        }

        return dataZSet;
    }

    public <T> Set<T> getCacheZSetByScore(String key, double min, double max) {
        return this.redisTemplate.opsForZSet().rangeByScore(key, min, max);
    }

    public <T> Set<T> getCacheZSetByScore(String key, double min, double max, long offset, long count) {
        return this.redisTemplate.opsForZSet().rangeByScore(key, min, max, offset, count);
    }

    public <T> boolean isMemberCacheZSet(String key, T value) {
        Long rank = this.redisTemplate.opsForZSet().rank(key, value);
        return rank != null;
    }

    public long countCacheZSet(String key) {
        Long count = this.redisTemplate.opsForZSet().zCard(key);
        return count == null ? 0L : count;
    }

    public <T> long removeCacheZSet(String key, T... dataZSet) {
        Long count = this.redisTemplate.opsForZSet().remove(key, dataZSet);
        return count == null ? 0L : count;
    }

    public <T> long removeRangeByScore(String key, double start, double end) {
        Long count = this.redisTemplate.opsForZSet().removeRangeByScore(key, start, end);
        return count == null ? 0L : count;
    }

    public <T> long removeRangeCacheZSet(String key, long start, long end) {
        Long count = this.redisTemplate.opsForZSet().removeRange(key, start, end);
        return count == null ? 0L : count;
    }

    public <T> Double scoreCacheZSet(String key, T value) {
        return this.redisTemplate.opsForZSet().score(key, value);
    }

    public <T> void setCacheMap(String key, Map<String, T> dataMap) {
        if (dataMap != null) {
            this.redisTemplate.opsForHash().putAll(key, dataMap);
        }

    }

    public <T> Map<String, T> getCacheMap(String key) {
        return this.redisTemplate.opsForHash().entries(key);
    }

    public <T> void setCacheMapValue(String key, String hKey, T value) {
        this.redisTemplate.opsForHash().put(key, hKey, value);
    }

    public <T> boolean hasMapKey(String key, String hKey) {
        return this.redisTemplate.opsForHash().hasKey(key, hKey);
    }

    public <T> T getCacheMapValue(String key, String hKey) {
        HashOperations<String, String, T> opsForHash = this.redisTemplate.opsForHash();
        return opsForHash.get(key, hKey);
    }

    public <T> List<T> getMultiCacheMapValue(String key, Collection<Object> hKeys) {
        return this.redisTemplate.opsForHash().multiGet(key, hKeys);
    }

    public long removeCacheMapValue(String key, Object hKey) {
        return this.redisTemplate.opsForHash().delete(key, new Object[]{hKey});
    }

    public Collection<String> keys(String pattern) {
        return this.redisTemplate.keys(pattern);
    }

    public Boolean isExist(String key) {
        return this.redisTemplate.hasKey(key);
    }

    public Long incrBy(String key, long count) {
        return this.redisTemplate.opsForValue().increment(key, count);
    }

    public boolean lock(String key, String requestId, long seconds) {
        Object execute = this.redisTemplate.execute((connection) -> {
            return connection.set(this.rawKey(key), this.rawValue(requestId), Expiration.seconds(seconds), SetOption.ifAbsent());
        });
        return Convert.toBool(execute);
    }

    public boolean unlock(String key, String requestId) {
        String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
        Object execute = this.redisTemplate.execute(new DefaultRedisScript(script, Boolean.class), ListUtil.toList(new String[]{key}), new Object[]{requestId});
        return Convert.toBool(execute);
    }

    private byte[] rawKey(Object key) {
        Assert.notNull(key, "non null key required");
        RedisSerializer keySerializer = this.redisTemplate.getKeySerializer();
        return keySerializer == null && key instanceof byte[] ? (byte[])((byte[])key) : keySerializer.serialize(key);
    }

    private byte[] rawValue(Object value) {
        RedisSerializer valueSerializer = this.redisTemplate.getValueSerializer();
        return valueSerializer == null && value instanceof byte[] ? (byte[])((byte[])value) : valueSerializer.serialize(value);
    }
}
 

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JB091

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值