// Object String按需调整
@Autowired
private RedisTemplate<String, Object> redisTemplate;
设定有效时长
//设定有效时长,TimeUnit.SECONDS是单位
redisTemplate.opsForValue().set("key2","value2",1, TimeUnit.SECONDS);
// 刷新有效时间
redisTemplate.expire(accessToken, seconds, TimeUnit.SECONDS);
获取有效时长
public long tokenTtl(String key) {
return redisTemplate.getExpire(key);
}
保存和读取Set:
SetOperations<String, String> set = redisTemplate.opsForSet();
set.add("set1","22");
set.add("set1","33");
set.add("set1","44");
Set<String> resultSet =redisTemplate.opsForSet().members("set1");
System.out.println("resultSet:"+resultSet); // resultSet:[[set3, set2, set1]] jedis
Hash结构,保存和读取map:
Map<String,String> map=new HashMap<String,String>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
map.put("key5","value5");
redisTemplate.opsForHash().putAll("map1",map);
Map<String,String> resultMap= redisTemplate.opsForHash().entries("map1");
List<String>reslutMapList=redisTemplate.opsForHash().values("map1");
Set<String>resultMapSet=redisTemplate.opsForHash().keys("map1");
String value=(String)redisTemplate.opsForHash().get("map1","key1");
System.out.println("value:"+value);
System.out.println("resultMapSet:"+resultMapSet);
System.out.println("resultMap:"+resultMap);
System.out.println("resulreslutMapListtMap:"+reslutMapList);
/**
运行结果为:
value:value1
resultMapSet:[key1, key2, key5, key3, key4]
resultMap:{key3=value3, key2=value2, key1=value1, key5=value5, key4=value4}
resulreslutMapListtMap:[value1, value2, value5, value3, value4]
*/
获取集合长度
long listLength = redisTemplate.opsForList().size("list");
保存和读取list
List<String> list1=new ArrayList<String>();
list1.add("a1");
list1.add("a2");
list1.add("a3");
List<String> list2=new ArrayList<String>();
list2.add("b1");
list2.add("b2");
list2.add("b3");
redisTemplate.opsForList().leftPush("listkey1",list1);
redisTemplate.opsForList().rightPush("listkey2",list2);
// 集合不存在首次leftPush集合list,集合已存在leftPush从集合左侧插值,相当list.add()
leftPop(K key):移除集合中的左边第一个元素,并返回该元素
List<String> resultList1=(List<String>)redisTemplate.opsForList().leftPop("listkey1");
List<String> resultList2=(List<String>)redisTemplate.opsForList().rightPop("listkey2");
System.out.println("resultList1:"+resultList1);
System.out.println("resultList2:"+resultList2);
/**
resultList1:[a1, a2, a3]
resultList2:[b1, b2, b3]
最好leftPush用leftPoP遍历,rightPush用rightPoP遍历
*/
rightPop(K key, long timeout, TimeUnit unit):移除集合中右边的元素在等待的时间里,如果超过等待的时间仍没有元素则退出。
popValue = redisTemplate.opsForList().rightPop("presentList",1, TimeUnit.SECONDS);
remove(K key, long count, Object value)
从存储在键中的列表中删除等于值的元素的第一个计数事件。count> 0:删除等于从左到右移动的值的第一个元素;count< 0:删除等于从右到左移动的值的第一个元素;count = 0:删除等于value的所有元素。
long removeCount = redisTemplate.opsForList().remove("list",0,"w");
list = redisTemplate.opsForList().range("list",0,-1);
System.out.println("通过remove(K key, long count, Object value)方法移除元素数量:" + removeCount);
System.out.println(",剩余的元素:" + list);
range(K key, long start, long end)--获取指定区间的值
保存和读取String(最常用的)
System.out.println("缓存正在设置。。。。。。。。。");
redisTemplate.opsForValue().set("key1","value1");
redisTemplate.opsForValue().set("key2","value2");
redisTemplate.opsForValue().set("key3","value3");
redisTemplate.opsForValue().set("key4","value4");
System.out.println("缓存已经设置完毕。。。。。。。");
String result1=redisTemplate.opsForValue().get("key1").toString();
String result2=redisTemplate.opsForValue().get("key2").toString();
String result3=redisTemplate.opsForValue().get("key3").toString();
System.out.println("缓存结果为:result:"+result1+" "+result2+" "+result3);
//也可以存对象-- implements Serializable
User user = new User();
user.setName("哈哈");
redisTemplate.opsForValue().set("user1", user);
User user2 = (User) redisTemplate.opsForValue().get("user1");