1、SDR說明
Spring Data Redis(SDR),是SpringFramework提供的一套簡化訪問Redis的API,是對Jedis的又一層封裝。
SDR集成了Jedis,JRedis,SRP,Lettuce這四種開源的Redis Connector,這些Connector都是針對於Redis的開源Java庫。其中,JRedis和SRP從spring-data-redis1.7開始,就不支持了。
2、RedisTemplate說明
RedisTemplate是SDR的一個核心Helper類,該類是一個高級的抽象(對Jedis的又一層封裝),它封裝了對Redis中的數據的CRUD操作,包含“高級封裝”。
(1)高級封裝(推薦使用)
高級封裝的操作包含:OpsForValue(),OpsForList(),OpsForSet(),OpsForZset(),OpsForHash()等等。
SDR官方文檔中對Redistemplate的介紹:the template is in fact the central class of the Redis module due to its rich feature set. The template offers a high-level abstraction for Redis interactions.
通過Redistemplate可以調用ValueOperations和ListOperations等等方法,分別是對Redis命令的高級封裝。
但是ValueOperations等等這些命令最終是要轉化成為RedisCallback來執行的。也就是說通過使用RedisCallback可以實現更強的功能,SDR文檔對RedisCallback的介紹:RedisTemplate and StringRedisTemplate allow the developer to talk directly to Redis through the RedisCallback interface. This gives complete control to the developer as it talks directly to the RedisConnection。
(2)從高級封裝獲得低級封裝的過渡:
RedisOperations<String, Object> operations = opsForValue.getOperations();
3、RedisConnection提供了“低級封裝”。
(1)低級封裝
低級封裝的操作是通過連接到Redis的Connection對象,直接對Redis數據進行操作。
低級封裝的核心是:redisTemplate.execute(new RedisCallback(){})
【參考資料】
01.《Spring Data Redis官方文檔》:
http://docs.spring.io/spring-data/redis/docs/1.7.2.RELEASE/reference/html/