1. 程式人生 > >強大的Spring快取技術(上)

強大的Spring快取技術(上)

快取是實際工作中非常常用的一種提高效能的方法, 我們會在許多場景下來使用快取。

本文通過一個簡單的例子進行展開,通過對比我們原來的自定義快取和 spring 的基於註釋的 cache 配置方法,展現了 spring cache 的強大之處,然後介紹了其基本的原理,擴充套件點和使用場景的限制。通過閱讀本文,你應該可以短時間內掌握 spring 帶來的強大快取技術,在很少的配置下即可給既有程式碼提供快取能力。

概述

Spring 3.1 引入了激動人心的基於註釋(annotation)的快取(cache)技術,它本質上不是一個具體的快取實現方案(例如EHCache 或者 OSCache),而是一個對快取使用的抽象,通過在既有程式碼中新增少量它定義的各種 annotation,即能夠達到快取方法的返回物件的效果。

Spring 的快取技術還具備相當的靈活性,不僅能夠使用 SpEL(Spring Expression Language)來定義快取的 key 和各種 condition,還提供開箱即用的快取臨時儲存方案,也支援和主流的專業快取例如 EHCache 整合。

其特點總結如下:

  • 通過少量的配置 annotation 註釋即可使得既有程式碼支援快取

  • 支援開箱即用 Out-Of-The-Box,即不用安裝和部署額外第三方元件即可使用快取

  • 支援 Spring Express Language,能使用物件的任何屬性或者方法來定義快取的 key 和 condition

  • 支援 AspectJ,並通過其實現任何方法的快取支援

  • 支援自定義 key 和自定義快取管理者,具有相當的靈活性和擴充套件性

本文將針對上述特點對 Spring cache 進行詳細的介紹,主要通過一個簡單的例子和原理介紹展開,然後我們將一起看一個比較實際的快取例子,最後會介紹 spring cache 的使用限制和注意事項。好吧,讓我們開始吧

我們以前如何自己實現快取的呢

這裡先展示一個完全自定義的快取實現,即不用任何第三方的元件來實現某種物件的記憶體快取。

場景如下:

對一個賬號查詢方法做快取,以賬號名稱為 key,賬號物件為 value,當以相同的賬號名稱查詢賬號的時候,直接從快取中返回結果,否則更新快取。賬號查詢服務還支援 reload 快取(即清空快取)

首先定義一個實體類:賬號類,具備基本的 id 和 name 屬性,且具備 getter 和 setter 方法

public class Account {
    private int id;
    private String name;
    public Account(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

然後定義一個快取管理器,這個管理器負責實現快取邏輯,支援物件的增加、修改和刪除,支援值物件的泛型。如下:

import com.google.common.collect.Maps;
import java.util.Map;
/**
 * @author wenchao.ren
 *         2015/1/5.
 */
public class CacheContext<T> {
    private Map<String, T> cache = Maps.newConcurrentMap();
    public T get(String key){
        return  cache.get(key);
    }
    public void addOrUpdateCache(String key,T value) {
        cache.put(key, value);
    }
    // 根據 key 來刪除快取中的一條記錄
    public void evictCache(String key) {
        if(cache.containsKey(key)) {
            cache.remove(key);
        }
    }
    // 清空快取中的所有記錄
    public void evictCache() {
        cache.clear();
    }
}

好,現在我們有了實體類和一個快取管理器,還需要一個提供賬號查詢的服務類,此服務類使用快取管理器來支援賬號查詢快取,如下:

import com.google.common.base.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
 * @author wenchao.ren
 *         2015/1/5.
 */
@Service
public class AccountService1 {
    private final Logger logger = LoggerFactory.getLogger(AccountService1.class);
    @Resource
    private CacheContext<Account> accountCacheContext;
    public Account getAccountByName(String accountName) {
        Account result = accountCacheContext.get(accountName);
        if (result != null) {
            logger.info("get from cache... {}", accountName);
            return result;
        }
        Optional<Account> accountOptional = getFromDB(accountName);
        if (!accountOptional.isPresent()) {
            throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));
        }
        Account account = accountOptional.get();
        accountCacheContext.addOrUpdateCache(accountName, account);
        return account;
    }
    public void reload() {
        accountCacheContext.evictCache();
    }
    private Optional<Account> getFromDB(String accountName) {
        logger.info("real querying db... {}", accountName);
        //Todo query data from database
        return Optional.fromNullable(new Account(accountName));
    }
}

現在我們開始寫一個測試類,用於測試剛才的快取是否有效

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class AccountService1Test {
    private AccountService1 accountService1;
    private final Logger logger = LoggerFactory.getLogger(AccountService1Test.class);
    @Before
    public void setUp() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");
        accountService1 = context.getBean("accountService1", AccountService1.class);
    }
    @Test
    public void testInject(){
        assertNotNull(accountService1);
    }
    @Test
    public void testGetAccountByName() throws Exception {
        accountService1.getAccountByName("accountName");
        accountService1.getAccountByName("accountName");
        accountService1.reload();
        logger.info("after reload ....");
        accountService1.getAccountByName("accountName");
        accountService1.getAccountByName("accountName");
    }
}

按照分析,執行結果應該是:首先從資料庫查詢,然後直接返回快取中的結果,重置快取後,應該先從資料庫查詢,然後返回快取中的結果. 檢視程式執行的日誌如下:

00:53:17.166 [main] INFO  c.r.s.cache.example1.AccountService - real querying db... accountName

00:53:17.168 [main] INFO  c.r.s.cache.example1.AccountService - get from cache... accountName

00:53:17.168 [main] INFO  c.r.s.c.example1.AccountServiceTest - after reload ....

00:53:17.168 [main] INFO  c.r.s.cache.example1.AccountService - real querying db... accountName

00:53:17.169 [main] INFO  c.r.s.cache.example1.AccountService - get from cache... accountName

可以看出我們的快取起效了,但是這種自定義的快取方案有如下劣勢:

  • 快取程式碼和業務程式碼耦合度太高,如上面的例子,AccountService 中的 getAccountByName()方法中有了太多快取的邏輯,不便於維護和變更

  • 不靈活,這種快取方案不支援按照某種條件的快取,比如只有某種型別的賬號才需要快取,這種需求會導致程式碼的變更

  • 快取的儲存這塊寫的比較死,不能靈活的切換為使用第三方的快取模組

如果你的程式碼中有上述程式碼的影子,那麼你可以考慮按照下面的介紹來優化一下你的程式碼結構了,也可以說是簡化,你會發現,你的程式碼會變得優雅的多!

Spring cache是如何做的呢

我們對AccountService1 進行修改,建立AccountService2:

import com.google.common.base.Optional;
import com.rollenholt.spring.cache.example1.Account;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
 * @author wenchao.ren
 *         2015/1/5.
 */
@Service
public class AccountService2 {
    private final Logger logger = LoggerFactory.getLogger(AccountService2.class);
    // 使用了一個快取名叫 accountCache
    @Cacheable(value="accountCache")
    public Account getAccountByName(String accountName) {
        // 方法內部實現不考慮快取邏輯,直接實現業務
        logger.info("real querying account... {}", accountName);
        Optional<Account> accountOptional = getFromDB(accountName);
        if (!accountOptional.isPresent()) {
            throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));
        }
        return accountOptional.get();
    }
    private Optional<Account> getFromDB(String accountName) {
        logger.info("real querying db... {}", accountName);
        //Todo query data from database
        return Optional.fromNullable(new Account(accountName));
    }
}

我們注意到在上面的程式碼中有一行:

@Cacheable(value="accountCache")

這個註釋的意思是,當呼叫這個方法的時候,會從一個名叫 accountCache 的快取中查詢,如果沒有,則執行實際的方法(即查詢資料庫),並將執行的結果存入快取中,否則返回快取中的物件。這裡的快取中的 key 就是引數 accountName,value 就是 Account 物件。“accountCache”快取是在 spring*.xml 中定義的名稱。我們還需要一個 spring 的配置檔案來支援基於註釋的快取

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
    <context:component-scan base-package="com.rollenholt.spring.cache"/>
    <context:annotation-config/>
    <cache:annotation-driven/>
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="default"/>
                </bean>
                <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                    <property name="name" value="accountCache"/>
                </bean>
            </set>
        </property>
    </bean> 
</beans>

注意這個 spring 配置檔案有一個關鍵的支援快取的配置項:

<cache:annotation-driven />

這個配置項預設使用了一個名字叫 cacheManager 的快取管理器,這個快取管理器有一個 spring 的預設實現,即 org.springframework.cache.support.SimpleCacheManager,這個快取管理器實現了我們剛剛自定義的快取管理器的邏輯,它需要配置一個屬性 caches,即此快取管理器管理的快取集合,除了預設的名字叫 default 的快取,我們還自定義了一個名字叫 accountCache 的快取,使用了預設的記憶體儲存方案 ConcurrentMapCacheFactoryBean,它是基於 java.util.concurrent.ConcurrentHashMap 的一個記憶體快取實現方案。

然後我們編寫測試程式:

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class AccountService2Test {
    private AccountService2 accountService2;
    private final Logger logger = LoggerFactory.getLogger(AccountService2Test.class);
    @Before
    public void setUp() throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
        accountService2 = context.getBean("accountService2", AccountService2.class);
    }
    @Test
    public void testInject(){
        assertNotNull(accountService2);
    }
    @Test
    public void testGetAccountByName() throws Exception {
        logger.info("first query...");
        accountService2.getAccountByName("accountName");
        logger.info("second query...");
        accountService2.getAccountByName("accountName");
    }
}

上面的測試程式碼主要進行了兩次查詢,第一次應該會查詢資料庫,第二次應該返回快取,不再查資料庫,我們執行一下,看看結果

01:10:32.435 [main] INFO  c.r.s.c.example2.AccountService2Test - first query...

01:10:32.456 [main] INFO  c.r.s.cache.example2.AccountService2 - real querying account... accountName

01:10:32.457 [main] INFO  c.r.s.cache.example2.AccountService2 - real querying db... accountName

01:10:32.458 [main] INFO  c.r.s.c.example2.AccountService2Test - second query...

可以看出我們設定的基於註釋的快取起作用了,而在 AccountService.java 的程式碼中,我們沒有看到任何的快取邏輯程式碼,只有一行註釋:@Cacheable(value=”accountCache”),就實現了基本的快取方案,是不是很強大?