缓存扩展

扩展说明

用请求参数作为 key,缓存返回结果。

扩展接口

org.apache.dubbo.cache.CacheFactory

扩展配置

  1. <dubbo:service cache="lru" />
  2. <!-- 方法级缓存 -->
  3. <dubbo:service><dubbo:method cache="lru" /></dubbo:service>
  4. <!-- 缺省值设置,当<dubbo:service>没有配置cache属性时,使用此配置 -->
  5. <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 项目结构:

  1. src
  2. |-main
  3. |-java
  4. |-com
  5. |-xxx
  6. |-XxxCacheFactory.java (实现CacheFactory接口)
  7. |-resources
  8. |-META-INF
  9. |-dubbo
  10. |-org.apache.dubbo.cache.CacheFactory (纯文本文件,内容为:xxx=com.xxx.XxxCacheFactory)

XxxCacheFactory.java:

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

XxxCache.java:

  1. package com.xxx;
  2. import org.apache.dubbo.cache.Cache;
  3. public class XxxCache implements Cache {
  4. public Cache(URL url, String name) {
  5. // ...
  6. }
  7. public void put(Object key, Object value) {
  8. // ...
  9. }
  10. public Object get(Object key) {
  11. // ...
  12. }
  13. }

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

  1. xxx=com.xxx.XxxCacheFactory