Skip to content

Commit

Permalink
Showing 3 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -47,13 +47,18 @@ public RefreshCache(Cache cache) {
super(cache);
}

@Override
public void close() {
protected void stopRefresh() {
List<RefreshTask> tasks = new ArrayList<>();
tasks.addAll(taskMap.values());
tasks.forEach(task -> task.cancel());
}

@Override
public void close() {
stopRefresh();
super.close();
}


private boolean hasLoader() {
return config.getLoader() != null;
Original file line number Diff line number Diff line change
@@ -147,4 +147,9 @@ public CacheResult REMOVE_ALL(Set<? extends K> keys) {
public CacheResult PUT_IF_ABSENT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
return cache.PUT_IF_ABSENT(key, value, expireAfterWrite, timeUnit);
}

@Override
public void close() {
cache.close();
}
}
Original file line number Diff line number Diff line change
@@ -34,10 +34,12 @@ public static void refreshCacheTest(Cache cache, long refresh, long stopRefreshA
RefreshPolicy policy = RefreshPolicy.newPolicy(refresh, TimeUnit.MILLISECONDS);
cache.config().setRefreshPolicy(policy);
refreshCacheTest1(cache);
getRefreshCache(cache).stopRefresh();

count.set(0);
cache.config().getRefreshPolicy().setStopRefreshAfterLastAccessMillis(stopRefreshAfterLastAccess);
refreshCacheTest2(cache);
getRefreshCache(cache).stopRefresh();

cache.config().setLoader(oldLoader);
cache.config().setRefreshPolicy(oldPolicy);
@@ -48,11 +50,25 @@ public static void refreshCacheTest(AbstractCacheBuilder builder, long refresh,
builder.loader((key) -> key + "_V" + count.getAndIncrement());
RefreshPolicy policy = RefreshPolicy.newPolicy(refresh, TimeUnit.MILLISECONDS);
builder.refreshPolicy(policy);
refreshCacheTest1(builder.buildCache());
Cache cache = builder.buildCache();
refreshCacheTest1(cache);
cache.close();

count.set(0);
builder.getConfig().getRefreshPolicy().setStopRefreshAfterLastAccessMillis(stopRefreshAfterLastAccess);
refreshCacheTest2(builder.buildCache());
cache = builder.buildCache();
refreshCacheTest2(cache);
cache.close();
}

private static RefreshCache getRefreshCache(Cache cache){
Cache c = cache;
while (!(c instanceof RefreshCache)) {
if (c instanceof ProxyCache) {
c = ((ProxyCache) c).getTargetCache();
}
}
return (RefreshCache) c;
}

private static void refreshCacheTest1(Cache cache) throws Exception {
@@ -86,13 +102,8 @@ private static void refreshCacheTest1(Cache cache) throws Exception {
Assert.assertEquals(2, monitor.getCacheStat().getPutCount());

Thread.sleep((long) (1.5 * refreshMillis));
Cache c = cache;
while (!(c instanceof RefreshCache)) {
if (c instanceof ProxyCache) {
c = ((ProxyCache) c).getTargetCache();
}
}
boolean external = ((RefreshCache) c).concreteCache() instanceof AbstractExternalCache;

boolean external = getRefreshCache(cache).concreteCache() instanceof AbstractExternalCache;

Assert.assertEquals(4, monitor.getCacheStat().getLoadCount());
Assert.assertNotEquals("refreshCacheTest1_K1_V0", cache.get("refreshCacheTest1_K1"));
@@ -125,7 +136,6 @@ private static void refreshCacheTest1(Cache cache) throws Exception {
}

cache.config().getMonitors().remove(monitor);
cache.close();
}

private static void refreshCacheTest2(Cache cache) throws Exception {
@@ -135,6 +145,7 @@ private static void refreshCacheTest2(Cache cache) throws Exception {
long stopRefresh = cache.config().getRefreshPolicy().getStopRefreshAfterLastAccessMillis();

Assert.assertEquals("refreshCacheTest2_K1_V0", cache.get("refreshCacheTest2_K1"));
long key1StartRefreshTime = System.currentTimeMillis();
Assert.assertEquals(1, monitor.getCacheStat().getGetCount());
Assert.assertEquals(0, monitor.getCacheStat().getGetHitCount());
Assert.assertEquals(1, monitor.getCacheStat().getGetMissCount());
@@ -147,12 +158,11 @@ private static void refreshCacheTest2(Cache cache) throws Exception {
Assert.assertEquals(2, monitor.getCacheStat().getLoadCount());
Assert.assertEquals(2, monitor.getCacheStat().getPutCount());

long beginTime = System.currentTimeMillis();
while (true) {
long sleepTime = stopRefresh / 5;
Thread.sleep(sleepTime);
cache.get("refreshCacheTest2_K1");
long totalSpendTime = System.currentTimeMillis() - beginTime;
long totalSpendTime = System.currentTimeMillis() - key1StartRefreshTime;
if (totalSpendTime > 1.4 * refreshMillis) {
break;
}
@@ -174,6 +184,5 @@ private static void refreshCacheTest2(Cache cache) throws Exception {
Assert.assertEquals(3, monitor.getCacheStat().getLoadCount());

cache.config().getMonitors().remove(monitor);
cache.close();
}
}

0 comments on commit e346c1b

Please sign in to comment.