Search in sources :

Example 6 with Cache

use of org.apache.dubbo.cache.Cache in project dubbo by alibaba.

the class CacheFilter method invoke.

/**
 * If cache is configured, dubbo will invoke method on each method call. If cache value is returned by cache store
 * then it will return otherwise call the remote method and return value. If remote method's return value has error
 * then it will not cache the value.
 * @param invoker    service
 * @param invocation invocation.
 * @return Cache returned value if found by the underlying cache store. If cache miss it will call target method.
 * @throws RpcException
 */
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), CACHE_KEY))) {
        Cache cache = cacheFactory.getCache(invoker.getUrl(), invocation);
        if (cache != null) {
            String key = StringUtils.toArgumentString(invocation.getArguments());
            Object value = cache.get(key);
            if (value != null) {
                if (value instanceof ValueWrapper) {
                    return AsyncRpcResult.newDefaultAsyncResult(((ValueWrapper) value).get(), invocation);
                } else {
                    return AsyncRpcResult.newDefaultAsyncResult(value, invocation);
                }
            }
            Result result = invoker.invoke(invocation);
            if (!result.hasException()) {
                cache.put(key, new ValueWrapper(result.getValue()));
            }
            return result;
        }
    }
    return invoker.invoke(invocation);
}
Also used : Cache(org.apache.dubbo.cache.Cache) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result)

Example 7 with Cache

use of org.apache.dubbo.cache.Cache in project dubbo by alibaba.

the class JCacheFactoryTest method testJCacheFactory.

@Test
public void testJCacheFactory() throws Exception {
    Cache cache = super.constructCache();
    assertThat(cache instanceof JCache, is(true));
}
Also used : Cache(org.apache.dubbo.cache.Cache) Test(org.junit.jupiter.api.Test) AbstractCacheFactoryTest(org.apache.dubbo.cache.support.AbstractCacheFactoryTest)

Example 8 with Cache

use of org.apache.dubbo.cache.Cache in project dubbo by alibaba.

the class ExpiringCacheFactoryTest method testExpiringCacheUnExpired.

@Test
public void testExpiringCacheUnExpired() throws Exception {
    URL url = URL.valueOf("test://test:12/test?cache=expiring&cache.seconds=0&cache.interval=1");
    AbstractCacheFactory cacheFactory = getCacheFactory();
    Invocation invocation = new RpcInvocation();
    Cache cache = cacheFactory.getCache(url, invocation);
    cache.put("testKey", "testValue");
    Thread.sleep(1100);
    assertNotNull(cache.get("testKey"));
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AbstractCacheFactory(org.apache.dubbo.cache.support.AbstractCacheFactory) URL(org.apache.dubbo.common.URL) Cache(org.apache.dubbo.cache.Cache) Test(org.junit.jupiter.api.Test) AbstractCacheFactoryTest(org.apache.dubbo.cache.support.AbstractCacheFactoryTest)

Example 9 with Cache

use of org.apache.dubbo.cache.Cache in project dubbo by alibaba.

the class ExpiringCacheFactoryTest method testExpiringCacheExpired.

@Test
public void testExpiringCacheExpired() throws Exception {
    Cache cache = constructCache();
    assertThat(cache instanceof ExpiringCache, is(true));
    // 500ms
    TimeUnit.MILLISECONDS.sleep(500);
    cache.put("testKey", "testValue");
    // 1000ms ExpireThread clear all expire cache
    TimeUnit.MILLISECONDS.sleep(500);
    // 1700ms  get should be null
    TimeUnit.MILLISECONDS.sleep(700);
    assertNull(cache.get("testKey"));
}
Also used : Cache(org.apache.dubbo.cache.Cache) Test(org.junit.jupiter.api.Test) AbstractCacheFactoryTest(org.apache.dubbo.cache.support.AbstractCacheFactoryTest)

Example 10 with Cache

use of org.apache.dubbo.cache.Cache in project dubbo by alibaba.

the class ExpiringCacheFactoryTest method testExpiringCacheFactory.

@Test
public void testExpiringCacheFactory() throws Exception {
    Cache cache = super.constructCache();
    assertThat(cache instanceof ExpiringCache, is(true));
}
Also used : Cache(org.apache.dubbo.cache.Cache) Test(org.junit.jupiter.api.Test) AbstractCacheFactoryTest(org.apache.dubbo.cache.support.AbstractCacheFactoryTest)

Aggregations

Cache (org.apache.dubbo.cache.Cache)12 Test (org.junit.jupiter.api.Test)10 AbstractCacheFactoryTest (org.apache.dubbo.cache.support.AbstractCacheFactoryTest)9 URL (org.apache.dubbo.common.URL)4 Invocation (org.apache.dubbo.rpc.Invocation)4 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)4 AbstractCacheFactory (org.apache.dubbo.cache.support.AbstractCacheFactory)3 HashMap (java.util.HashMap)1 CacheFactory (org.apache.dubbo.cache.CacheFactory)1 ThreadLocalCache (org.apache.dubbo.cache.support.threadlocal.ThreadLocalCache)1 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)1 Result (org.apache.dubbo.rpc.Result)1