Spring Data Redis常见操作总结
我列出来的都是最常用的,其他的你要自己去搜搜
1. 列表类型数据
@Autowired
private RedisTemplate<String ,Object> redisTemplate;
public void f1() {
String k = "key";
ListOperations<String, Object> list = redisTemplate.opsForList();
redisTemplate.expire(k, 2, TimeUnit.HOURS);//设置过期时间
list.rightPush(k,"a");//在尾部插入
list.leftPush(k,"c");//在头部插入
List<Object> range = list.range(k, 0, -1);//获取指定范围的列表,在这里获取全部
System.out.println(range);//输出:[c, a]
list.set(k,0,"new");//设置列表指定索引的值
list.set(k,1,"old");
List<Object> range2 = list.range(k, 0, -1);
System.out.println(range2);//输出:[new, old]
}
2. 字符串类型数据
@Autowired
private RedisTemplate<String ,Object> redisTemplate;
@org.junit.jupiter.api.Test
public void f1() {
String k = "ts";
ValueOperations<String, Object> operations = redisTemplate.opsForValue();
redisTemplate.expire(k, 2, TimeUnit.HOURS);
operations.set(k,"09000");
System.out.println(operations.get(k));//09000
}
3. 有序集合类型数据
@Autowired
private RedisTemplate<String ,Object> redisTemplate;
@org.junit.jupiter.api.Test
public void f1() {
String k = "dc";
SetOperations<String, Object> set = redisTemplate.opsForSet();
redisTemplate.expire(k, 2, TimeUnit.HOURS);
set.add(k,"a");
set.add(k,"b");
set.add(k,"c");
System.out.println(set.members(k));//获取集合的所有元素,输出:[a, b, c]
}
原文地址:https://blog.csdn.net/2301_77615930/article/details/143762653
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!