Redis工具类,用于统计访问量


import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    public RedisUtils() {
    }

    @PostConstruct
    public void init() {
        this.redisTemplate.setKeySerializer(new StringRedisSerializer());
        RedisSerializer rs = new Jackson2JsonRedisSerializer(Object.class);
        this.redisTemplate.setValueSerializer(rs);
    }

    public String getString(String key) {
        String value = (String)this.stringRedisTemplate.opsForValue().get(key);
        return value;
    }

    public Long incr(String key) {
        Long vaue = this.stringRedisTemplate.opsForValue().increment(key);
        return vaue;
    }

    public void setString(String key, String value) {
        this.stringRedisTemplate.opsForValue().set(key, value);
    }

    public void setString(String key, String value, long expire) {
        this.stringRedisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
    }

    public void setString(String key, String value, long expire, TimeUnit timeUnit) {
        this.stringRedisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }

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

    public void setExpire(String key, long expire) {
        this.stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    public void setExpire(String key, long expire, TimeUnit timeUnit) {
        this.stringRedisTemplate.expire(key, expire, timeUnit);
    }

    public Boolean deleteKey(String key) {
        Boolean delete = this.stringRedisTemplate.delete(key);
        return delete;
    }

    public void deleteKeyBatch(Collection<String> keys) {
        this.stringRedisTemplate.delete(keys);
    }

    public Boolean setZset(String key, String value, double score) {
        Boolean re = this.stringRedisTemplate.opsForZSet().add(key, value, score);
        return re;
    }

    public Long zSetSize(String key) {
        Long size = this.stringRedisTemplate.opsForZSet().size(key);
        return size;
    }

    public Double incrZsetScore(String key, String value, double incr) {
        Double score = this.stringRedisTemplate.opsForZSet().incrementScore(key, value, incr);
        return score;
    }

    public Boolean zadd(String key, String value, double score) {
        Boolean re = this.stringRedisTemplate.opsForZSet().add(key, value, score);
        return re;
    }

    public Long countByScore(String key, double min, double max) {
        Long count = this.stringRedisTemplate.opsForZSet().count(key, min, max);
        return count;
    }

    public Double getZsetValueScore(String key, Object value) {
        Double score = this.stringRedisTemplate.opsForZSet().score(key, value);
        return score;
    }

    public void removeZsetValue(String key, Object value) {
        this.stringRedisTemplate.opsForZSet().remove(key, new Object[]{value});
    }

    public Set<String> getZsetValueDesc(String key, long start, long end) {
        Set<String> values = this.stringRedisTemplate.opsForZSet().reverseRange(key, start, end);
        return values;
    }

    public Set<String> getZsetValueAsc(String key, long start, long end) {
        Set<String> values = this.stringRedisTemplate.opsForZSet().range(key, start, end);
        return values;
    }

    public void removeZsetValueByRange(String key, long start, long end) {
        this.stringRedisTemplate.opsForZSet().removeRange(key, start, end);
    }

    public void removeZsetValueByScore(String key, long min, long max) {
        this.stringRedisTemplate.opsForZSet().removeRangeByScore(key, (double)min, (double)max);
    }

    public void setObject(Object key, Object value) {
        this.redisTemplate.opsForValue().set(key, value);
    }

    public void setObject(Object key, Object value, long expire, TimeUnit timeUnit) {
        this.redisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }

    public <T> T getObject(Object key, Class<T> Clazz) {
        Object object = this.redisTemplate.opsForValue().get(key);
        return object;
    }

    public void hPut(String key, String hashKey, String value) {
        this.redisTemplate.opsForHash().put(key, hashKey, value);
    }

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

    public void hPut(String key, String hashKey, Object value) {
        this.redisTemplate.opsForHash().put(key, hashKey, value);
    }

    public void removeHashKey(String key, String hashKey) {
        this.redisTemplate.opsForHash().delete(key, new Object[]{hashKey});
    }

    public <T, K> T luaExecute(RedisScript<T> script, List<Object> keys, Object... args) {
        return this.redisTemplate.execute(script, keys, args);
    }

    public Set<TypedTuple<String>> getZsetTypedTupleDesc(String key, long start, long end) {
        Set<TypedTuple<String>> reverseRangeWithScores = this.stringRedisTemplate.opsForZSet().reverseRangeWithScores(key, start, end);
        return reverseRangeWithScores;
    }

    public Boolean lock(String key, Integer expire) {
        String lockkey = "LOCK_" + key;
        if (expire == null) {
            expire = 120;
        }

        Boolean lock = this.stringRedisTemplate.opsForValue().setIfAbsent(lockkey, "1", (long)expire, TimeUnit.SECONDS);
        return lock;
    }

    public Boolean unlock(String key) {
        String lockkey = "LOCK_" + key;
        Boolean unlock = this.stringRedisTemplate.delete(lockkey);
        return unlock;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值