前言一、RedisTemplate是什么?二、使用步骤1.引入库2.自动装配3.常用数据类型
1.String
1.1 存入数据到缓存 Integer、String、实体类等1.2 缓存基本的对象并设置过期时间,Integer、String、实体类等1.3 获取缓存基本对象1.4 删除缓存中单个对象1.5 获得缓存的基本对象列表 2.List
2.1 缓存List数据2.2 获得缓存的list对象2.3 删除集合对象 3.Set
3.1 缓存Set数据3.2 获得缓存的set 4.Map
5.1 缓存Map5.2 获得缓存的Map5.3 删除Hash中的数据5.4 获取多个Hash中的数据 5.常用方法
5.1 设置有效时间 总结
前言
公司开发中,使用装Redis封装类,本文记录RedisTemplate封常用方法,以便于日常使用方便
以下是本篇文章正文内容,下面案例可供参考
一、RedisTemplate是什么?Spring中用于操作Redis工具类
二、使用步骤 1.引入库代码如下(示例):
import org.springframework.data.redis.core.RedisTemplate;2.自动装配
代码如下(示例):
@Autowired public RedisTemplate redisTemplate;3.常用数据类型 1.String
代码如下(示例):
1.1 存入数据到缓存 Integer、String、实体类等
public void setCacheObject(final String key, final T value)
{
redisTemplate.opsForValue().set(key, value);
}
1.2 缓存基本的对象并设置过期时间,Integer、String、实体类等
代码如下(示例):
public void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit)
{
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
1.3 获取缓存基本对象时间单位:TimeUnit工具类(NANOSECONDS、MICROSECONDS、MILLISECONDS、SECONDS、MINUTES、HOURS、DAYS)
代码如下(示例):
public T getCacheObject(final String key)
{
ValueOperations operation = redisTemplate.opsForValue();
return operation.get(key);
}
1.4 删除缓存中单个对象
代码如下(示例):
public boolean deleteObject(final String key)
{
return redisTemplate.delete(key);
}
1.5 获得缓存的基本对象列表
代码如下(示例):
public Collection keys(final String pattern)
{
return redisTemplate.keys(pattern);
}
2.List
代码如下(示例):
2.1 缓存List数据代码如下(示例):
public long setCacheList(final String key, final List dataList)
{
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
2.2 获得缓存的list对象
代码如下(示例):
public List getCacheList(final String key)
{
return redisTemplate.opsForList().range(key, 0, -1);
}
2.3 删除集合对象
代码如下(示例):
public long deleteObject(final Collection collection)
{
return redisTemplate.delete(collection);
}
3.Set
代码如下(示例):
3.1 缓存Set数据代码如下(示例):
public BoundSetOperations setCacheSet(final String key, final Set dataSet)
{
BoundSetOperations setOperation = redisTemplate.boundSetOps(key);
Iterator it = dataSet.iterator();
while (it.hasNext())
{
setOperation.add(it.next());
}
return setOperation;
}
3.2 获得缓存的set
代码如下(示例):
public Set getCacheSet(final String key)
{
return redisTemplate.opsForSet().members(key);
}
4.Map
代码如下(示例):
5.1 缓存Map代码如下(示例):
public void setCacheMap(final String key, final Map dataMap)
{
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
5.2 获得缓存的Map
代码如下(示例):
public T getCacheMapValue(final String key, final String hKey)
{
HashOperations opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
5.3 删除Hash中的数据
代码如下(示例):
public void delCacheMapValue(final String key, final String hkey)
{
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.delete(key, hkey);
}
5.4 获取多个Hash中的数据
代码如下(示例):
public List getMultiCacheMapValue(final String key, final Collection
5.常用方法
5.1 设置有效时间
public boolean expire(final String key, final long timeout, final TimeUnit unit)
{
return redisTemplate.expire(key, timeout, unit);
}
总结
以上为常用RedisTemplate常用方法,这里记录一下,方便工作中的使用



