Result Cache Invocation

Accelerate access speed through result caching

Function Description

Dubbo supports server-side and client-side result caching.

Cache Types

Currently, versions 3.0 and above of Dubbo support the following built-in caching strategies:

  • lru removes excess cache based on the least recently used principle, keeping the hottest data cached.
  • lfu implements cache strategy based on the principle of evicting the least frequently used.
  • expiring implements cache strategy based on expiration time.
  • threadlocal caches in the current thread, for example in a page render when many portals are needed to query user information, using thread cache can reduce such unnecessary accesses.
  • jcache integrates with JSR107, bridging various cache implementations.

Cache types are extensible Cache Extension

About Example Code

Usage Scenarios

Result caching is used to accelerate access speed to hot data. Dubbo provides declarative caching to reduce users’ workload in adding caching.

Usage

Client-Side Caching

In Dubbo, RPC call result caching supports configuration control at both the interface and method granularity.

Interface Granularity

XML Configuration:

  1. <dubbo:reference interface="com.foo.DemoService" cache="lru" />

Annotation Configuration:

  1. @DubboReference(cache = "lru")
  2. private DemoService demoService;

Method Granularity

  1. <dubbo:reference interface="com.foo.DemoService">
  2. <dubbo:method name="sayHello" cache="lru" />
  3. </dubbo:reference>

Annotation Configuration:

  1. @DubboReference(methods = {@Method(name="sayHello",cache = "lru")})
  2. private DemoService demoService;

Server-Side Caching

Interface Granularity

XML Configuration:

  1. <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
  2. <dubbo:service interface="com.foo.DemoService" ref="demoService" cache="lru" />

Annotation Configuration:

  1. @DubboService(cache = "lru")
  2. public class DemoServiceImpl implements DemoService {
  3. private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
  4. @Override
  5. public String sayHello(String name) {
  6. logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
  7. return "Hello " + name;
  8. }
  9. }

Method Granularity

  1. <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
  2. <dubbo:service interface="com.foo.DemoService" ref="demoService" cache="lru" />
  3. <dubbo:method name="sayHello" cache="lru" />
  4. </dubbo:service>

Annotation Configuration:

  1. @DubboService(methods = {@Method(name="sayHello",cache = "lru")})
  2. public class DemoServiceImpl implements DemoService {
  3. private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
  4. @Override
  5. public String sayHello(String name) {
  6. logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
  7. return "Hello " + name;
  8. }
  9. }

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)