Class JCacheXSpringCache
- java.lang.Object
-
- org.springframework.cache.support.AbstractValueAdaptingCache
-
- io.github.dhruv1110.jcachex.spring.core.JCacheXSpringCache
-
- All Implemented Interfaces:
org.springframework.cache.Cache
public class JCacheXSpringCache extends org.springframework.cache.support.AbstractValueAdaptingCache
Spring Cache adapter that bridges JCacheX with Spring's caching abstraction. This adapter allows JCacheX caches to be used seamlessly with Spring's declarative caching support (@Cacheable, @CacheEvict, etc.) while providing access to all JCacheX features and maintaining compatibility with Spring's Cache interface.Key Features:
- Full JCacheX Integration: Direct access to underlying JCacheX cache
- Spring Compatibility: Works with Spring's caching annotations
- Null Value Support: Configurable handling of null values
- Type Safety: Proper handling of generic types
- Thread Safety: Fully thread-safe for concurrent access
Usage Examples:
{ @code // Direct usage (usually not needed) JCacheXSpringCache springCache = new JCacheXSpringCache("users", jcacheXCache, true); // Access via cache manager @Service public class UserService {
- Since:
- 1.0.0
- See Also:
Cache
,Cache
,JCacheXCacheManager
-
-
Constructor Summary
Constructors Constructor Description JCacheXSpringCache(String name, io.github.dhruv1110.jcachex.Cache<Object,Object> nativeCache)
Creates a new JCacheXSpringCache instance with null values allowed.JCacheXSpringCache(String name, io.github.dhruv1110.jcachex.Cache<Object,Object> nativeCache, boolean allowNullValues)
Creates a new JCacheXSpringCache instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
boolean
containsKey(Object key)
Checks if the cache contains a value for the specified key.void
evict(Object key)
void
evictBeforeInvocation(Object key)
Evicts a key from the cache, ensuring it happens before method execution.boolean
evictIfPresent(Object key)
<T> T
get(Object key, Class<T> type)
Gets a value from the cache with the specified type.<T> T
get(Object key, Callable<T> valueLoader)
io.github.dhruv1110.jcachex.Cache<Object,Object>
getJCacheXCache()
Gets the underlying JCacheX cache instance.String
getName()
Object
getNativeCache()
io.github.dhruv1110.jcachex.CacheStats
getStats()
Gets cache statistics if available.protected Object
lookup(Object key)
void
put(Object key, Object value)
org.springframework.cache.Cache.ValueWrapper
putIfAbsent(Object key, Object value)
long
size()
Gets the size of the cache.String
toString()
-
Methods inherited from class org.springframework.cache.support.AbstractValueAdaptingCache
fromStoreValue, get, isAllowNullValues, toStoreValue, toValueWrapper
-
-
-
-
Constructor Detail
-
JCacheXSpringCache
public JCacheXSpringCache(String name, io.github.dhruv1110.jcachex.Cache<Object,Object> nativeCache, boolean allowNullValues)
Creates a new JCacheXSpringCache instance.- Parameters:
name
- the cache namenativeCache
- the underlying JCacheX cacheallowNullValues
- whether to allow null values
-
-
Method Detail
-
getName
public String getName()
-
getNativeCache
public Object getNativeCache()
-
lookup
protected Object lookup(Object key)
- Specified by:
lookup
in classorg.springframework.cache.support.AbstractValueAdaptingCache
-
putIfAbsent
@Nullable public org.springframework.cache.Cache.ValueWrapper putIfAbsent(Object key, @Nullable Object value)
-
evict
public void evict(Object key)
-
evictBeforeInvocation
public void evictBeforeInvocation(Object key)
Evicts a key from the cache, ensuring it happens before method execution. This is used by @CacheEvict with beforeInvocation=true.- Parameters:
key
- the key to evict
-
evictIfPresent
public boolean evictIfPresent(Object key)
-
clear
public void clear()
-
get
@Nullable public <T> T get(Object key, @Nullable Class<T> type)
Gets a value from the cache with the specified type. This method provides type-safe access to cached values, automatically casting to the expected type.- Specified by:
get
in interfaceorg.springframework.cache.Cache
- Overrides:
get
in classorg.springframework.cache.support.AbstractValueAdaptingCache
- Type Parameters:
T
- the value type- Parameters:
key
- the cache keytype
- the expected value type- Returns:
- the cached value, or null if not found
- Throws:
ClassCastException
- if the cached value cannot be cast to the expected type
-
containsKey
public boolean containsKey(Object key)
Checks if the cache contains a value for the specified key.- Parameters:
key
- the cache key- Returns:
- true if the cache contains a value for the key, false otherwise
-
size
public long size()
Gets the size of the cache.- Returns:
- the number of entries in the cache
-
getStats
@Nullable public io.github.dhruv1110.jcachex.CacheStats getStats()
Gets cache statistics if available.- Returns:
- cache statistics, or null if statistics are not enabled or not supported
-
getJCacheXCache
public io.github.dhruv1110.jcachex.Cache<Object,Object> getJCacheXCache()
Gets the underlying JCacheX cache instance. This provides direct access to JCacheX-specific features like async operations, detailed statistics, and advanced configuration options.- Returns:
- the underlying JCacheX cache
-
-