通用操作:
/**
* 判断key是否存在
* @param key 键
* @return true 存在 false不存在
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 设置有效时间
*
* @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);
}
/**
* 根据key 获取过期时间
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public Long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 删除单个缓存
*
* @param key
*/
public boolean deleteObject(final String key) {
return redisTemplate.delete(key);
}
/**
* 删除多个缓存
*
* @param keys 多个对象
* @return
*/
public long deleteObject(Collection<String> keys) {
return redisTemplate.delete(keys);
}
opsForValue
/**
* 给指定的key加1(可用于实现乐观锁)
*
* @param key
* @return
*/
public Long incrForValue(final String key) {
return redisTemplate.opsForValue().increment(key);
}
/**
* 递减
*
* @param key 键
* @param value 要减少几(大于0)
* @return 递减后结果
*/
public Long decrForValue(String key, long value) {
return redisTemplate.opsForValue().decrement(key, value);
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
*/
public <T> void setForValueObject(final String key, final T value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 缓存基本的对象,Integer、String、实体类等
*
* @param key 缓存的键值
* @param value 缓存的值
* @param timeout 时间
* @param timeUnit 时间颗粒度
*/
public <T> void setForValueObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
* 缓存基本的对象,Integer、String、实体类等
* 如果指定的key不存在则设置值,返回成功:true (可用于实现分布式锁)
* 如果已存在,则不设置,返回失败:false
*
* @param key 缓存的键值
* @param value 缓存的值
* @param timeout 时间
* @param timeUnit 时间颗粒度
*/
public <T> boolean setForValueIfAbsent(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) {
return redisTemplate.opsForValue().setIfAbsent(key, value, timeout, timeUnit);
}
/**
* 获得缓存的基本对象。
*
* @param key 缓存键值
* @return 缓存键值对应的数据
*/
public <T> T getForValueObject(final String key) {
return (T) redisTemplate.opsForValue().get(key);
}
/**
* 给指定的key的value,在原有的值基础上新增字符串到末尾
* 此方法只适用于字符串
*
* @param key 缓存键值
*/
public void setForValueAppend(final String key, final String value) {
redisTemplate.opsForValue().append(key, value);
}
/**
* 覆盖从指定位置开始的值
* 此方法只适用于字符串
*
* @param key 缓存键值
*/
public void setForValueByIndex(final String key, final String value, long offset) {
redisTemplate.opsForValue().set(key, value, offset);
}
/**
* 获取,对应key的value字符串的长度
* 此方法只适用于字符串
*
* @param key 缓存键值
*/
public Long sizeForValue(final String key) {
return redisTemplate.opsForValue().size(key);
}
opsForList
/**
* 从头部开始缓存List数据
*
* @param key 缓存的键值
* @param dataList 待缓存的List数据
* @return 集合的长度
*/
public <T> long setForListLeftPush(final String key, final List<T> dataList) {
Long count = redisTemplate.opsForList().leftPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* 移除集合头部的第一个元素并返回这个元素
*
* @param key 缓存的键值
* @return 集合的长度
*/
public <T> T delForListLeftAndGet(final String key) {
return (T) redisTemplate.opsForList().leftPop(key);
}
/**
* 从尾部开始缓存List数据
*
* @param key 缓存的键值
* @param dataList 待缓存的List数据
* @return 集合的长度
*/
public <T> long setForListRightPush(final String key, final List<T> dataList) {
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
/**
* 移除集合尾部的第一个元素并返回这个元素
*
* @param key 缓存的键值
* @return 集合的长度
*/
public <T> T delForListRightAndGet(final String key) {
return (T) redisTemplate.opsForList().rightPop(key);
}
/**
* 获得缓存的整个list对象
*
* @param key 缓存的键值
* @return 缓存键值对应的数据
*/
public <T> List<T> getForListAll(final String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
/**
* 获取集合中从指定位置开始到指定位置结束的值
* <p>
* list缓存的内容
*
* @param key 键
* @param start 开始
* @param end 结束 0 到 -1代表所有值
* @return 缓存列表
*/
public List<Object> getForListRange(String key, long start, long end) {
return redisTemplate.opsForList().range(key, start, end);
}
/**
* 获取list缓存的长度
*
* @param key 键
* @return 长度
*/
public Long sizeForList(String key) {
return redisTemplate.opsForList().size(key);
}
/**
* 根据索引修改list中的某条数据
*
* @param key 键
* @param index 索引
* @param value 值
*/
public void setForListByIndex(String key, long index, Object value) {
redisTemplate.opsForList().set(key, index, value);
}
/**
* @param key 通过索引 获取list中的值
* @param index 索引 index>=0时, 0 第一个元素,1 第二个元素,依次类推;index<0时,-1,倒数第一个元素,-2倒数第二个元素,依次类推
* @return
*/
public Object getForListByIndex(String key, long index) {
return redisTemplate.opsForList().index(key, index);
}
opsForSet
/**
* 缓存Set
*
* @param key 缓存键值
* @param dataSet 缓存的数据
* @return 缓存数据的对象
*/
public <T> long setForSet(final String key, List<T> dataSet) {
Long count = redisTemplate.opsForSet().add(key, dataSet.toArray(new String[0]));
return count == null ? 0 : count;
}
/**
* 获得缓存的set
*
* @param key
* @return
*/
public <T> Set<T> getForSetAll(final String key) {
return redisTemplate.opsForSet().members(key);
}
/**
* 根据value从一个set中查询,是否存在
*
* @param key 键
* @param value 值
* @return true 存在 false不存在
*/
public Boolean hasForSetValue(String key, Object value) {
return redisTemplate.opsForSet().isMember(key, value);
}
/**
* 获取set缓存的长度
*
* @param key 键
* @return set长度
*/
public Long sizeForSet(String key) {
return redisTemplate.opsForSet().size(key);
}
/**
* 从set中移除值为value的
*
* @param key 键
* @param values 值 可以是多个
* @return 移除的个数
*/
public Long delForSetByKeyAndValues(String key, Object... values) {
return redisTemplate.opsForSet().remove(key, values);
}
/**
* 获取指定key中与给定集合不一样的数据
* 获取两个集合的差值
*
* @param key 指定的key
* @param otherKeys 取差集的集合
* @return
*/
public <T> Set<T> getForSetDifferenceByList(final String key, Collection<T> otherKeys) {
return redisTemplate.opsForSet().difference(key, otherKeys);
}
/**
* 获取两个指定key所代表的集合中不一样的数据
* 获取两个集合的差值
*
* @param key 指定的key
* @param key2 取差集的key
* @return
*/
public <T> Set<T> getForSetDifferenceByKey(final String key, final String key2) {
return redisTemplate.opsForSet().difference(key, key2);
}
/**
* 获取两个指定key所代表的集合的合集
* 获取两个集合的合集
*
* @param key 指定的key
* @param key2 取合集的集合key
* @return
*/
public <T> Set<T> getForSetunionByKey(final String key, final String key2) {
return redisTemplate.opsForSet().union(key, key2);
}
opsForHash
/**
* 缓存Map
*
* @param key
* @param dataMap
*/
public <T> void setForHash(final String key, final Map<String, T> dataMap) {
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* 缓存Map 并设置时间
*
* @param key 键
* @param map 对应多个键值
* @param time 时间(秒)
*/
public void setForHash(String key, Map<String, Object> map, long time) {
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
}
/**
* 往Hash中存入数据
*
* @param key Redis键
* @param hashKey 项 不能为null
* @param value 值
*/
public <T> void setForHash(final String key, final String hashKey, final T value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* 判断hash表中是否有该项的值
*
* @param key 键 不能为null
* @param hashKey 项 不能为null
* @return true 存在 false不存在
*/
public Boolean hasForHashValue(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* 获得缓存的Map
*
* @param key
* @return
*/
public <T> Map<String, T> getForHashMap(final String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 获得指定Map中的所有key
*
* @param key
* @return
*/
public <T> Set<T> getForHashKeys(final String key) {
return redisTemplate.opsForHash().keys(key);
}
/**
* 获得指定Map中的所有values
*
* @param key
* @return
*/
public <T> List<T> getForHashValues(final String key) {
return redisTemplate.opsForHash().values(key);
}
/**
* 获取Hash中的数据
*
* @param key Redis键
* @param hashKey 项 不能为null
* @return Hash中的对象
*/
public <T> T getForHashMapValue(final String key, final String hashKey) {
return (T) redisTemplate.opsForHash().get(key, hashKey);
}
/**
* 获取多个Hash中的数据
*
* @param key Redis键
* @param hashKeys 项 可以是多个 不能为null
* @return Hash对象集合
*/
public <T> List<T> getForHashMapValue(final String key, final Collection<Object> hashKeys) {
return redisTemplate.opsForHash().multiGet(key, hashKeys);
}
/**
* 获取指定map的长度大小
*
* @param key
*/
public Long sizeForHash(final String key) {
return redisTemplate.opsForHash().size(key);
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param hashKey 项 可以是多个 不能为null
*/
public void delForHashByHashKey(String key, Object... hashKey) {
redisTemplate.opsForHash().delete(key, hashKey);
}
/**
* 删除hash表中的值
*
* @param key 键 不能为null
* @param hashKeys 项 可以是多个 不能为null
*/
public void delForHashByHashKey(String key, Collection hashKeys) {
redisTemplate.opsForHash().delete(key, hashKeys.toArray());
}
/**
* hash递增 如果不存在,就会创建一个 并把新增后的值返回
*
* @param key 键
* @param hashKey 项
* @param value 要增加几(大于0)
* @return 递增后结果
*/
public Double incrForHashValue(String key, String hashKey, double value) {
return redisTemplate.opsForHash().increment(key, hashKey, value);
}
/**
* hash递减
*
* @param key 键
* @param hashKey 项
* @param value 要减少记(小于0)
* @return 递减后结果
*/
public Double decrForHashValue(String key, String hashKey, double value) {
return redisTemplate.opsForHash().increment(key, hashKey, -value);
}