-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Cache class to CacheConfig (#3942)
* adding cacheclass to cacheconfig * - add cachefactory test * - revert connection ctors to public - udpate some tests with UnifiedJedis.getCache - add ping to flaky tests * remove unnecessary anonymous types * change ctor access modifiers * fix test name * make cachefactory methods static * removing pings due to still flaky with inv messages * - 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 * remove unncessary maxsize * - remove inline anonymious
- Loading branch information
Showing
15 changed files
with
233 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package redis.clients.jedis.csc; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Arrays; | ||
|
||
import redis.clients.jedis.exceptions.JedisCacheException; | ||
|
||
public final class CacheFactory { | ||
|
||
public static Cache getCache(CacheConfig config) { | ||
if (config.getCacheClass() == null) { | ||
if (config.getCacheable() == null) { | ||
throw new JedisCacheException("Cacheable is required to create the default cache!"); | ||
} | ||
return new DefaultCache(config.getMaxSize(), config.getCacheable(), getEvictionPolicy(config)); | ||
} | ||
return instantiateCustomCache(config); | ||
} | ||
|
||
private static Cache instantiateCustomCache(CacheConfig config) { | ||
try { | ||
if (config.getCacheable() != null) { | ||
Constructor ctorWithCacheable = findConstructorWithCacheable(config.getCacheClass()); | ||
if (ctorWithCacheable != null) { | ||
return (Cache) ctorWithCacheable.newInstance(config.getMaxSize(), getEvictionPolicy(config), config.getCacheable()); | ||
} | ||
} | ||
Constructor ctor = getConstructor(config.getCacheClass()); | ||
return (Cache) ctor.newInstance(config.getMaxSize(), getEvictionPolicy(config)); | ||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | ||
| SecurityException e) { | ||
throw new JedisCacheException("Failed to insantiate custom cache type!", e); | ||
} | ||
} | ||
|
||
private static Constructor findConstructorWithCacheable(Class customCacheType) { | ||
return Arrays.stream(customCacheType.getConstructors()) | ||
.filter(ctor -> Arrays.equals(ctor.getParameterTypes(), new Class[] { int.class, EvictionPolicy.class, Cacheable.class })) | ||
.findFirst().orElse(null); | ||
} | ||
|
||
private static Constructor getConstructor(Class customCacheType) { | ||
try { | ||
return customCacheType.getConstructor(int.class, EvictionPolicy.class); | ||
} catch (NoSuchMethodException e) { | ||
String className = customCacheType.getName(); | ||
throw new JedisCacheException(String.format( | ||
"Failed to find compatible constructor for custom cache type! Provide one of these;" | ||
// give hints about the compatible constructors | ||
+ "\n - %s(int maxSize, EvictionPolicy evictionPolicy)\n - %s(int maxSize, EvictionPolicy evictionPolicy, Cacheable cacheable)", | ||
className, className), e); | ||
} | ||
} | ||
|
||
private static EvictionPolicy getEvictionPolicy(CacheConfig config) { | ||
if (config.getEvictionPolicy() == null) { | ||
// It will be default to LRUEviction, until we have other eviction implementations | ||
return new LRUEviction(config.getMaxSize()); | ||
} | ||
return config.getEvictionPolicy(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.