Class CacheConfig<K,​V>

  • Type Parameters:
    K - the type of keys maintained by this cache
    V - the type of mapped values

    public class CacheConfig<K,​V>
    extends Object
    Configuration options for a JCacheX cache instance.

    This class provides a comprehensive set of configuration options to customize cache behavior including size limits, expiration policies, eviction strategies, and performance tuning.

    Basic Usage Examples:

    
     // Simple cache with size limit
     CacheConfig<String, String> config = CacheConfig.<String, String>builder()
             .maximumSize(1000L)
             .build();
    
     // Cache with time-based expiration
     CacheConfig<String, User> userConfig = CacheConfig.<String, User>builder()
             .maximumSize(500L)
             .expireAfterWrite(Duration.ofMinutes(15))
             .expireAfterAccess(Duration.ofMinutes(5))
             .build();
    
     // Cache with custom loader and statistics
     CacheConfig<String, String> loadingConfig = CacheConfig.<String, String>builder()
             .maximumSize(200L)
             .loader(key -> loadFromDatabase(key))
             .recordStats(true)
             .build();
     

    Advanced Configuration Examples:

    
     // Cache with custom eviction strategy and listeners
     CacheConfig<String, LargeObject> advancedConfig = CacheConfig.<String, LargeObject>builder()
             .maximumWeight(10_000L)
             .weigher((key, value) -> value.getSize())
             .evictionStrategy(new LRUEvictionStrategy<>())
             .addListener(new CacheEventListener<String, LargeObject>() { @Override
                 public void onEvict(String key, LargeObject value, EvictionReason reason) {
                     System.out.println("Evicted: " + key + " due to " + reason);
                 }
                 // ... other methods
             })
             .build();
    
     // Async loading cache with refresh
     CacheConfig<String, String> asyncConfig = CacheConfig.<String, String>builder()
             .maximumSize(1000L)
             .asyncLoader(key -> CompletableFuture.supplyAsync(() -> loadAsync(key)))
             .refreshAfterWrite(Duration.ofMinutes(10))
             .build();
     
    Since:
    1.0.0
    See Also:
    DefaultCache, CacheEventListener, EvictionStrategy
    • Method Detail

      • getMaximumSize

        public Long getMaximumSize()
        Returns the maximum number of entries the cache may contain.
        Returns:
        the maximum size, or null if no limit is set
      • getMaximumWeight

        public Long getMaximumWeight()
        Returns the maximum total weight of entries the cache may contain.
        Returns:
        the maximum weight, or null if no limit is set
      • getWeigher

        public BiFunction<K,​V,​Long> getWeigher()
        Returns the weigher function used to calculate entry weights.
        Returns:
        the weigher function, or null if not set
      • getExpireAfterWrite

        public Duration getExpireAfterWrite()
        Returns the duration after which an entry should be automatically removed from the cache once the duration has elapsed after the entry's creation or last update.
        Returns:
        the expiration duration, or null if not set
      • getExpireAfterAccess

        public Duration getExpireAfterAccess()
        Returns the duration after which an entry should be automatically removed from the cache once the duration has elapsed after the entry's creation, last update, or last access.
        Returns:
        the expiration duration, or null if not set
      • getEvictionStrategy

        public EvictionStrategy<K,​V> getEvictionStrategy()
        Returns the eviction strategy used to determine which entries to remove when the cache size or weight limits are exceeded.
        Returns:
        the eviction strategy, or null if not set (will use LRU by default)
      • isWeakKeys

        public boolean isWeakKeys()
        Returns whether cache keys should be stored using weak references.
        Returns:
        true if weak keys are enabled, false otherwise
      • isWeakValues

        public boolean isWeakValues()
        Returns whether cache values should be stored using weak references.
        Returns:
        true if weak values are enabled, false otherwise
      • isSoftValues

        public boolean isSoftValues()
        Returns whether cache values should be stored using soft references.
        Returns:
        true if soft values are enabled, false otherwise
      • getLoader

        public Function<K,​V> getLoader()
        Returns the function used to load values synchronously when not present in cache.
        Returns:
        the loader function, or null if not set
      • getAsyncLoader

        public Function<K,​CompletableFuture<V>> getAsyncLoader()
        Returns the function used to load values asynchronously when not present in cache.
        Returns:
        the async loader function, or null if not set
      • getRefreshAfterWrite

        public Duration getRefreshAfterWrite()
        Returns the duration after which an entry should be automatically refreshed once the duration has elapsed after the entry's creation or last update.
        Returns:
        the refresh duration, or null if not set
      • isRecordStats

        public boolean isRecordStats()
        Returns whether statistics should be recorded for this cache.
        Returns:
        true if statistics recording is enabled, false otherwise
      • getInitialCapacity

        public int getInitialCapacity()
        Returns the initial capacity of the cache's internal data structure.
        Returns:
        the initial capacity
      • getConcurrencyLevel

        public int getConcurrencyLevel()
        Returns the concurrency level for the cache's internal data structure.
        Returns:
        the concurrency level
      • getDirectory

        public String getDirectory()
        Returns the directory path for persistent cache storage.
        Returns:
        the directory path, or null if not set
      • getListeners

        public Set<CacheEventListener<K,​V>> getListeners()
        Returns a copy of the set of event listeners registered for this cache.
        Returns:
        the event listeners
      • newBuilder

        public static <K,​V> CacheConfig.Builder<K,​V> newBuilder()
        Creates a new builder instance for constructing cache configurations.
        Type Parameters:
        K - the type of keys maintained by the cache
        V - the type of mapped values
        Returns:
        a new builder instance
      • builder

        public static <K,​V> CacheConfig.Builder<K,​V> builder()
        Creates a new builder instance for constructing cache configurations.
        Type Parameters:
        K - the type of keys maintained by the cache
        V - the type of mapped values
        Returns:
        a new builder instance