use of com.alibaba.dubbo.cache.Cache in project dubbo by alibaba.
the class CacheTest method testCacheFactory.
@Test
public void testCacheFactory() {
URL url = URL.valueOf("test://test:11/test?cache=jacache&.cache.write.expire=1");
CacheFactory cacheFactory = new MyCacheFactory();
Invocation invocation = new NullInvocation();
Cache cache = cacheFactory.getCache(url, invocation);
cache.put("testKey", "testValue");
org.apache.dubbo.cache.CacheFactory factory = cacheFactory;
org.apache.dubbo.common.URL u = org.apache.dubbo.common.URL.valueOf("test://test:11/test?cache=jacache&.cache.write.expire=1");
org.apache.dubbo.rpc.Invocation inv = new RpcInvocation();
org.apache.dubbo.cache.Cache c = factory.getCache(u, inv);
String v = (String) c.get("testKey");
Assertions.assertEquals("testValue", v);
}
use of com.alibaba.dubbo.cache.Cache in project dubbo by alibaba.
the class AbstractCacheFactory method getCache.
@Override
public Cache getCache(URL url, Invocation invocation) {
url = url.addParameter(METHOD_KEY, invocation.getMethodName());
String key = url.toFullString();
Cache cache = caches.get(key);
if (cache == null) {
caches.put(key, createCache(url));
cache = caches.get(key);
}
return cache;
}
use of com.alibaba.dubbo.cache.Cache in project dubbo by alibaba.
the class AbstractCacheFactory method getCache.
public Cache getCache(URL url) {
String key = url.toFullString();
Cache cache = caches.get(key);
if (cache == null) {
caches.put(key, createCache(url));
cache = caches.get(key);
}
return cache;
}
use of com.alibaba.dubbo.cache.Cache in project dubbo by alibaba.
the class CacheTest method testCacheProvider.
@Test
public void testCacheProvider() throws Exception {
CacheFactory cacheFactory = ExtensionLoader.getExtensionLoader(CacheFactory.class).getAdaptiveExtension();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("findCache.cache", "threadlocal");
URL url = new URL("dubbo", "127.0.0.1", 29582, "com.alibaba.dubbo.config.cache.CacheService", parameters);
Invocation invocation = new RpcInvocation("findCache", new Class[] { String.class }, new String[] { "0" }, null, null);
Cache cache = cacheFactory.getCache(url, invocation);
assertTrue(cache instanceof ThreadLocalCache);
}
use of com.alibaba.dubbo.cache.Cache in project dubbo by alibaba.
the class CacheFilter method invoke.
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.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) {
return new RpcResult(value);
}
Result result = invoker.invoke(invocation);
if (!result.hasException()) {
cache.put(key, result.getValue());
}
return result;
}
}
return invoker.invoke(invocation);
}
Aggregations