Class MemoryCacher

All Implemented Interfaces:
Runnable, MetricConstants, MoleculerLifecycle

public class MemoryCacher extends Cacher implements Runnable
On-heap memory cache. MemoryCacher is the fastest cache implementation in Moleculer. This can be a "distributed" cache, the content of the other node's cache is removable via events; the developer can send distributed events that erase the cache on the other nodes - but this is not automatic! Supports global and entry-level TTL configuration. Configuration properties:
  • capacity: Maximum capacity per partition (must be a power of 2), defaults to 2048
  • ttl: Expire time of entries in memory, in seconds (default: 0 = never expires)
  • cleanup: Cleanup period, in seconds
Performance (small and large data): 5.5 million gets / second (per thread / core)

See Also:
  • Field Details

    • capacity

      protected int capacity
      Maximum number of entries per partition
    • ttl

      protected int ttl
      Expire time, in SECONDS (0 = never expires)
    • cleanup

      protected int cleanup
      Cleanup period time, in SECONDS (0 = disable cleanup process)
    • useCloning

      protected boolean useCloning
      Do you need to make a copy of the returned values? Cloning the values is much safer, but little bit slower. If the services work with common objects, they can modify the cached object. If you turn off cloning, the cache will be faster, but you need to be careful NOT TO CHANGE the values from the cache!
    • readLock

      protected final ReentrantReadWriteLock.ReadLock readLock
    • writeLock

      protected final ReentrantReadWriteLock.WriteLock writeLock
    • partitions

      protected final HashMap<String, MemoryCacher.MemoryPartition> partitions
    • timer

      protected volatile ScheduledFuture<?> timer
      Cancelable timer
    • timerStarted

      protected AtomicBoolean timerStarted
    • timerStopped

      protected AtomicBoolean timerStopped
    • counterExpired

      protected MetricCounter counterExpired
    • counterGet

      protected MetricCounter counterGet
    • counterSet

      protected MetricCounter counterSet
    • counterDel

      protected MetricCounter counterDel
    • counterClean

      protected MetricCounter counterClean
    • counterFound

      protected MetricCounter counterFound
  • Constructor Details

    • MemoryCacher

      public MemoryCacher()
    • MemoryCacher

      public MemoryCacher(int capacityPerPartition, int defaultTtl)
    • MemoryCacher

      public MemoryCacher(int capacityPerPartition, int defaultTtl, int cleanupSeconds)
  • 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
    • startTimer

      protected void startTimer(int entryTTL)
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • 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 MemoryCacher.MemoryPartition 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
    • 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.
    • getCapacity

      public int getCapacity()
    • setCapacity

      public void setCapacity(int capacity)
    • getTtl

      public int getTtl()
    • setTtl

      public void setTtl(int ttl)
    • getCleanup

      public int getCleanup()
    • setCleanup

      public void setCleanup(int cleanup)
    • isUseCloning

      public boolean isUseCloning()
    • setUseCloning

      public void setUseCloning(boolean useCloning)