1.RedisTemplate操作hash类
下面是一个使用RedisTemplate操作hash作为缓存,并设置过期时间的案例:
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class HashCacheExample {
private RedisTemplate<String, Object> redisTemplate;
private HashOperations<String, String, Object> hashOperations;
private ValueOperations<String, Object> valueOperations;
public HashCacheExample(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
this.valueOperations = redisTemplate.opsForValue();
}
public void setHashCache(String cacheKey, String hashKey, Object value, long timeout, TimeUnit unit) {
hashOperations.put(cacheKey, hashKey, value);
redisTemplate.expire(cacheKey, timeout, unit);
}
public Object getHashCache(String cacheKey, String hashKey) {
return hashOperations.get(cacheKey, hashKey);
}
public void setCache(String cacheKey, Object value, long timeout, TimeUnit unit) {
valueOperations.set(cacheKey, value);
redisTemplate.expire(cacheKey, timeout, unit);
}
public Object getCache(String cacheKey) {
return valueOperations.get(cacheKey);
}
public Map<String, Object> getAllHashCache(String cacheKey) {
return hashOperations.entries(cacheKey);
}
public void deleteHashCache(String cacheKey, String hashKey) {
hashOperations.delete(cacheKey, hashKey);
}
public void deleteCache(String cacheKey) {
redisTemplate.delete(cacheKey);
}
}
2. 使用案例
// 创建RedisTemplate对象并设置连接工厂
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
// 创建HashCacheExample对象
HashCacheExample cacheExample = new HashCacheExample(redisTemplate);
// 设置hash缓存,并设置过期时间为1小时
String cacheKey = "myCacheKey";
String hashKey = "myHashKey";
Object value = "myValue";
cacheExample.setHashCache(cacheKey, hashKey, value, 1, TimeUnit.HOURS);
// 获取hash缓存
Object cachedValue = cacheExample.getHashCache(cacheKey, hashKey);
// 设置普通缓存,并设置过期时间为30分钟
String cacheKey2 = "myCacheKey2";
Object value2 = "myValue2";
cacheExample.setCache(cacheKey2, value2, 30, TimeUnit.MINUTES);
// 获取普通缓存
Object cachedValue2 = cacheExample.getCache(cacheKey2);
// 获取所有hash缓存数据
Map<String, Object> allHashCache = cacheExample.getAllHashCache(cacheKey);
// 删除hash缓存
cacheExample.deleteHashCache(cacheKey, hashKey);
// 删除普通缓存
cacheExample.deleteCache(cacheKey2);
3. 定期清除Redis的hash结构的缓存数据
如果有需要,就加
要定期清除Redis的hash结构的缓存数据,可以使用Redis的过期时间来实现。可以通过设置过期时间来让Redis在一定时间后自动删除缓存数据。
以下是一个示例代码,使用RedisTemplate和Spring的Scheduled注解来定期清除缓存数据:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class CacheCleaner {
private RedisTemplate<String, Object> redisTemplate;
private HashOperations<String, String, Object> hashOperations;
@Autowired
public CacheCleaner(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
}
@Scheduled(fixedDelay = 24 * 60 * 60 * 1000) // 每隔24小时执行一次
public void cleanCache() {
String cacheKey = "myCacheKey";
hashOperations.entries(cacheKey).keySet().forEach(hashKey -> {
Long ttl = redisTemplate.getExpire(cacheKey);
if (ttl != null && ttl <= 0) {
hashOperations.delete(cacheKey, hashKey);
}
});
}
}
在上述示例中,CacheCleaner类使用@Component注解将其声明为一个Spring组件,并使用@Autowired注解将RedisTemplate注入到该类中。
cleanCache方法使用@Scheduled注解来定义定时任务,fixedDelay参数指定了任务的执行间隔,这里设置为每隔24小时执行一次。
在cleanCache方法中,我们遍历了hash结构中的所有hashKey,然后通过getExpire方法获取hash结构的过期时间,如果过期时间小于等于0,则表示该缓存数据已经过期,我们可以使用delete方法将其删除。
通过以上方法,可以实现定期清除Redis的hash结构的缓存数据。