1. 程式人生 > >Spring-Data-Redis之RedisTemplate的使用

Spring-Data-Redis之RedisTemplate的使用

        上篇部落格是Spring-Data-Redis的例項,接著上篇的內容,這篇部落格介紹一下RedisTemplate的詳細方法。

功能介紹

        大部分的使用者都喜歡用RedisTemplate,它相應的包是org.springframework.data.redis.core。該模板實際是Redis模組的核心類,因為它的功能豐富。模板為Redis互動提供了高階抽象。雖然RedisConnection提供接受和返回二進位制值(位元組陣列)的低階方法,但該模板可以處理序列化和連線管理,使得使用者不需要處理太多的細節。

        此外,模板提供了操作檢視(按照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型別資料,比如:GEOADD,GEORADIUS,…​)

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