Dubbo3 緩存拓展

2022-04-22 11:54 更新

擴展說明

用請求參數(shù)作為 key,緩存返回結(jié)果。

擴展接口

org.apache.dubbo.cache.CacheFactory

擴展配置

<dubbo:service cache="lru" />
<!-- 方法級緩存 -->
<dubbo:service><dubbo:method cache="lru" /></dubbo:service> 
<!-- 缺省值設(shè)置,當(dāng)<dubbo:service>沒有配置cache屬性時,使用此配置 -->
<dubbo:provider cache="xxx,yyy" /> 

已知擴展

  • org.apache.dubbo.cache.support.lru.LruCacheFactory
  • org.apache.dubbo.cache.support.threadlocal.ThreadLocalCacheFactory
  • org.apache.dubbo.cache.support.jcache.JCacheFactory

擴展示例

Maven 項目結(jié)構(gòu):

src
 |-main
    |-java
        |-com
            |-xxx
                |-XxxCacheFactory.java (實現(xiàn)CacheFactory接口)
    |-resources
        |-META-INF
            |-dubbo
                |-org.apache.dubbo.cache.CacheFactory (純文本文件,內(nèi)容為:xxx=com.xxx.XxxCacheFactory)

XxxCacheFactory.java:

package com.xxx;
 
import org.apache.dubbo.cache.CacheFactory;
 
public class XxxCacheFactory implements CacheFactory {
    public Cache getCache(URL url, String name) {
        return new XxxCache(url, name);
    }
}

XxxCache.java:

package com.xxx;
 
import org.apache.dubbo.cache.Cache;
 
public class XxxCache implements Cache {
    public Cache(URL url, String name) {
        // ...
    }
    public void put(Object key, Object value) {
        // ...
    }
    public Object get(Object key) {
        // ...
    }
}

META-INF/dubbo/org.apache.dubbo.cache.CacheFactory:

xxx=com.xxx.XxxCacheFactory


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號