Class Cacher

All Implemented Interfaces:
MetricConstants, MoleculerLifecycle
Direct Known Subclasses:
DistributedCacher, MemoryCacher

public abstract class Cacher extends Middleware implements MetricConstants
Base superclass of all Cacher implementations.
See Also:
  • Field Details

    • metrics

      protected Metrics metrics
    • debug

      protected boolean debug
  • Constructor Details

    • Cacher

      public Cacher()
  • Method Details

    • started

      public void started(ServiceBroker broker) throws Exception
      Specified by:
      started in interface MoleculerLifecycle
      Overrides:
      started in class MoleculerComponent
      Throws:
      Exception
    • install

      public Action install(Action action, io.datatree.Tree config)
      Specified by:
      install in class Middleware
    • getCacheKey

      public String getCacheKey(String name, io.datatree.Tree params, String... keys)
      Creates a cacher-specific key by name and params. Concatenates the name and params.
      Parameters:
      name - qualified name of the action
      params - input (key) structure (~JSON)
      keys - optional array of keys (eg. "id")
      Returns:
      generated cache key
    • serializeKey

      protected void serializeKey(StringBuilder key, io.datatree.Tree params, String... keys)
    • appendToKey

      protected void appendToKey(StringBuilder key, io.datatree.Tree tree)
    • appendTree

      protected void appendTree(StringBuilder key, Object source)
    • isCachingDisabledByTheContext

      protected boolean isCachingDisabledByTheContext(Context ctx)
      Is the request disables caching?
      Parameters:
      ctx - Input Context
      Returns:
      true, if the caching is disabled at the request level
    • logClean

      protected void logClean(String cacheName, String match, long count)
    • get

      public abstract io.datatree.Promise get(String key)
      Gets a cached content by a key.
      Parameters:
      key - cache key
      Returns:
      Promise with cached value (or null, the returned Promise also can be null)
    • set

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

      public abstract io.datatree.Promise del(String key)
      Deletes a content from this cache.
      Parameters:
      key - cache key
      Returns:
      Promise with empty value
    • clean

      public abstract io.datatree.Promise clean(String match)
      Cleans this cache. Removes every key by a match string. The default match string is "**".
      Parameters:
      match - regex
      Returns:
      Promise with empty value
    • getCacheKeys

      public abstract io.datatree.Promise getCacheKeys()
      Lists all keys of cached entries.
      Returns:
      a Tree object with a "keys" array.
    • isDebug

      public boolean isDebug()
      Is debug enabled?
      Returns:
      debug flag
    • setDebug

      public void setDebug(boolean debug)
      Enable debug messages.
      Parameters:
      debug - debug mode