Class XCacheUtils


  • public class XCacheUtils
    extends Object
    This helper class for the implementation of a Cache.
    Since:
    8.1.0
    Author:
    XIMA MEDIA GmbH
    • Constructor Detail

      • XCacheUtils

        public XCacheUtils()
    • Method Detail

      • createCache

        public static <K,​V> org.ehcache.config.CacheConfiguration<K,​V> createCache​(Class<K> keyType,
                                                                                               Class<V> valueType,
                                                                                               long onHeapSize,
                                                                                               long offHeapSize,
                                                                                               long diskSize,
                                                                                               long timeToExpire)
        Creates a cache configuration with specified parameters for key and value types, sizes for on-heap, off-heap, and disk storage, as well as an expiration time. This method allows for the creation of a customized cache configuration using Ehcache, specifying how much memory to use in different tiers (heap, off-heap, disk) and setting an expiration time for elements in the cache.
        Type Parameters:
        K - the type of keys maintained by the cache
        V - the type of values to be stored in the cache
        Parameters:
        keyType - the Class object representing the type of keys, not null
        valueType - the Class object representing the type of values, not null
        onHeapSize - the maximum number of entries to store in the heap
        offHeapSize - the maximum amount of memory to use for the off-heap
        diskSize - the maximum amount of disk space to use for the disk
        timeToExpire - the time after which the cache entries will expire
        Returns:
        a CacheConfiguration instance configured with the specified parameters
      • update

        public static <K,​V> V update​(org.ehcache.Cache<K,​V> cache,
                                           K key,
                                           Supplier<V> supplier,
                                           UnaryOperator<V> updater)
        Updates an entry within the specified cache. This method first checks if the cache contains an entry for the provided key. If an entry exists, it retrieves the value; otherwise, it obtans a new value by invoking the provided supplier. The value is then passed to the given updater function, which should return the new value. If the updater function returns null the entry for the given key is removed from the cache.
        Type Parameters:
        K - the type of keys by the cache
        V - the type of cached values
        Parameters:
        cache - the cache to update
        key - the key whose value is to be updated
        supplier - a Supplier that provides a new value when the key does not exist in the cache
        updater - a UnaryOperator that takes the current or supplied value and returns the updated value. If the result is null, the entry is removed.
        Returns:
        the updated value