Search in sources :

Example 1 with Cache

use of org.apache.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, "org.apache.dubbo.config.cache.CacheService", parameters);
    Invocation invocation = new RpcInvocation("findCache", CacheService.class.getName(), "", new Class[] { String.class }, new String[] { "0" }, null, null, null);
    Cache cache = cacheFactory.getCache(url, invocation);
    assertTrue(cache instanceof ThreadLocalCache);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) Invocation(org.apache.dubbo.rpc.Invocation) RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) HashMap(java.util.HashMap) CacheFactory(org.apache.dubbo.cache.CacheFactory) URL(org.apache.dubbo.common.URL) Cache(org.apache.dubbo.cache.Cache) ThreadLocalCache(org.apache.dubbo.cache.support.threadlocal.ThreadLocalCache) ThreadLocalCache(org.apache.dubbo.cache.support.threadlocal.ThreadLocalCache) Test(org.junit.jupiter.api.Test)

Example 2 with Cache

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

the class JCacheFactoryTest method testJCacheGetExpired.

@Test
public void testJCacheGetExpired() throws Exception {
    URL url = URL.valueOf("test://test:12/test?cache=jacache&cache.write.expire=1");
    AbstractCacheFactory cacheFactory = getCacheFactory();
    Invocation invocation = new RpcInvocation();
    Cache cache = cacheFactory.getCache(url, invocation);
    cache.put("testKey", "testValue");
    Thread.sleep(10);
    assertNull(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 3 with Cache

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

the class ThreadLocalCacheFactoryTest method testThreadLocalCacheFactory.

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

Example 4 with Cache

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

the class ExpiringCacheFactoryTest method testExpiringCache.

@Test
public void testExpiringCache() throws Exception {
    Cache cache = constructCache();
    assertThat(cache instanceof ExpiringCache, is(true));
    // 500ms
    TimeUnit.MILLISECONDS.sleep(500);
    cache.put("testKey", "testValue");
    // 800ms
    TimeUnit.MILLISECONDS.sleep(300);
    assertNotNull(cache.get("testKey"));
    // 1300ms
    TimeUnit.MILLISECONDS.sleep(500);
    assertNotNull(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 5 with Cache

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

the class AbstractCacheFactory method getCache.

/**
 * Takes URL and invocation instance and return cache instance for a given url.
 *
 * @param url        url of the method
 * @param invocation invocation context.
 * @return Instance of cache store used as storage for caching return values.
 */
@Override
public Cache getCache(URL url, Invocation invocation) {
    url = url.addParameter(METHOD_KEY, invocation.getMethodName());
    String key = url.toFullString();
    Cache cache = caches.get(key);
    // get from cache first.
    if (null != cache) {
        return cache;
    }
    synchronized (MONITOR) {
        // double check.
        cache = caches.get(key);
        if (null != cache) {
            return cache;
        }
        cache = createCache(url);
        caches.put(key, cache);
    }
    return cache;
}
Also used : Cache(org.apache.dubbo.cache.Cache)

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