Search in sources :

Example 1 with Cache

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);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) URL(com.alibaba.dubbo.common.URL) CacheFactory(com.alibaba.dubbo.cache.CacheFactory) Cache(com.alibaba.dubbo.cache.Cache) Test(org.junit.jupiter.api.Test)

Example 2 with Cache

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;
}
Also used : Cache(com.alibaba.dubbo.cache.Cache)

Example 3 with 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;
}
Also used : Cache(com.alibaba.dubbo.cache.Cache)

Example 4 with 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);
}
Also used : RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) RpcInvocation(com.alibaba.dubbo.rpc.RpcInvocation) Invocation(com.alibaba.dubbo.rpc.Invocation) HashMap(java.util.HashMap) CacheFactory(com.alibaba.dubbo.cache.CacheFactory) URL(com.alibaba.dubbo.common.URL) ThreadLocalCache(com.alibaba.dubbo.cache.support.threadlocal.ThreadLocalCache) Cache(com.alibaba.dubbo.cache.Cache) ThreadLocalCache(com.alibaba.dubbo.cache.support.threadlocal.ThreadLocalCache) Test(org.junit.Test)

Example 5 with Cache

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);
}
Also used : RpcResult(com.alibaba.dubbo.rpc.RpcResult) Cache(com.alibaba.dubbo.cache.Cache) Result(com.alibaba.dubbo.rpc.Result) RpcResult(com.alibaba.dubbo.rpc.RpcResult)

Aggregations

Cache (com.alibaba.dubbo.cache.Cache)5 CacheFactory (com.alibaba.dubbo.cache.CacheFactory)2 URL (com.alibaba.dubbo.common.URL)2 Invocation (com.alibaba.dubbo.rpc.Invocation)2 ThreadLocalCache (com.alibaba.dubbo.cache.support.threadlocal.ThreadLocalCache)1 Result (com.alibaba.dubbo.rpc.Result)1 RpcInvocation (com.alibaba.dubbo.rpc.RpcInvocation)1 RpcResult (com.alibaba.dubbo.rpc.RpcResult)1 HashMap (java.util.HashMap)1 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1