Package org.cache2k

Class CacheManager

  • All Implemented Interfaces:
    AutoCloseable

    public abstract class CacheManager
    extends Object
    implements AutoCloseable
    A cache manager holds a set of caches. Caches within a cache manager share the same class loader and may have a different default configuration. If a cache manager is not specified a default manager is used.

    Usually there is one cache manager per application which is retrieved by getInstance(). Multiple cache managers can be used to separate different caching aspects. This may help with a better organization of the cache configuration and operate with reasonable defaults within each aspect.

    Cache managers are identified by a unique name. If no name is specified the name "default" is used. The default name in use may be changed, see setDefaultName(String).

    Author:
    Jens Wilke
    • Constructor Detail

      • CacheManager

        public CacheManager()
    • Method Detail

      • getDefaultName

        public static String getDefaultName()
        Name of the default cache manager, which is ""default"" by default.
      • setDefaultName

        public static void setDefaultName​(String managerName)
        Change the default manager name. The method can only be called once early in application startup, before the default manager instance is requested.

        It is also possible to set a different default manager name via the XML configuration.

        The allowed characters in a manager name are identical to the characters in a cache name, this is documented at Cache2kBuilder.name(String)

        See Also:
        Cache2kBuilder.name(String)
      • getInstance

        public static CacheManager getInstance()
        Get the default cache manager for the default class loader. The default class loader is the class loader used to load the cache2k implementation classes.

        The name of default cache manager is ""default"". This may be changed, by setDefaultName(String).

      • getInstance

        public static CacheManager getInstance​(ClassLoader cl)
        Get the default cache manager for the specified class loader.
      • getInstance

        public static CacheManager getInstance​(String managerName)
        Retrieve a cache manager with the specified name. If not existing, a manager with that name is created. The default class loader is used. The default class loader is the class loader used to load the cache2k implementation classes.

        The allowed characters in a manager name are identical to the characters in a cache name, this is documented at Cache2kBuilder.name(String)

        See Also:
        Cache2kBuilder.name(String)
      • getInstance

        public static CacheManager getInstance​(ClassLoader cl,
                                               String managerName)
        Retrieve a cache manager with the specified name using the specified classloader. If not existing, a manager with that name is created. Different cache managers are created for different class loaders. Manager names should be unique within one VM instance.

        The allowed characters in a manager name are identical to the characters in a cache name, this is documented at Cache2kBuilder.name(String)

        See Also:
        Cache2kBuilder.name(String)
      • closeAll

        public static void closeAll()
        Close all cache managers.
      • closeAll

        public static void closeAll​(ClassLoader cl)
        Close all cache manager associated with this class loader.
      • close

        public static void close​(ClassLoader cl,
                                 String name)
        Close the named cache manager.
      • isDefaultManager

        public abstract boolean isDefaultManager()
        True if this is the default manager of the application, returned by getInstance()
      • getName

        public abstract String getName()
        The name to uniquely identify the manager within a VM instance.
        See Also:
        getInstance(String)
      • getConfiguredCacheNames

        public abstract Iterable<String> getConfiguredCacheNames()
        Returns a list of caches that are found in the XML based configuration.
      • createCache

        public abstract <K,​V> Cache<K,​V> createCache​(Cache2kConfig<K,​V> cfg)
        Create a new cache from the configuration. The recommended way is to use the Cache2kBuilder to create a new cache. This method is identical to and a shorthand to:
        
            CacheManager manager = ...
            Cache2kConfiguration<K, V> config = ...
            Cache<K, V> cache = Cache2kBuilder.of(config).manager(manager).build();
         
      • clear

        public abstract void clear()
        Clear all currently active caches in this cache manager
      • close

        public abstract void close()
        Free all resources from managed caches. Same as calling all Cache.close() methods. A closed manager cannot be used to create new caches. A new CacheManager with the same name may be requested via getInstance(String). Multiple calls to close have no effect.
        Specified by:
        close in interface AutoCloseable
      • isClosed

        public abstract boolean isClosed()
        Returns true if this cache manager was closed.
        See Also:
        close()
      • getProperties

        public abstract Properties getProperties()
        Properties for the cache manager, never null. By default, the properties are empty. Cache clients may store arbitrary information.
      • getClassLoader

        public abstract ClassLoader getClassLoader()
        Class loader this manager is using to load additional classes, resources or configuration.