Class 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 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 name
        nativeCache - the underlying JCacheX cache
        allowNullValues - whether to allow null values
      • JCacheXSpringCache

        public JCacheXSpringCache​(String name,
                                  io.github.dhruv1110.jcachex.Cache<Object,​Object> nativeCache)
        Creates a new JCacheXSpringCache instance with null values allowed.
        Parameters:
        name - the cache name
        nativeCache - the underlying JCacheX cache
    • Method Detail

      • getName

        public String getName()
      • getNativeCache

        public Object getNativeCache()
      • lookup

        protected Object lookup​(Object key)
        Specified by:
        lookup in class org.springframework.cache.support.AbstractValueAdaptingCache
      • get

        @Nullable
        public <T> T get​(Object key,
                         Callable<T> valueLoader)
      • put

        public void put​(Object key,
                        @Nullable
                        Object value)
      • 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 interface org.springframework.cache.Cache
        Overrides:
        get in class org.springframework.cache.support.AbstractValueAdaptingCache
        Type Parameters:
        T - the value type
        Parameters:
        key - the cache key
        type - 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