Class JCacheCacher

All Implemented Interfaces:
MetricConstants, MoleculerLifecycle

public class JCacheCacher extends DistributedCacher
JSR-107 JCache is a standardized caching API that is Java 6 compatible and introduced in JEE 8. WARNING! Core JCache API does NOT support entry-level TTL parameter! If you need this feature use RedisCacher or MemoryCacher. JCache is implemented by various caching solutions:
  • Apache Ignite
  • Hazelcast
  • Oracle Coherence
  • Couchbase (https://github.com/couchbaselabs/couchbase-java-cache)
  • Terracotta Ehcache
  • Infinispan
  • Blazing Cache
  • Cache2k
  • Caffeine
  • WebSphere eXtreme Scale
Required dependency:

// https://mvnrepository.com/artifact/javax.cache/cache-api
compile group: 'javax.cache', name: 'cache-api', version: '1.1.1'

See Also:
  • Field Details

    • partitions

      protected final HashMap<String, javax.cache.Cache<String,byte[]>> partitions
    • cacheManager

      protected javax.cache.CacheManager cacheManager
    • serializer

      protected Serializer serializer
    • closeEmptyPartitions

      protected boolean closeEmptyPartitions
      Close empty caches
    • defaultConfiguration

      protected javax.cache.configuration.Configuration<String,byte[]> defaultConfiguration
      Default cache cconfiguration
    • cacheConfigurations

      protected Map<String, javax.cache.configuration.Configuration<String,byte[]>> cacheConfigurations
      Optional cache configurations by cache regions
    • readLock

      protected final ReentrantReadWriteLock.ReadLock readLock
    • writeLock

      protected final ReentrantReadWriteLock.WriteLock writeLock
    • counterGet

      protected MetricCounter counterGet
    • counterSet

      protected MetricCounter counterSet
    • counterDel

      protected MetricCounter counterDel
    • counterClean

      protected MetricCounter counterClean
    • counterFound

      protected MetricCounter counterFound
  • Constructor Details

    • JCacheCacher

      public JCacheCacher()
    • JCacheCacher

      public JCacheCacher(URI uri)
    • JCacheCacher

      public JCacheCacher(javax.cache.CacheManager cacheManager)
  • Method Details

    • started

      public void started(ServiceBroker broker) throws Exception
      Initializes cacher instance.
      Specified by:
      started in interface MoleculerLifecycle
      Overrides:
      started in class Cacher
      Parameters:
      broker - parent ServiceBroker
      Throws:
      Exception
    • stopped

      public void stopped()
      Specified by:
      stopped in interface MoleculerLifecycle
      Overrides:
      stopped in class MoleculerComponent
    • get

      public io.datatree.Promise get(String key)
      Description copied from class: Cacher
      Gets a cached content by a key.
      Specified by:
      get in class Cacher
      Parameters:
      key - cache key
      Returns:
      Promise with cached value (or null, the returned Promise also can be null)
    • getPartition

      protected javax.cache.Cache<String,byte[]> getPartition(String prefix)
    • set

      public io.datatree.Promise set(String key, io.datatree.Tree value, int ttl)
      Description copied from class: Cacher
      Sets a content by key into the cache.
      Specified by:
      set in class Cacher
      Parameters:
      key - cache key
      value - new value
      ttl - optional TTL of entry (0 == use default TTL)
      Returns:
      Promise with empty value
    • del

      public io.datatree.Promise del(String key)
      Description copied from class: Cacher
      Deletes a content from this cache.
      Specified by:
      del in class Cacher
      Parameters:
      key - cache key
      Returns:
      Promise with empty value
    • clean

      public io.datatree.Promise clean(String match)
      Description copied from class: Cacher
      Cleans this cache. Removes every key by a match string. The default match string is "**".
      Specified by:
      clean in class Cacher
      Parameters:
      match - regex
      Returns:
      Promise with empty value
    • clean

      protected static final long clean(javax.cache.Cache<String,byte[]> partition, String match) throws Exception
      Throws:
      Exception
    • partitionPosition

      protected int partitionPosition(String key, boolean throwErrorIfMissing)
    • getCacheKeys

      public io.datatree.Promise getCacheKeys()
      Lists all keys of cached entries.
      Specified by:
      getCacheKeys in class Cacher
      Returns:
      a Tree object with a "keys" array.
    • addCacheConfiguration

      public void addCacheConfiguration(String partition, javax.cache.configuration.Configuration<String,byte[]> configuration)
    • removeCacheConfiguration

      public void removeCacheConfiguration(String partition)
    • getCacheManager

      public javax.cache.CacheManager getCacheManager()
    • setCacheManager

      public void setCacheManager(javax.cache.CacheManager cacheManager)
    • isCloseEmptyPartitions

      public boolean isCloseEmptyPartitions()
    • setCloseEmptyPartitions

      public void setCloseEmptyPartitions(boolean closeEmptyCaches)
    • getDefaultConfiguration

      public javax.cache.configuration.Configuration<String,byte[]> getDefaultConfiguration()
    • setDefaultConfiguration

      public void setDefaultConfiguration(javax.cache.configuration.Configuration<String,byte[]> defaultConfiguration)
    • getCacheConfigurations

      public Map<String, javax.cache.configuration.Configuration<String,byte[]>> getCacheConfigurations()
    • setCacheConfigurations

      public void setCacheConfigurations(Map<String, javax.cache.configuration.Configuration<String,byte[]>> cacheConfigurations)