Interface CacheFactory
- All Known Subinterfaces:
CacheManager
This factory and its builders hide many of the implementation details of any cache. The only real control you have is whether it is a local (JVM-local) or shared cache. All of the following maybe be different in some execution environments, so you should not assume any of them in your code:
- Size and expiry settings (e.g. maxsize, expire times) may be overridden by admin or deployment settings.
- When you builder.build() a cache for the first time in your code, the cache may already have items in it. For example, you are in a cluster. If your plugin is reloaded, or even if a node is restarted, shared caches will already have data in them.
- For shared caches, when a new version of your code/plugin is deployed, it may already exist with data in it from a previous version of your code/plugin.
- The keys and values in your non-local cache are effectively persistent from the point of view of your code/plugin. Therefore, you should consider backward compatibility of data, or invalidate caches on start/upgrade.
- See Also:
-
Method Summary
Modifier and TypeMethodDescription<K,V> Cache <K, V> Returns the cache with the given name, creates it if necessary.<K,V> Cache <K, V> Returns the cache with the given name, creates it if necessary.<K,V> Cache <K, V> getCache(String name, CacheLoader<K, V> loader) Returns the cache with the given name and loader, creates it if necessary.<K,V> Cache <K, V> getCache(String name, CacheLoader<K, V> loader, CacheSettings required) Returns the cache with the given name, loader and required cache settings, creates it if necessary.<K,V> Cache <K, V> Deprecated.since 2.0, use getCache(name) instead<V> CachedReference<V> getCachedReference(Class<?> owningClass, String name, Supplier<V> supplier) Returns a Cached Reference, creating it if necessary.<V> CachedReference<V> getCachedReference(Class<?> owningClass, String name, Supplier<V> supplier, CacheSettings required) Returns a Cached Reference, creating it if necessary.<V> CachedReference<V> getCachedReference(String name, Supplier<V> supplier) Returns a Cached Reference, creating it if necessary.<V> CachedReference<V> getCachedReference(String name, Supplier<V> supplier, CacheSettings required) Returns a Cached Reference, creating it if necessary.default <K,V> ReadThroughCache <K, V> getReadThroughCache(String name) Returns theReadThroughCachewith the given name, creates it if necessary.default <K,V> ReadThroughCache <K, V> getReadThroughCache(String name, CacheSettings required) Returns theReadThroughCachewith the given name, creates it if necessary.
-
Method Details
-
getCachedReference
@Nonnull <V> CachedReference<V> getCachedReference(@Nonnull String name, @Nonnull Supplier<V> supplier) Returns a Cached Reference, creating it if necessary.- Parameters:
name- the name of the Cached Referencesupplier- the supplier for value to be cached, called if the value needs to be generated- Returns:
- the Cached Reference
-
getCachedReference
@Nonnull <V> CachedReference<V> getCachedReference(@Nonnull String name, @Nonnull Supplier<V> supplier, @Nonnull CacheSettings required) Returns a Cached Reference, creating it if necessary.- Parameters:
name- the name of the Cached Referencesupplier- the supplier for value to be cached, called if the value needs to be generatedrequired- specifies the required cache settings- Returns:
- the Cached Reference
-
getCachedReference
@Nonnull <V> CachedReference<V> getCachedReference(@Nonnull Class<?> owningClass, @Nonnull String name, @Nonnull Supplier<V> supplier) Returns a Cached Reference, creating it if necessary.It is equivalent to calling
getCachedReference(String, Supplier, CacheSettings)- Parameters:
owningClass- the owning classname- the name of the cache spaced within theowningClasssupplier- the supplier for value to be cached, called if the value needs to be generated- Returns:
- the Cached Reference
-
getCachedReference
@Nonnull <V> CachedReference<V> getCachedReference(@Nonnull Class<?> owningClass, @Nonnull String name, @Nonnull Supplier<V> supplier, @Nonnull CacheSettings required) Returns a Cached Reference, creating it if necessary.It is equivalent to calling
getCachedReference(String, Supplier, CacheSettings)- Parameters:
owningClass- the owning classname- the name of the cache spaced within theowningClasssupplier- the supplier for value to be cached, called if the value needs to be generatedrequired- specifies the required cache settings- Returns:
- the Cached Reference
-
getCache
Returns the cache with the given name, creates it if necessary. This is equivalent to callinggetCache(String, CacheLoader)withnameandnull.- Parameters:
name- the name of the cache- Returns:
- a Cache
-
getCache
Returns the cache with the given name, creates it if necessary.It is equivalent to calling
getCache(String)with the computed name.- Parameters:
owningClass- the owning classname- the name of the cache spaced within theowningClass- Returns:
- a Cache
-
getCache
Returns the cache with the given name and loader, creates it if necessary. This is equivalent to callinggetCache(String, CacheLoader, CacheSettings)withname,nullandnew CacheSettingsBuilder().build().- Parameters:
name- the name of the cacheloader- the loader that will be used to provide values for keys that will be added to the cache.nullindicates no loader- Returns:
- a Cache
-
getCache
@Nonnull <K,V> Cache<K,V> getCache(@Nonnull String name, @Nullable CacheLoader<K, V> loader, @Nonnull CacheSettings required) Returns the cache with the given name, loader and required cache settings, creates it if necessary.- Parameters:
name- the name of the cacheloader- the loader that will be used to provide values for keys that will be added to the cache.nullindicates no loaderrequired- the cache settings that are required to be set if the cache is created.- Returns:
- a Cache
-
getReadThroughCache
Returns theReadThroughCachewith the given name, creates it if necessary.- Parameters:
name- the name of the cache- Returns:
- a
ReadThroughCache - Since:
- 6.1
-
getReadThroughCache
default <K,V> ReadThroughCache<K,V> getReadThroughCache(@Nonnull String name, @Nonnull CacheSettings required) Returns theReadThroughCachewith the given name, creates it if necessary.- Parameters:
name- the name of the cache- Returns:
- a
ReadThroughCache - Since:
- 6.1
-
getCache
@Deprecated @Nonnull <K,V> Cache<K,V> getCache(@Nonnull String name, @Nonnull Class<K> keyType, @Nonnull Class<V> valueType) Deprecated.since 2.0, use getCache(name) insteadReturns a cache with the given name, and the types specified. Creates it if necessary. If two caches of the same name are queried, with different types, the call with incorrect type arguments will throw a ClassCastException. For example with:Cache<String,String> firstCache = cacheManager.getCache("myCache", String.class, String.class);
Cache<String,Long> secondCache = cacheManager.getCache("myCache", String.class, Long.class);the second call to getCache will result in a ClassCastException.
- Parameters:
name- the name of the cachekeyType- the type of keys in the cache. Must extend SerializablevalueType- the type of values in the cache. Must extend Serializable- Returns:
- a Cache
-