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