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接口
- ValueOperations:簡單K-V操作
- SetOperations:set類型數據操作
- ZSetOperations:zset類型數據操作
- HashOperations:針對map類型的數據操作
-
ListOperations:針對list類型的數據操作
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的用法
/**
* 需要指定hashtable的名字
*
* @param tableName
*/
public void boundHashOps(String tableName) {
System.out.println("==================Hash==============");
BoundHashOperations<String, String, String> ops = stringRedisTemplate
.boundHashOps(tableName);
stringRedisTemplate.delete("student");
stringRedisTemplate.delete("student:1");
ops.put("cs01", "123");// 存入數據 ops.putAll(maps); 存入多條數據
String key1 = ops.getKey();// tableName的名字
System.out.println("key1:" + key1);
String key11 = ops.get("cs01");
System.out.println("key11:" + key11);// 獲取key的值
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<String, String> maps = ops.entries();// 獲取所有的key-value值
for (String key : maps.keySet()) {
System.out.println("map-key:" + key + "map-value:" + maps.get(key));
}
// ops.persist();//刪除過期(如果有的話)的數據。
System.out.println("ops.getExpire():" + ops.getExpire());// -1
System.out.println("ops.expireAt(new Date()):"
+ ops.expireAt(new Date()));// true 設置生存過期時間
System.out.println("ops.getType():" + ops.getType());// Hash
System.out.println("ops.hasKey(cs01):" + ops.hasKey("cs01"));// true
System.out.println("ops.hasKey(cs02):" + ops.hasKey("cs02"));// true
System.out.println("ops.size():" + ops.size());// 2
Set<String> keys = ops.keys();// 獲取所有的key
for (String string : keys) {
System.out.println("ops.keys():" + string);
}
System.out.println("ops.values():" + ops.values());// 獲取所有的value
System.out.println("ops.size():" + ops.size());// 2 獲取數量
ops.delete("cs01");// 刪除key爲cs01的數據
}
/**
* 未指定hashtable的名字
*
* @param tableName
*/
public void opsForHash(String tableName) {
System.out.println("==================Hash==============");
HashOperations<String, Object, Object> ops = stringRedisTemplate
.opsForHash();
stringRedisTemplate.delete("student");
stringRedisTemplate.delete("student:1");
ops.put(tableName, "cs01", "123");// 存入數據 ops.putAll(maps); 存入多條數據
Object key11 = ops.get(tableName, "cs01");
System.out.println("key11:" + key11);// 獲取key的值
ops.putIfAbsent(tableName, "cs02", "456");
Object key21 = ops.get(tableName, "cs02");
System.out.println("ops.get(cs02)-key21:" + key21);
Map<Object, Object> maps = ops.entries(tableName);// 獲取所有的key-value值
for (Object key : maps.keySet()) {
System.out.println("map-key:" + key + "map-value:" + maps.get(key));
}
// ops.persist();//刪除過期(如果有的話)的數據。
System.out.println("ops.hasKey(cs01):"
+ ops.hasKey(tableName, "cs01"));// true
System.out.println("ops.hasKey(cs02):"
+ ops.hasKey(tableName, "cs02"));// true
System.out.println("ops.size():" + ops.size(tableName));// 2
Set<Object> keys = ops.keys(tableName);// 獲取所有的key
for (Object string : keys) {
System.out.println("ops.keys():" + string);
}
System.out.println("ops.values():"
+ ops.values(tableName));// 獲取所有的value
System.out.println("ops.size():"
+ ops.size(tableName));// 2 獲取數量
ops.delete("cs01");// 刪除key爲cs01的數據
}
/**
* List 裏面有重複數據
*
* @param tableName
*/
public void boundListOps(String tableName) {
System.out.println("==================List==============");
BoundListOperations<String, String> ops = stringRedisTemplate
.boundListOps(tableName);
ops.leftPush("cs01");// left push 左側入棧,先進後出,和右側相比,左側先出
ops.leftPushIfPresent("cs011");// 不知道和上面的有什麼區別
ops.leftPush("cs01", "cs0111");// 在cs01的左側入棧
ops.leftPushAll("cs01111", "cs011111");
List<String> values = ops.range(0, -1);
for (String string : values) {
System.out.println("letf push:" + string);
}
ops.rightPush("cs02");// right push 右側入棧 先進先出 ,和左側相比,左側先出
ops.rightPushIfPresent("cs022");
ops.rightPush("cs02", "cs0222");// 在cs02的右側入棧
ops.rightPushAll("cs02222", "cs022222");
ops.set(0, "cs04");// 把第一個數據替換成cs04
// ops.trim(0, 3);//從第一個數據到第4個數據刪除
List<String> values1 = ops.range(0, -1);// 查出所有數據
for (String string : values1) {
System.out.println("right push:" + string);
}
List<String> 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"));// 1,刪除“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"));// 0 ,如果“cs01”不存在返回0
// ops.persist();//刪除過期(如果有的話)的數據。
}
public void boundSetOps() {
System.out.println("==================Set==============");
String tableName2 = "caoshuai03";
BoundSetOperations<String, String> ops = stringRedisTemplate
.boundSetOps(tableName2);
String[] values = { "cs03", "cs04" };
System.out.println("ops.add(values):"
+ ops.add(values));// 添加多條數據到set裏,返回添加的數量
Set<String> sets1 = ops.members();// 獲取set裏的所有數據,每次顯示的順序可能不一樣
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"));// 是否含有cs04,有的話返回true
System.out.println(ops.isMember("cs01"));// 沒有返回false
System.out.println(ops.size());// set裏數據量
System.out.println(ops.getKey());// 獲取set的名字
System.out.println(ops.getType());// 獲取類型
Set<String> set7 = ops.diff("caoshuai02");// 獲取和另一set裏不一樣的數據,差集
for (String string : set7) {
System.out.println("ops.diff(caoshuai02):" + string);
}
// 獲取和另一set裏一樣的數據,交集
Set<String> set8 = ops.intersect("caoshuai02");
for (String string : set8) {
System.out.println("ops.intersect(caoshuai02):" + string);
}
// 獲取另一set所有的數據,和自己合併起來,去掉重複的數據,並集
Set<String> set6 = ops.union("caoshuai02");
for (String string : set6) {
System.out.println("ops.union(caoshuai02):" + string);
}
// 獲取和其它set裏一樣的數據,交集
List<String> keys = new ArrayList<String>();
keys.add("caoshuai02");
Set<String> set = ops.intersect(keys);
for (String string : set) {
System.out.println("ops.intersect(keys):" + string);
}
// 獲取和其它set裏不一樣的數據,差集
List<String> keys1 = new ArrayList<String>();
keys1.add("caoshuai02");
Set<String> set3 = ops.diff(keys);
for (String string : set3) {
System.out.println("ops.diff(keys)3:" + string);
}
// 獲取其它set所有的數據,和自己合併起來,去掉重複的數據,並集
List<String> keys2 = new ArrayList<String>();
keys2.add("caoshuai02");
Set<String> set4 = ops.union(keys2);
for (String string : set4) {
System.out.println("ops.union(keys2):" + string);
}
// 獲取和另一set裏不一樣的數據,差集,存入到另一set裏
ops.diffAndStore("caoshuai02", "caoshuai04");
// ops.diffAndStore(keys, destKey);
// 獲取和另一set裏一樣的數據,交集,存入到另一set裏
ops.intersectAndStore("caoshuai02", "caoshuai05");
// ops.intersectAndStore(keys, destKey);
// 獲取另一set裏的所有數據,並集,存入到另一set裏
ops.unionAndStore("caoshuai02", "caoshuai06");
// ops.unionAndStore(keys, destKey);
// 把指定的數據移到另一set裏,移動成功返回true
System.out.println(ops.move("caoshuai07", "cs03"));
// 把指定的數據移到另一set裏,移動失敗,返回false,注:當前set裏沒有cs01
System.out.println(ops.move("caoshuai07", "cs01"));
System.out.println(ops.pop());// pop出一個數據,第一個數據被pop出
System.out.println(ops.remove("cs03"));// 刪除多個數據,返回刪除的個數
System.out.println(ops.remove("cs01"));// 刪除數據,返回刪除的個數
}
public void boundValueOps() {
System.out.println("==================String==============");
String tableName2 = "LiMing01";
BoundValueOperations<String, String> 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);//在位置0處添加數據
System.out.println("ops.set(stu04, 0):" + ops.get());// 獲取添加的數據
//如果原先的string裏有數據,則使用此方法set新數據會失敗,並返回false
System.out.println(ops.setIfAbsent("stu04"));
System.out.println("ops.setIfAbsent(stu04):" +ops.get());// 獲取添加的數據
stringRedisTemplate.delete(tableName2);//刪除此string
//如果原先的string裏沒有數據,則使用此方法set新數據會成功並返回true
System.out.println(ops.setIfAbsent("stu06"));
System.out.println("ops.setIfAbsent(stu06):" + ops.get());// 獲取添加的數據
ops.set("stu05", 30, TimeUnit.SECONDS);//設置30秒過期
}
public void boundZSetOps() {
System.out.println("==================Zset==============");
String tableName2 = "LiMing03";
BoundZSetOperations<String, String> ops = stringRedisTemplate
.boundZSetOps(tableName2);
System.out.println(ops.add("stu01", 1));//Zset裏添加數據
System.out.println(ops.add("stu03", 1));//Zset裏添加數據
System.out.println(ops.count(0, 1));//返回score在給定區間的數量
//添加數據和score,返回score的大小,如果stu04存在,則只需score相加即可
//例如:原先存在數據stu04 score是2.0,執行下面語句後變爲:stu04, score:6.0
System.out.println(ops.incrementScore("stu04", 4));
ops.intersectAndStore("LiMing02", "LiMing04");
//ops.intersectAndStore(otherKeys, destKey);
ops.unionAndStore("LiMing02", "LiMing05");
//ops.unionAndStore(otherKeys, destKey);
Set<TypedTuple<String>> sets = new HashSet<TypedTuple<String>>();
TypedTuple<String> typedTuple = new TypedTuple<String>() {
@Override
public int compareTo(TypedTuple<String> o) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getValue() {
// TODO Auto-generated method stub
return "stu06";
}
@Override
public Double getScore() {
// TODO Auto-generated method stub
return 6.0;
}
};
sets.add(typedTuple);
ops.add(sets);//添加數據
Set<String> set1 = ops.range(0, -1); //返回指定區間的元素
for (String string : set1) {
System.out.println("ops.range(0, 1):" + string);
}
Set<String> set2 = ops.rangeByScore(1, 4);//返回指定score區間的元素,包含1和4
for (String string : set2) {
System.out.println("ops.rangeByScore(0, 4):" + string);
}
//返回指定位置的元素
Set<TypedTuple<String>> set3 = ops.rangeWithScores(0, 4);
for (TypedTuple<String> string : set3) {
System.out.println("ops.rangeByScore(0, 4):" + string.getValue()
+ "score:" + string.getScore());
}
System.out.println(ops.remove("stu01"));//刪除數據,返回刪除數據的個數
Set<String> set5 = ops.range(0, -1); //返回指定區間的元素
for (String string : set5) {
System.out.println("ops.range(0, 1)5:" + string);
}
ops.removeRangeByScore(1, 4);//刪除score的範圍是1和4的數,包含1和4
Set<String> set6 = ops.range(0, -1); //返回指定區間的元素
for (String string : set6) {
System.out.println("ops.range(0, 1)6:" + string);
}
ops.removeRange(0, 1);//刪除懂第一個開始到第二個結束的數據
Set<String> set7 = ops.range(0, -1); //返回指定區間的元素
for (String string : set7) {
System.out.println("ops.range(0, 1)7:" + string);
}
}