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:

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

Annotation Configuration:

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

Method Granularity

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

Annotation Configuration:

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

Server-Side Caching

Interface Granularity

XML Configuration:

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

Annotation Configuration:

@DubboService(cache = "lru")
public class DemoServiceImpl implements DemoService {

    private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
    @Override
    public String sayHello(String name) {
        logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name;

    }

}

Method Granularity

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

Annotation Configuration:

@DubboService(methods = {@Method(name="sayHello",cache = "lru")})
public class DemoServiceImpl implements DemoService {

    private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);
    @Override
    public String sayHello(String name) {
        logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name;

    }

}
Last modified September 30, 2024: Translate (22d3d83a3b)