Class MemoryCache

java.lang.Object
org.pipservices3.components.cache.MemoryCache
All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable, org.pipservices3.commons.config.IReconfigurable, org.pipservices3.commons.run.ICleanable, ICache

public class MemoryCache
extends Object
implements ICache, org.pipservices3.commons.config.IReconfigurable, org.pipservices3.commons.run.ICleanable
Cache that stores values in the process memory.

Remember: This implementation is not suitable for synchronization of distributed processes.

### Configuration parameters ###

  • options:
    • timeout: default caching timeout in milliseconds (default: 1 minute)
    • max_size: maximum number of values stored in this cache (default: 1000)

### Example ###

 
 MemoryCache cache = new MemoryCache();
 
 cache.store("123", "key1", "ABC", 0);
 
 
See Also:
ICache
  • Constructor Details

    • MemoryCache

      public MemoryCache()
      Creates instance of local in-memory cache component
  • Method Details

    • configure

      public void configure​(org.pipservices3.commons.config.ConfigParams config) throws org.pipservices3.commons.errors.ConfigException
      Configures component by passing configuration parameters.
      Specified by:
      configure in interface org.pipservices3.commons.config.IConfigurable
      Parameters:
      config - configuration parameters to be set.
      Throws:
      org.pipservices3.commons.errors.ConfigException - when component is in illegal state or configuration validation fails.
    • retrieve

      public Object retrieve​(String correlationId, String key)
      Retrieves cached value from the cache using its key. If value is missing in the cache or expired it returns null.
      Specified by:
      retrieve in interface ICache
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      key - a unique value key.
      Returns:
      a cached value or null if value wasn't found or timeout expired.
    • store

      public Object store​(String correlationId, String key, Object value, long timeout)
      Stores value in the cache with expiration time.
      Specified by:
      store in interface ICache
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      key - a unique value key.
      value - a value to store.
      timeout - expiration timeout in milliseconds.
      Returns:
      a cached value stored in the cache.
    • remove

      public void remove​(String correlationId, String key)
      Removes a value from the cache by its key.
      Specified by:
      remove in interface ICache
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      key - a unique value key.
    • clear

      public void clear​(String correlationId)
      Clears component state.
      Specified by:
      clear in interface org.pipservices3.commons.run.ICleanable
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.