package com.qiwu.inference.common.utils; import com.alibaba.fastjson.JSON; import com.qiwu.inference.common.TimingAcquisition; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.util.concurrent.TimeUnit; @Component public class RedisUtil { @Value("${redisExpireTime}") private String redisExpireTime; @Autowired private RedisTemplate redisTemplate; public void setStr(String key, String value) { //存入redis缓存中,设置过期时间 redisTemplate.opsForValue().set(key, value, Long.parseLong(redisExpireTime), TimeUnit.MILLISECONDS); } public String getStr(String key) { //存redis缓存中根据key取出value return (String) redisTemplate.opsForValue().get(key); } public void setHash(String key, String hk, String hv) { //存入redis缓存中 redisTemplate.opsForHash().put(key, hk, hv); //设置过期时间 redisTemplate.opsForHash().getOperations().expire(key, Long.parseLong(redisExpireTime), TimeUnit.MILLISECONDS); } public String getHash(String key, String hk) { //存入redis缓存中 return (String) redisTemplate.opsForHash().get(key, hk); } public void delHash(String key, String hk) { redisTemplate.opsForHash().delete(key, hk); } //判断key是否存在 public boolean keyIsExists(String key) { return Boolean.TRUE.equals(redisTemplate.hasKey(key)); } //key续期 public void renewalStr(String key, long times, TimeUnit timeUnit) { if (!StringUtils.isEmpty(getStr(key))) { //设置过期时间 redisTemplate.opsForValue().getOperations().expire(key, times, timeUnit); } } //key续期 public void renewalHash(String key, String hk, long times, TimeUnit timeUnit) { if (!StringUtils.isEmpty(getHash(key, hk))) { //设置过期时间 redisTemplate.opsForHash().getOperations().expire(key, times, timeUnit); } } }