Spring-data-redis 为spring-data模块中对redis的支持部分,简称为“SDR”,提供了基于jedis客户端API的高度封装以及与spring容器的整合,事实上jedis客户端已经足够简单和轻量级,而spring-data-redis反而具有“过度设计”的嫌疑。
一. jedis客户端在编程实施方面存在如下不足:
1) connection管理缺乏自动化,connection-pool的设计缺少必要的容器支持。
2) 数据操作需要关注“序列化”/“反序列化”,因为jedis的客户端API接受的数据类型为string和byte,对结构化数据(json,xml,pojo等)操作需要额外的支持。
3) 事务操作纯粹为硬编码
4) pub/sub功能,缺乏必要的设计模式支持,对于开发者而言需要关注的太多。
不过jedis与spring整合,也是非常的简单,参见“jedis连接池实例”.
二. spring-data-redis针对jedis提供了如下功能 :
1 . 连接池自动管理,提供了一个高度封装的“RedisTemplate”类
2 . 针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口
3 . 提供了对key的“bound”(绑定)便捷化操作API,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key,即BoundKeyOperations:
BoundValueOperations
BoundSetOperations
BoundListOperations
BoundSetOperations
BoundHashOperations
4 . 将事务操作封装,有容器控制。
5 . 针对数据的“序列化/反序列化”,提供了多种可选择策略(RedisSerializer)
JdkSerializationRedisSerializer:POJO对象的存取场景,使用JDK本身序列化机制,将pojo类通过ObjectInputStream/ObjectOutputStream进行序列化操作,最终redis-server中将存储字节序列。是目前最常用的序列化策略。
StringRedisSerializer:Key或者value为字符串的场景,根据指定的charset对数据的字节序列编码成string,是“new String(bytes, charset)”和“string.getBytes(charset)”的直接封装。是最轻量级和高效的策略。
JacksonJsonRedisSerializer:jackson-json工具提供了javabean与json之间的转换能力,可以将pojo实例序列化成json格式存储在redis中,也可以将json格式的数据转换成pojo实例。因为jackson工具在序列化和反序列化时,需要明确指定Class类型,因此此策略封装起来稍微复杂。【需要jackson-mapper-asl工具支持】
OxmSerializer:提供了将javabean与xml之间的转换能力,目前可用的三方支持包括jaxb,apache-xmlbeans;redis存储的数据将是xml工具。不过使用此策略,编程将会有些难度,而且效率最低;不建议使用。【需要spring-oxm模块的支持】
针对“序列化和发序列化”中JdkSerializationRedisSerializer和StringRedisSerializer是最基础的策略,原则上,我们可以将数据存储为任何格式以便应用程序存取和解析(其中应用包括app,hadoop等其他工具),不过在设计时仍然不推荐直接使用“JacksonJsonRedisSerializer”和“OxmSerializer”,因为无论是json还是xml,他们本身仍然是String。
如果你的数据需要被第三方工具解析,那么数据应该使用StringRedisSerializer而不是JdkSerializationRedisSerializer。
如果你的数据格式必须为json或者xml,那么在编程级别,在redisTemplate配置中仍然使用StringRedisSerializer,在存储之前或者读取之后,使用“SerializationUtils”工具转换转换成json或者xml,请参见下文实例。
6 . 基于设计模式,和JMS开发思路,将pub/sub的API设计进行了封装,使开发更加便捷。
7 .spring-data-redis中,并没有对sharding提供良好的封装,如果你的架构是基于sharding,那么你需要自己去实现,这也是sdr和jedis相比,唯一缺少的特性。
三. 有关BoundKeyOperations中API的用法
public void boundHashOps(String tableName) {
System.out.println("==================Hash==============" );
BoundHashOperations ops = stringRedisTemplate
.boundHashOps(tableName);
stringRedisTemplate.delete("student" );
stringRedisTemplate.delete("student:1" );
ops.put("cs01" , "123" );
String key1 = ops.getKey();
System.out.println("key1:" + key1);
String key11 = ops.get("cs01" );
System.out.println("key11:" + key11);
ops.putIfAbsent("cs02" , "456" );
String key2 = ops.getKey();
System.out.println("ops.getKey()-key2:" + key2);
String key21 = ops.get("cs02" );
System.out.println("ops.get(cs02)-key21:" + key21);
Map maps = ops.entries();
for (String key : maps.keySet()) {
System.out.println("map-key:" + key + "map-value:" + maps.get(key));
}
System.out.println("ops.getExpire():" + ops.getExpire());
System.out.println("ops.expireAt(new Date()):"
+ ops.expireAt(new Date()));
System.out.println("ops.getType():" + ops.getType());
System.out.println("ops.hasKey(cs01):" + ops.hasKey( "cs01" ));
System.out.println("ops.hasKey(cs02):" + ops.hasKey( "cs02" ));
System.out.println("ops.size():" + ops.size());
Set keys = ops.keys();
for (String string : keys) {
System.out.println("ops.keys():" + string);
}
System.out.println("ops.values():" + ops.values());
System.out.println("ops.size():" + ops.size());
ops.delete("cs01" );
}
public void opsForHash(String tableName) {
System.out.println("==================Hash==============" );
HashOperations ops = stringRedisTemplate
.opsForHash();
stringRedisTemplate.delete("student" );
stringRedisTemplate.delete("student:1" );
ops.put(tableName, "cs01" , "123" );
Object key11 = ops.get(tableName, "cs01" );
System.out.println("key11:" + key11);
ops.putIfAbsent(tableName, "cs02" , "456" );
Object key21 = ops.get(tableName, "cs02" );
System.out.println("ops.get(cs02)-key21:" + key21);
Map maps = ops.entries(tableName);
for (Object key : maps.keySet()) {
System.out.println("map-key:" + key + "map-value:" + maps.get(key));
}
System.out.println("ops.hasKey(cs01):"
+ ops.hasKey(tableName, "cs01" ));
System.out.println("ops.hasKey(cs02):"
+ ops.hasKey(tableName, "cs02" ));
System.out.println("ops.size():" + ops.size(tableName));
Set keys = ops.keys(tableName);
for (Object string : keys) {
System.out.println("ops.keys():" + string);
}
System.out.println("ops.values():"
+ ops.values(tableName));
System.out.println("ops.size():"
+ ops.size(tableName));
ops.delete("cs01" );
}
public void boundListOps(String tableName) {
System.out.println("==================List==============" );
BoundListOperations ops = stringRedisTemplate
.boundListOps(tableName);
ops.leftPush("cs01" );
ops.leftPushIfPresent("cs011" );
ops.leftPush("cs01" , "cs0111" );
ops.leftPushAll("cs01111" , "cs011111" );
List values = ops.range(0 , - 1 );
for (String string : values) {
System.out.println("letf push:" + string);
}
ops.rightPush("cs02" );
ops.rightPushIfPresent("cs022" );
ops.rightPush("cs02" , "cs0222" );
ops.rightPushAll("cs02222" , "cs022222" );
ops.set(0 , "cs04" );
List values1 = ops.range(0 , - 1 );
for (String string : values1) {
System.out.println("right push:" + string);
}
List values2 = ops.range(1 , 2 );
for (String string : values2) {
System.out.println("right push1:" + string);
}
System.out.println("ops.index(1):" + ops.index( 0 ));
System.out.println("ops.remove(0, cs01):"
+ ops.remove(0 , "cs01" ));
System.out.println("ops.leftPop():" + ops.leftPop());
System.out.println("ops.rightPop():" + ops.rightPop());
System.out.println("ops.remove(0, cs01)1:"
+ ops.remove(0 , "cs01" ));
}
public void boundSetOps() {
System.out.println("==================Set==============" );
String tableName2 = "caoshuai03" ;
BoundSetOperations ops = stringRedisTemplate
.boundSetOps(tableName2);
String[] values = { "cs03" , "cs04" };
System.out.println("ops.add(values):"
+ ops.add(values));
Set sets1 = ops.members();
for (String string : sets1) {
System.out.println("ops.members()1:" + string);
}
System.out.println("ops.randomMember():" + ops.randomMember());
System.out.println("ops.randomMembers(1):" + ops.randomMembers( 1 ));
System.out.println("ops.randomMembers(2):" + ops.randomMembers( 2 ));
System.out.println("ops.distinctRandomMembers(1):"
+ ops.distinctRandomMembers(1 ));
System.out.println("ops.distinctRandomMembers(2):"
+ ops.distinctRandomMembers(2 ));
System.out.println(ops.isMember("cs04" ));
System.out.println(ops.isMember("cs01" ));
System.out.println(ops.size());
System.out.println(ops.getKey());
System.out.println(ops.getType());
Set set7 = ops.diff("caoshuai02" );
for (String string : set7) {
System.out.println("ops.diff(caoshuai02):" + string);
}
Set set8 = ops.intersect("caoshuai02" );
for (String string : set8) {
System.out.println("ops.intersect(caoshuai02):" + string);
}
Set set6 = ops.union("caoshuai02" );
for (String string : set6) {
System.out.println("ops.union(caoshuai02):" + string);
}
List keys = new ArrayList();
keys.add("caoshuai02" );
Set set = ops.intersect(keys);
for (String string : set) {
System.out.println("ops.intersect(keys):" + string);
}
List keys1 = new ArrayList();
keys1.add("caoshuai02" );
Set set3 = ops.diff(keys);
for (String string : set3) {
System.out.println("ops.diff(keys)3:" + string);
}
List keys2 = new ArrayList();
keys2.add("caoshuai02" );
Set set4 = ops.union(keys2);
for (String string : set4) {
System.out.println("ops.union(keys2):" + string);
}
ops.diffAndStore("caoshuai02" , "caoshuai04" );
ops.intersectAndStore("caoshuai02" , "caoshuai05" );
ops.unionAndStore("caoshuai02" , "caoshuai06" );
System.out.println(ops.move("caoshuai07" , "cs03" ));
System.out.println(ops.move("caoshuai07" , "cs01" ));
System.out.println(ops.pop());
System.out.println(ops.remove("cs03" ));
System.out.println(ops.remove("cs01" ));
}
public void boundValueOps() {
System.out.println("==================String==============" );
String tableName2 = "LiMing01" ;
BoundValueOperations ops = stringRedisTemplate
.boundValueOps(tableName2);
System.out.println(ops.append("stu01" ));
System.out.println(ops.getAndSet("stu02" ));
System.out.println(ops.get());
System.out.println(ops.get(0 , 1 ));
System.out.println(ops.get(0 , 5 ));
System.out.println(ops.size());
ops.set("stu03" );
System.out.println("ops.set(stu03):" + ops.get());
ops.set("stu04" , 0 );
System.out.println("ops.set(stu04, 0):" + ops.get());
System.out.println(ops.setIfAbsent("stu04" ));
System.out.println("ops.setIfAbsent(stu04):" +ops.get());
stringRedisTemplate.delete(tableName2);
System.out.println(ops.setIfAbsent("stu06" ));
System.out.println("ops.setIfAbsent(stu06):" + ops.get());
ops.set("stu05" , 30 , TimeUnit.SECONDS);
}
public void boundZSetOps() {
System.out.println("==================Zset==============" );
String tableName2 = "LiMing03" ;
BoundZSetOperations ops = stringRedisTemplate
.boundZSetOps(tableName2);
System.out.println(ops.add("stu01" , 1 ));
System.out.println(ops.add("stu03" , 1 ));
System.out.println(ops.count(0 , 1 ));
System.out.println(ops.incrementScore("stu04" , 4 ));
ops.intersectAndStore("LiMing02" , "LiMing04" );
ops.unionAndStore("LiMing02" , "LiMing05" );
Set> sets = new HashSet>();
TypedTuple typedTuple = new TypedTuple() {
@Override
public int compareTo(TypedTuple o) {
return 0 ;
}
@Override
public String getValue() {
return "stu06" ;
}
@Override
public Double getScore() {
return 6.0 ;
}
};
sets.add(typedTuple);
ops.add(sets);
Set set1 = ops.range(0 , - 1 );
for (String string : set1) {
System.out.println("ops.range(0, 1):" + string);
}
Set set2 = ops.rangeByScore(1 , 4 );
for (String string : set2) {
System.out.println("ops.rangeByScore(0, 4):" + string);
}
Set> set3 = ops.rangeWithScores(0 , 4 );
for (TypedTuple string : set3) {
System.out.println("ops.rangeByScore(0, 4):" + string.getValue()
+ "score:" + string.getScore());
}
System.out.println(ops.remove("stu01" ));
Set set5 = ops.range(0 , - 1 );
for (String string : set5) {
System.out.println("ops.range(0, 1)5:" + string);
}
ops.removeRangeByScore(1 , 4 );
Set set6 = ops.range(0 , - 1 );
for (String string : set6) {
System.out.println("ops.range(0, 1)6:" + string);
}
ops.removeRange(0 , 1 );
Set set7 = ops.range(0 , - 1 );
for (String string : set7) {
System.out.println("ops.range(0, 1)7:" + string);
}
}