这是一个用于封装与Redis缓存交互的工具类 RedisCache
,该类提供了各种方法来处理不同类型的数据存储和检索。
package cn.zjy.travel.redis.utils;
import cn.zjy.travel.redis.key.KeyPrefix;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* spring redis 工具类
*
* @author ruoyi
**/
@SuppressWarnings(value = {"unchecked", "rawtypes"})
@Component
public class RedisCache {
public final RedisTemplate redisTemplate;
public RedisCache(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public Long increment(String key, int incr) {
return redisTemplate.opsForValue().increment(key, incr);
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
*/
public <T> void setCacheObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
}
public <T> void setCacheObject(final KeyPrefix prefix, final T value, String... suffix) {
if (prefix.getTimeout() > 0) {
this.setCacheObject(prefix.fullKey(suffix), value, prefix.getTimeout(), prefix.getUnit());
} else {
this.setCacheObject(prefix.fullKey(suffix), value);
}
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @param timeout 时间
* @param timeUnit 时间颗粒度
*/
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* 设置有效时间
*
* @param key Redis键
* @param timeout 超时时间
* @return true=设置成功;false=设置失败
*/
public boolean expire(final String key, final long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 设置有效时间
*
* @param key Redis键
* @param timeout 超时时间
* @param unit 时间单位
* @return true=设置成功;false=设置失败
*/
public boolean expire(final String key, final long timeout, final TimeUnit unit) {
return redisTemplate.expire(key, timeout, unit);
}
/**
* 获取有效时间
*
* @param key Redis键
* @return 有效时间
*/
public long getExpire(final String key) {
return redisTemplate.getExpire(key);
}
/**
* 判断 key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 获得缓存的基本对象。
*
* @param key 缓存键值
* @return 缓存键值对应的数据
*/
public <T> T getCacheObject(final String key) {
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
* 删除单个对象
*
* @param key
*/
public boolean deleteObject(final String key) {
return redisTemplate.delete(key);
}
/**
* 删除集合对象
*
* @param collection 多个对象
* @return
*/
public boolean deleteObject(final Collection collection) {
return redisTemplate.delete(collection) > 0;
}
/**
* 缓存List数据
*
* @param key 缓存的键值
* @param dataList 待缓存的List数据
* @return 缓存的对象
*/
public <T> long setCacheList(final String key, final List<T> dataList) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* 获得缓存的list对象
*
* @param key 缓存的键值
* @return 缓存键值对应的数据
*/
public <T> List<T> getCacheList(final String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
/**
* 缓存Set
*
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) {
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator();
while (it.hasNext()) {
setOperation.add(it.next());
}
return setOperation;
}
/**
* 获得缓存的set
*
* @param key
* @return
*/
public <T> Set<T> getCacheSet(final String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* 缓存Map
*
* @param key
* @param dataMap
*/
public <T> void setCacheMap(final String key, final Map<String, T> dataMap) {
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* 获得缓存的Map
*
* @param key
* @return
*/
public <T> Map<String, T> getCacheMap(final String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 往Hash中存入数据
*
* @param key Redis键
* @param hKey Hash键
* @param value 值
*/
public <T> void setCacheMapValue(final String key, final String hKey, final T value) {
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* 获取Hash中的数据
*
* @param key Redis键
* @param hKey Hash键
* @return Hash中的对象
*/
public <T> T getCacheMapValue(final String key, final String hKey) {
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/**
* 获取多个Hash中的数据
*
* @param key Redis键
* @param hKeys Hash键集合
* @return Hash对象集合
*/
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/**
* 删除Hash中的某条数据
*
* @param key Redis键
* @param hKey Hash键
* @return 是否成功
*/
public boolean deleteCacheMapValue(final String key, final String hKey) {
return redisTemplate.opsForHash().delete(key, hKey) > 0;
}
/**
* 获得缓存的基本对象列表
*
* @param pattern 字符串前缀
* @return 对象列表
*/
public Collection<String> keys(final String pattern) {
return redisTemplate.keys(pattern);
}
/**
* 针对 hash key 进行 increment
*
* @param prefix 前缀
* @param hashKey hash key
* @param increment 自增值
* @param suffix 后缀
*/
public Long hashIncrement(KeyPrefix prefix, String hashKey, int increment, String... suffix) {
Long ret = redisTemplate.opsForHash().increment(prefix.fullKey(suffix), hashKey, increment);
if (prefix.getTimeout() != null && prefix.getTimeout() > 0 && ret == 1) {
// 只有有设置过期时间 && 每一个用户第一次访问页面时才会+1
expire(prefix.fullKey(suffix), prefix.getTimeout(), prefix.getUnit());
}
return ret;
}
/**
* 针对 zset 成员进行增加分数
*
* @param keyPrefix 前缀
* @param increment 增加的值
* @param member 成员
* @param suffix 后缀
*/
public void zsetIncrement(KeyPrefix keyPrefix, double increment, Object member, String... suffix) {
redisTemplate.opsForZSet().incrementScore(keyPrefix.fullKey(suffix), member, increment);
}
/**
* 按照指定分数范围获取 zset 集合中的元素
*
* @param keyPrefix 前缀
* @param start 开始分数
* @param end 结束分数
* @return 分数范围内的 member 集合
*/
public <T> Set<T> zsetRevrange(KeyPrefix keyPrefix, int start, int end, String... suffix) {
return redisTemplate.opsForZSet().reverseRange(keyPrefix.fullKey(suffix), start, end);
}
/**
* 按照分数范围删除范围内的所有元素
*
* @param keyPrefix 前缀
* @param min 最小分数
* @param max 最大分数
* @param suffix 后缀
*/
public void zsetRemoveRange(KeyPrefix keyPrefix, int min, int max, String... suffix) {
redisTemplate.opsForZSet().removeRange(keyPrefix.fullKey(suffix), min, max);
}
public Boolean setnx(String key, String value) {
return redisTemplate.opsForValue().setIfAbsent(key, value);
}
}
下面是对这个类的主要方法的解释:
-
public RedisCache(RedisTemplate redisTemplate)
:构造函数,接受一个RedisTemplate
对象,用于与Redis进行交互。 -
public Long increment(String key, int incr)
:增加指定键的值。适用于整数数据。 -
public <T> void setCacheObject(final String key, final T value)
:缓存一个基本对象,例如整数、字符串或实体类。 -
public <T> void setCacheObject(final KeyPrefix prefix, final T value, String... suffix)
:根据指定的KeyPrefix
缓存对象。可以设置超时时间。 -
public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
:缓存对象并设置自定义的超时时间。 -
public boolean expire(final String key, final long timeout)
:设置指定键的超时时间(秒)。 -
public boolean expire(final String key, final long timeout, final TimeUnit unit)
:设置指定键的超时时间,可以指定时间单位。 -
public long getExpire(final String key)
:获取指定键的剩余超时时间(秒)。 -
public Boolean hasKey(String key)
:检查指定键是否存在于Redis中。 -
public <T> T getCacheObject(final String key)
:获取缓存中的基本对象。 -
public boolean deleteObject(final String key)
:删除指定键的对象。 -
public boolean deleteObject(final Collection collection)
:删除一组对象。 -
public <T> long setCacheList(final String key, final List<T> dataList)
:缓存一个列表数据。 -
public <T> List<T> getCacheList(final String key)
:获取缓存中的列表数据。 -
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
:缓存一个集合。 -
public <T> Set<T> getCacheSet(final String key)
:获取缓存中的集合数据。 -
public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
:缓存一个Map数据。 -
public <T> Map<String, T> getCacheMap(final String key)
:获取缓存中的Map数据。 -
public <T> void setCacheMapValue(final String key, final String hKey, final T value)
:往一个Hash中存入数据。 -
public <T> T getCacheMapValue(final String key, final String hKey)
:获取Hash中的数据。 -
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
:获取多个Hash中的数据。 -
public boolean deleteCacheMapValue(final String key, final String hKey)
:删除Hash中的某条数据。 -
public Collection<String> keys(final String pattern)
:根据键的前缀模式获取Redis中的键。 -
public Long hashIncrement(KeyPrefix prefix, String hashKey, int increment, String... suffix)
:根据指定的KeyPrefix
对应的前缀,对Hash中的键进行增量操作。 -
public void zsetIncrement(KeyPrefix keyPrefix, double increment, Object member, String... suffix)
:对ZSet中的成员进行分数增量操作。 -
public <T> Set<T> zsetRevrange(KeyPrefix keyPrefix, int start, int end, String... suffix)
:按分数范围获取ZSet中的元素。 -
public void zsetRemoveRange(KeyPrefix keyPrefix, int min, int max, String... suffix)
:按分数范围删除ZSet中的元素。 -
public Boolean setnx(String key, String value)
:将键值设置到缓存中,仅在键不存在时设置成功。
这个类为你提供了方便的方法,用于在应用程序中与Redis进行交互,包括缓存数据、设置超时、检索数据等。这对于实现缓存和数据存储功能非常有用,可以提高应用程序的性能和效率。