Skip to content

Commit

Permalink
[Dubbo-#1362] cache provider always lru cache (#1396)
Browse files Browse the repository at this point in the history
* #1362修复

#1362修复

* #1362修复

#1362修复

* #1362修复

#1362修复

* 修改bug

修改bug

* 修改bug

修改bug

* 添加cache的测试用例

添加cache的测试用例
  • Loading branch information
maxiaoguang64 authored and beiwei30 committed Mar 5, 2018
1 parent 3d4f2fc commit d770d92
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,28 @@
*/
package com.alibaba.dubbo.config.cache;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;

import com.alibaba.dubbo.cache.Cache;
import com.alibaba.dubbo.cache.CacheFactory;
import com.alibaba.dubbo.cache.support.threadlocal.ThreadLocalCache;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.RpcInvocation;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* CacheTest
*/
public class CacheTest extends TestCase {

@Test
public void testCache() throws Exception {
private void testCache(String type) throws Exception {
ServiceConfig<CacheService> service = new ServiceConfig<CacheService>();
service.setApplication(new ApplicationConfig("cache-provider"));
service.setRegistry(new RegistryConfig("N/A"));
Expand All @@ -45,6 +50,12 @@ public void testCache() throws Exception {
reference.setApplication(new ApplicationConfig("cache-consumer"));
reference.setInterface(CacheService.class);
reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&cache=true");

MethodConfig method = new MethodConfig();
method.setName("findCache");
method.setCache(type);
reference.setMethods(Arrays.asList(method));

CacheService cacheService = reference.get();
try {
// verify cache, same result is returned for multiple invocations (in fact, the return value increases
Expand All @@ -57,19 +68,21 @@ public void testCache() throws Exception {
Thread.sleep(100);
}

// default cache.size is 1000 for LRU, should have cache expired if invoke more than 1001 times
for (int n = 0; n < 1001; n++) {
String pre = null;
for (int i = 0; i < 10; i++) {
String result = cacheService.findCache(String.valueOf(n));
Assert.assertTrue(pre == null || pre.equals(result));
pre = result;
if ("lru".equals(type)) {
// default cache.size is 1000 for LRU, should have cache expired if invoke more than 1001 times
for (int n = 0; n < 1001; n++) {
String pre = null;
for (int i = 0; i < 10; i++) {
String result = cacheService.findCache(String.valueOf(n));
Assert.assertTrue(pre == null || pre.equals(result));
pre = result;
}
}
}

// verify if the first cache item is expired in LRU cache
String result = cacheService.findCache("0");
Assert.assertFalse(fix == null || fix.equals(result));
// verify if the first cache item is expired in LRU cache
String result = cacheService.findCache("0");
Assert.assertFalse(fix == null || fix.equals(result));
}
} finally {
reference.destroy();
}
Expand All @@ -78,4 +91,24 @@ public void testCache() throws Exception {
}
}

@Test
public void testCache() throws Exception {
testCache("lru");
testCache("threadlocal");
}

@Test
public void testCacheProvider() throws Exception {
CacheFactory 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);
Assert.assertTrue(cache instanceof ThreadLocalCache);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.Adaptive;
import com.alibaba.dubbo.common.extension.SPI;
import com.alibaba.dubbo.rpc.Invocation;

/**
* CacheFactory
Expand All @@ -27,6 +28,6 @@
public interface CacheFactory {

@Adaptive("cache")
Cache getCache(URL url);
Cache getCache(URL url, Invocation invocation);

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void setCacheFactory(CacheFactory cacheFactory) {

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().addParameter(Constants.METHOD_KEY, invocation.getMethodName()));
Cache cache = cacheFactory.getCache(invoker.getUrl(), invocation);
if (cache != null) {
String key = StringUtils.toArgumentString(invocation.getArguments());
Object value = cache.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import com.alibaba.dubbo.cache.Cache;
import com.alibaba.dubbo.cache.CacheFactory;
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.rpc.Invocation;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -30,7 +32,8 @@ public abstract class AbstractCacheFactory implements CacheFactory {

private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();

public Cache getCache(URL url) {
public Cache getCache(URL url, Invocation invocation) {
url = url.addParameter(Constants.METHOD_KEY, invocation.getMethodName());
String key = url.toFullString();
Cache cache = caches.get(key);
if (cache == null) {
Expand Down

0 comments on commit d770d92

Please sign in to comment.