Skip to content

Commit e69ae8d

Browse files
committed
- drop CustomCache in tests and use TestCache
- check null cacheable issue with defaultcache - support both ctors in custom cache classes regarding to value of cacheconfig.cacheable
1 parent 74a4185 commit e69ae8d

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

src/main/java/redis/clients/jedis/csc/CacheFactory.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,33 @@ public final class CacheFactory {
1010

1111
public static Cache getCache(CacheConfig config) {
1212
if (config.getCacheClass() == null) {
13+
if (config.getCacheable() == null) {
14+
throw new JedisCacheException("Cacheable is required to create the default cache!");
15+
}
1316
return new DefaultCache(config.getMaxSize(), config.getCacheable(), getEvictionPolicy(config));
1417
}
1518
return instantiateCustomCache(config);
1619
}
1720

18-
private static Cache instantiateCustomCache(CacheConfig config) {
21+
private static Cache instantiateCustomCache(CacheConfig config) {
1922
try {
20-
Constructor ctorWithCacheable = findConstructorWithCacheable(config.getCacheClass());
21-
if (ctorWithCacheable != null) {
22-
return (Cache) ctorWithCacheable.newInstance(config.getMaxSize(), getEvictionPolicy(config), config.getCacheable());
23+
if (config.getCacheable() != null) {
24+
Constructor ctorWithCacheable = findConstructorWithCacheable(config.getCacheClass());
25+
if (ctorWithCacheable != null) {
26+
return (Cache) ctorWithCacheable.newInstance(config.getMaxSize(), getEvictionPolicy(config), config.getCacheable());
27+
}
2328
}
2429
Constructor ctor = getConstructor(config.getCacheClass());
2530
return (Cache) ctor.newInstance(config.getMaxSize(), getEvictionPolicy(config));
26-
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
31+
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
32+
| SecurityException e) {
2733
throw new JedisCacheException("Failed to insantiate custom cache type!", e);
2834
}
2935
}
3036

3137
private static Constructor findConstructorWithCacheable(Class customCacheType) {
3238
return Arrays.stream(customCacheType.getConstructors())
33-
.filter(
34-
ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { int.class, EvictionPolicy.class, Cacheable.class }))
39+
.filter(ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { int.class, EvictionPolicy.class, Cacheable.class }))
3540
.findFirst().orElse(null);
3641
}
3742

src/test/java/redis/clients/jedis/csc/ClientSideCacheFunctionalityTest.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ public void testNullValue() throws InterruptedException {
512512
String nonExisting = "non-existing-key";
513513
control.del(nonExisting);
514514

515-
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().maxSize(MAX_SIZE).build())) {
515+
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().maxSize(MAX_SIZE).build())) {
516516
Cache cache = jedis.getCache();
517517
CacheStats stats = cache.getStats();
518518

@@ -539,16 +539,10 @@ public void testNullValue() throws InterruptedException {
539539
}
540540
}
541541

542-
public static class CustomCache extends TestCache {
543-
public CustomCache(int maximumSize, EvictionPolicy evictionPolicy) {
544-
super(maximumSize, evictionPolicy, DefaultCacheable.INSTANCE);
545-
}
546-
}
547-
548542
@Test
549543
public void testCacheFactory() throws InterruptedException {
550544
// this checks the instantiation with parameters (int, EvictionPolicy, Cacheable)
551-
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().cacheClass(TestCache.class).build())) {
545+
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), CacheConfig.builder().cacheClass(TestCache.class).build())) {
552546
Cache cache = jedis.getCache();
553547
CacheStats stats = cache.getStats();
554548

@@ -562,7 +556,8 @@ public void testCacheFactory() throws InterruptedException {
562556
}
563557

564558
// this checks the instantiation with parameters (int, EvictionPolicy)
565-
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(), new CacheConfig().builder().cacheClass(CustomCache.class).build())) {
559+
try (JedisPooled jedis = new JedisPooled(hnp, clientConfig.get(),
560+
CacheConfig.builder().cacheClass(TestCache.class).cacheable(null).build())) {
566561
Cache cache = jedis.getCache();
567562
CacheStats stats = cache.getStats();
568563

@@ -575,5 +570,4 @@ public void testCacheFactory() throws InterruptedException {
575570
assertEquals(1, stats.getMissCount());
576571
}
577572
}
578-
579573
}

src/test/java/redis/clients/jedis/csc/TestCache.java

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public TestCache(Map<CacheKey, CacheEntry> map, Cacheable cacheable) {
1717
super(10000, map, cacheable, new LRUEviction(10000));
1818
}
1919

20+
public TestCache(int maximumSize, EvictionPolicy evictionPolicy ) {
21+
super(maximumSize, new HashMap<CacheKey, CacheEntry>(), DefaultCacheable.INSTANCE, evictionPolicy);
22+
}
23+
2024
public TestCache(int maximumSize, EvictionPolicy evictionPolicy, Cacheable cacheable ) {
2125
super(maximumSize, new HashMap<CacheKey, CacheEntry>(), cacheable, evictionPolicy);
2226
}

src/test/java/redis/clients/jedis/csc/UnifiedJedisClientSideCacheTestBase.java

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public void simple() {
4545

4646
@Test
4747
public void simpleWithSimpleMap() {
48-
// HashMap<CacheKey, CacheEntry> map = new HashMap<>();
4948
try (UnifiedJedis jedis = createCachedJedis(CacheConfig.builder().build())) {
5049
Cache cache = jedis.getCache();
5150
control.set("foo", "bar");

0 commit comments

Comments
 (0)