+ improve cache implementation by adding a primitive monitor mechanism (backed by Redis)

This commit is contained in:
Costin Leau
2011-10-08 22:17:21 +03:00
parent 1e6220f131
commit e018f55ec4
4 changed files with 109 additions and 18 deletions

View File

@@ -32,7 +32,7 @@ import org.springframework.cache.Cache;
public abstract class AbstractNativeCacheTest<T> {
private T nativeCache;
private Cache cache;
protected Cache cache;
protected final static String CACHE_NAME = "testCache";
@Before

View File

@@ -16,10 +16,15 @@
package org.springframework.data.redis.cache;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@@ -76,4 +81,46 @@ public class RedisCacheTest extends AbstractNativeCacheTest<RedisTemplate> {
protected Object getObject() {
return objFactory.instance();
}
@Test
public void testConcurrentRead() throws Exception {
final Object key1 = getObject();
final Object value1 = getObject();
final Object key2 = getObject();
final Object value2 = getObject();
cache.put(key1, value1);
cache.put(key2, value2);
final Object monitor = new Object();
Thread th = new Thread(new Runnable() {
@Override
public void run() {
synchronized (monitor) {
monitor.notify();
}
cache.clear();
cache.put(value1, key1);
cache.put(value2, key2);
}
}, "concurrent-cache-access");
th.run();
synchronized (monitor) {
monitor.wait(TimeUnit.MILLISECONDS.convert(1, TimeUnit.SECONDS));
}
final Object key3 = getObject();
final Object value3 = getObject();
cache.put(key3, value3);
cache.put(value3, key3);
assertNull(cache.get(key1));
assertNull(cache.get(key2));
assertEquals(key1, cache.get(value1).get());
}
}

View File

@@ -136,11 +136,15 @@ public abstract class CollectionTestParams {
jsonPersonTemplateRJC.afterPropertiesSet();
return Arrays.asList(new Object[][] { { stringFactory, stringTemplate }, { stringFactory, stringTemplateRJC },
{ personFactory, personTemplateRJC }, { stringFactory, stringTemplateJR },
{ personFactory, personTemplateJR }, { personFactory, personTemplate },
{ personFactory, personTemplateRJC },
//{ stringFactory, stringTemplateJR },
//{ personFactory, personTemplateJR },
{ personFactory, personTemplate },
{ stringFactory, xstreamStringTemplate }, { personFactory, xstreamPersonTemplate },
{ stringFactory, xstreamStringTemplateJR }, { personFactory, xstreamPersonTemplateJR },
{ personFactory, jsonPersonTemplate }, { personFactory, jsonPersonTemplateJR },
//{ stringFactory, xstreamStringTemplateJR },
//{ personFactory, xstreamPersonTemplateJR },
{ personFactory, jsonPersonTemplate },
//{ personFactory, jsonPersonTemplateJR },
{ stringFactory, xstreamStringTemplateRJC }, { personFactory, xstreamPersonTemplateRJC },
{ personFactory, jsonPersonTemplateRJC } });
}