概述
1.模板提供了操作视图(按照Redis命令参考分组),它们提供了丰富的、现成的接口用于对特定类型或者特定键的操作(通过KeyBound接口),如下所述:
接口 | 描述 |
---|---|
Key类型操作 | |
ValueOperations | 操作Redis String(或者Value)类型数据 |
ListOperations | 操作Redis List类型数据 |
SetOperations | 操作Redis Set类型数据 |
ZSetOperations | 操作Redis ZSet(或者Sorted Set)类型数据 |
HashOperations | 操作Redis Hash类型数据 |
HyperLogLogOperations | 操作Redis HyperLogLog类型数据,比如:pfadd,pfcount,... |
GeoOperations | 操作Redis Geospatial类型数据,比如: |
Key绑定操作 | |
BoundValueOperations | Redis字符串(或值)键绑定操作 |
BoundListOperations | Redis列表键绑定操作 |
BoundSetOperations | Redis Set键绑定操作 |
BoundZSetOperations | Redis ZSet(或Sorted Set)键绑定操作 |
BoundHashOperations | Redis Hash键绑定操作 |
BoundGeoOperations | Redis Geospatial 键绑定操作 |
一旦经过配置,该模板就是线程安全的,它可以被多个实例重复使用。
开箱即用,RedisTemplate使用了基于Java的串行器来进行大部分的操作。这就意味着,任何对象通过模板的读写都会通过Java来进行序列化/反序列化。该模板的序列化机制改变起来也很容易,并且Redis模块在org.springframework.data.redis.serializer包中提供了多种可用的实现,详情请参考Serializers。你也可以通过设置enableDefaultSerializer属性为false,将其他的序列化实现都设置成null,并将RedisTemplate和原生的字节数组一起使用。注意该模板的key不允许为null值,除非底层序列化程序可以接受。获取更多序列化器的信息,请阅读javadoc。
public class RedisTemplateTest {
@SuppressWarnings("rawtypes")
@Autowired
private RedisTemplate redisTemplate;
@SuppressWarnings("unchecked")
public void findAll() {
// -----------------String类型数据操作 start--------------------
ValueOperations<String, String> stringOperations = redisTemplate
.opsForValue();
// String类型数据存储,不设置过期时间,永久性保存
stringOperations.set("string1", "fiala");
// String类型数据存储,设置过期时间为80秒,采用TimeUnit控制时间单位
stringOperations.set("string2", "fiala", 80, TimeUnit.SECONDS);
// 判断key值是否存在,存在则不存储,不存在则存储
stringOperations.setIfAbsent("string1", "my fiala");
stringOperations.setIfAbsent("string3", "my fiala");
String value1 = stringOperations.get("string1");
String value2 = stringOperations.get("string3");
System.out.println(value1);
System.out.println(value2);
// -----------------String类型数据操作 end--------------------
// -----------------其他值类型数据操作 start--------------------
Demo demo = new Demo();
demo.setId("1");
demo.setName("fiala");
List<Demo> demos = new ArrayList<Demo>();
ValueOperations<String, Object> valueOperations = redisTemplate
.opsForValue();
// 设置value为对象类型,且不设置过期时间,默认永久
valueOperations.set("value1", demo);
// 设置value为对象类型,设置过期时间为80秒,时间单位由TimeUnit控制
valueOperations.set("value2", demos, 80, TimeUnit.SECONDS);
Demo demo1 = (Demo) valueOperations.get("value1");
System.out.println(demo1.toString());
// -----------------其他值类型数据操作 end--------------------
// -----------------List数据类型操作 start------------------
ListOperations<String, Object> listOperations = redisTemplate
.opsForList();
for (int i = 0; i < 5; i++) {
Demo listDemo = new Demo();
listDemo.setId(""" + i + """);
listDemo.setName("fiala" + i);
listOperations.leftPush("list1", listDemo);
listOperations.rightPush("list2", listDemo);
}
// 可给数据排序
Demo demo2 = (Demo) listOperations.leftPop("list1");
Demo demo3 = (Demo) listOperations.rightPop("list2");
System.out.println(demo2.toString());
System.out.println(demo3.toString());
// -----------------List数据类型操作 end------------------
// -----------------set数据类型操作 start------------------
SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
for (int i = 0; i < 5; i++) {
Demo setDemo = new Demo();
setDemo.setId(""" + i + """);
setDemo.setName("fiala" + i);
setOperations.add("set1", setDemo);
}
Demo demo4 = (Demo) setOperations.pop("set1");
System.out.println(demo4.toString());
// -----------------set数据类型操作 end------------------
// -----------------zset数据类型操作 start------------------
ZSetOperations<String, Object> zSetOperations = redisTemplate
.opsForZSet();
zSetOperations.add("zset", "fiala", 0);
zSetOperations.add("zset", "my fiala", 1);
System.out.println(zSetOperations.rangeByScore("zset", 0, 1));
// -----------------zset数据类型操作 end------------------
// -----------------hash数据类型操作 start------------------
HashOperations<String, Object, Object> hashOperations = redisTemplate
.opsForHash();
Map<String, String> map = new HashMap<String, String>();
map.put("map1", "fiala1");
map.put("map2", "fiala2");
hashOperations.putAll("hash", map);
System.out.println(hashOperations.entries("hash"));
// -----------------hash数据类型操作 start------------------
}
}
最后
以上就是开放鲜花为你收集整理的RedisTemplate的使用的全部内容,希望文章能够帮你解决RedisTemplate的使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复