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);
}
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));
}
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"));
}
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"));
}
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));
}
Aggregations