RedisTemplate API使用

由于之前的老的項(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);
            }
        }
    }
  1. 根據(jù)RedisConnectionFactory 獲取 RedisConnection
  2. 執(zhí)行RedisCallback. doInRedis
  3. 返回結(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ù)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末雀监,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子眨唬,更是在濱河造成了極大的恐慌会前,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匾竿,死亡現(xiàn)場離奇詭異瓦宜,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)岭妖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進(jìn)店門临庇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人昵慌,你說我怎么就攤上這事假夺。” “怎么了斋攀?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵已卷,是天一觀的道長。 經(jīng)常有香客問我淳蔼,道長侧蘸,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任肖方,我火速辦了婚禮闺魏,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘俯画。我一直安慰自己析桥,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布艰垂。 她就那樣靜靜地躺著泡仗,像睡著了一般。 火紅的嫁衣襯著肌膚如雪猜憎。 梳的紋絲不亂的頭發(fā)上娩怎,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機(jī)與錄音胰柑,去河邊找鬼截亦。 笑死爬泥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的崩瓤。 我是一名探鬼主播袍啡,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼却桶!你這毒婦竟也來了境输?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤颖系,失蹤者是張志新(化名)和其女友劉穎嗅剖,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嘁扼,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡信粮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了偷拔。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蒋院。...
    茶點(diǎn)故事閱讀 39,795評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖莲绰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情姑丑,我是刑警寧澤蛤签,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站栅哀,受9級特大地震影響震肮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜留拾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一戳晌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧痴柔,春花似錦沦偎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至谈火,卻和暖如春侈询,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背糯耍。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工扔字, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留囊嘉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓革为,卻偏偏與公主長得像扭粱,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子篷角,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評論 2 354