RedisTemplate API使用

因爲以前的老的項目一直在使用 Jedis 做爲Redis的Java客戶端操做數據,Jedis 與JedisPool 做爲官方推薦的API,其操做與redis-cli 客戶端操做一模一樣,在Spring 大行其道的時代,固然也須要好好了解下RedisTemplate 的操做以及源碼。
RedisTemplate的類圖以下:
Redis的類圖.jpg

經過源碼分析:
RedisOperations --定義了Redis的基本操做,提供可擴展性,由 RedisTemplate 實現。
RedisAccessor --定義了RedisTemplate 的一些公有屬性
InitializingBean --Spring Bean 對象的初始化,其內部僅僅有一個方法:afterPropertiesSet ,只要是實現該接口,均會在Bean對象初始化時調用該方法java

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數據訪問代碼的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);
            }
        }
    }
  1. 根據RedisConnectionFactory 獲取 RedisConnection
  2. 執行RedisCallback. doInRedis
  3. 返回結果
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);
    }
}
未完,待續
相關文章
相關標籤/搜索