由于之前的老的項(xiàng)目一直在使用 Jedis 作為Redis的Java客戶端操作數(shù)據(jù)脚仔,Jedis 與JedisPool 作為官方推薦的API澜沟,其操作與redis-cli 客戶端操作如出一轍,在Spring 大行其道的時(shí)代,當(dāng)然也需要好好了解下RedisTemplate 的操作以及源碼顽素。
RedisTemplate的類圖如下:
Redis的類圖.jpg
通過源碼分析:
RedisOperations --定義了Redis的基本操作粱胜,提供可擴(kuò)展性飒硅,由 RedisTemplate 實(shí)現(xiàn)。
RedisAccessor --定義了RedisTemplate 的一些公有屬性
InitializingBean --Spring Bean 對象的初始化作谚,其內(nèi)部僅僅有一個(gè)方法:afterPropertiesSet 三娩,只要是實(shí)現(xiàn)該接口,均會在Bean對象初始化時(shí)調(diào)用該方法
RedisAccessor 源碼
public class RedisAccessor implements InitializingBean {
protected final Log logger = LogFactory.getLog(getClass());
private RedisConnectionFactory connectionFactory;
public void afterPropertiesSet() {
Assert.notNull(getConnectionFactory(), "RedisConnectionFactory is required");
}
/**
* Returns the connectionFactory.
*/
public RedisConnectionFactory getConnectionFactory() {
return connectionFactory;
}
/**
* Sets the connection factory.
*/
public void setConnectionFactory(RedisConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
InitializingBean 源碼
public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
RedisTemplate 源碼分析
Helper class that simplifies Redis data access code.
簡化Redis數(shù)據(jù)訪問代碼的Helper類
RedisTemplate的核心方法execute
public <T> T execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline) {
Assert.isTrue(initialized, "template not initialized; call afterPropertiesSet() before using it");
Assert.notNull(action, "Callback object must not be null");
RedisConnectionFactory factory = getConnectionFactory();
RedisConnection conn = null;
try {
if (enableTransactionSupport) {
// only bind resources in case of potential transaction synchronization
conn = RedisConnectionUtils.bindConnection(factory, enableTransactionSupport);
} else {
conn = RedisConnectionUtils.getConnection(factory);
}
boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);
RedisConnection connToUse = preProcessConnection(conn, existingConnection);
boolean pipelineStatus = connToUse.isPipelined();
if (pipeline && !pipelineStatus) {
connToUse.openPipeline();
}
RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
T result = action.doInRedis(connToExpose);
// close pipeline
if (pipeline && !pipelineStatus) {
connToUse.closePipeline();
}
// TODO: any other connection processing?
return postProcessResult(result, connToUse, existingConnection);
} finally {
if (enableTransactionSupport) {
RedisConnectionUtils.unbindConnection(factory);
} else {
RedisConnectionUtils.releaseConnection(conn, factory);
}
}
}
- 根據(jù)RedisConnectionFactory 獲取 RedisConnection
- 執(zhí)行RedisCallback. doInRedis
- 返回結(jié)果
RedisTemplate String 字符串操作
public class StringRedisTemplate extends RedisTemplate<String, String> {
public StringRedisTemplate() {
RedisSerializer<String> stringSerializer = new StringRedisSerializer();
setKeySerializer(stringSerializer);
setValueSerializer(stringSerializer);
setHashKeySerializer(stringSerializer);
setHashValueSerializer(stringSerializer);
}
public StringRedisTemplate(RedisConnectionFactory connectionFactory) {
this();
setConnectionFactory(connectionFactory);
afterPropertiesSet();
}
protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
return new DefaultStringRedisConnection(connection);
}
}
未完妹懒,待續(xù)