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]
}