使用Simple-Spring-Memcached注解做緩存操作
2014-06-10 15:16 4798人閱讀 評論(0) 收藏 舉報

分類: memcache(14)
之前自己寫過一個通過注解和AOP來實作緩存的代碼,當時這段代碼寫得比較簡單,之後重構時發現之前的功能實作有很大的局限。主要問題在于:
- key的生成規則
- update 與 query 的參數不一樣,如何讓其生成一樣的key
- 清單緩存如何定義key及失效
最近同僚推薦了一個開源項目:Simple-Spring-Memcached,它也是一個通過Annatation與AOP來完成緩存資料操作的開源項目。仔細看了一下代碼,基本上把我之前碰到的問題都解決了,而且MultiCache這一塊的實作超出我的預期。該項目主要優點如下:
- 與Spring完善內建
- 支援兩種Memcached Java Client (spymemcached,Xmemcached)
- 基于Annotation方式實作緩存操作,對代碼侵入性小
- annotation豐富,可以滿足絕大部分需求
下面介紹一下其中各annotation的使用。ssm項目中的Annotation主要分成以下幾類
- SingleCache類 操作單個POJO的Cache資料,由ParameterValueKeyProvider和CacheKeyMethod來辨別組裝key
- MultiCache類 操作List型的Cache資料,由ParameterValueKeyProvider和CacheKeyMethod來辨別組裝key
- AssignCache類 指定key操作Cache資料,由annotation中的 assignedKey 指定key
各Annotation的詳細說明
-
ReadThroughSingleCache
作用:讀取Cache中資料,如果不存在,則将讀取的資料存入Cache
key生成規則:ParameterValueKeyProvider指定的參數,如果該參數對象中包含CacheKeyMethod注解的方法,則調用其方法,否則調用toString方法
代碼示例:
- [java] view plain copy print ?
- @ReadThroughSingleCache(namespace = "Alpha", expiration = 30)
- public String getDateString(@ParameterValueKeyProvider final String key) {
- final Date now = new Date();
- try {
- Thread.sleep(1500);
- } catch (InterruptedException ex) {
- }
- return now.toString() + ":" + now.getTime();
- }
InvalidateSingleCache
作用:失效Cache中的資料
key生成規則:
- 使用 ParameterValueKeyProvider注解時,與ReadThroughSingleCache一緻
- 使用 ReturnValueKeyProvider 注解時,key為傳回的對象的CacheKeyMethod或toString方法生成
- @InvalidateSingleCache(namespace = "Charlie")
- public void updateRandomString(@ParameterValueKeyProvider final Long key) {
- // Nothing really to do here.
- }
- @InvalidateSingleCache(namespace = "Charlie")
- @ReturnValueKeyProvider
- public Long updateRandomStringAgain(final Long key) {
- return key;
- }
UpdateSingleCache
作用:更新Cache中的資料
key生成規則:ParameterValueKeyProvider指定
ParameterDataUpdateContent:方法參數中的資料,作為更新緩存的資料
ReturnDataUpdateContent:方法調用後生成的資料,作為更新緩存的資料
注:上述兩個注解,必須與Update*系列的注解一起使用
[java] view plain copy print ?
- @UpdateSingleCache(namespace = "Alpha", expiration = 30)
- public void overrideDateString(final int trash, @ParameterValueKeyProvider final String key,
- @ParameterDataUpdateContent final String overrideData) {
- }
- @UpdateSingleCache(namespace = "Bravo", expiration = 300)
- @ReturnDataUpdateContent
- public String updateTimestampValue(@ParameterValueKeyProvider final Long key) {
- try {
- Thread.sleep(100);
- } catch (InterruptedException ex) {
- }
- final Long now = new Date().getTime();
- final String result = now.toString() + "-U-" + key.toString();
- return result;
- }
ReadThroughAssignCache
作用:讀取Cache中資料,如果不存在,則将讀取的資料存入Cache
key生成規則: ReadThroughAssignCache 注解中的 assignedKey 字段指定
[java]
view plain
copy
print
?
- @ReadThroughAssignCache(assignedKey = "SomePhatKey", namespace = "Echo", expiration = 3000)
- public List<String> getAssignStrings() {
- try {
- Thread.sleep(500);
- } catch (InterruptedException ex) {
- }
- final List<String> results = new ArrayList<String>();
- final long extra = System.currentTimeMillis() % 20;
- final String base = System.currentTimeMillis() + "";
- for (int ix = 0; ix < 20 + extra; ix++) {
- results.add(ix + "-" + base);
- }
- return results;
- }
InvalidateAssignCache
作用:失效緩存中指定key的資料
key生成規則:assignedKey 字段指定
@InvalidateAssignCache(assignedKey = "SomePhatKey", namespace = "Echo")
public void invalidateAssignStrings() {
}
UpdateAssignCache
作用:更新指定緩存
key生成規則:assignedKey 字段指定
@UpdateAssignCache(assignedKey = "SomePhatKey", namespace = "Echo", expiration = 3000)
public void updateAssignStrings(int bubpkus, @ParameterDataUpdateContent final List<String> newData) {
}