Class MemoryCacher
java.lang.Object
services.moleculer.service.MoleculerComponent
services.moleculer.service.Middleware
services.moleculer.cacher.Cacher
services.moleculer.cacher.MemoryCacher
- All Implemented Interfaces:
Runnable, MetricConstants, MoleculerLifecycle
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
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classprotected static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intMaximum number of entries per partitionprotected intCleanup period time, in SECONDS (0 = disable cleanup process)protected MetricCounterprotected MetricCounterprotected MetricCounterprotected MetricCounterprotected MetricCounterprotected MetricCounterprotected final HashMap<String, MemoryCacher.MemoryPartition> protected final ReentrantReadWriteLock.ReadLockprotected ScheduledFuture<?> Cancelable timerprotected AtomicBooleanprotected AtomicBooleanprotected intExpire time, in SECONDS (0 = never expires)protected booleanDo you need to make a copy of the returned values?protected final ReentrantReadWriteLock.WriteLockFields inherited from class MoleculerComponent
broker, logger, nameFields inherited from interface MetricConstants
MOLECULER_CACHER_CLEAN_TIME, MOLECULER_CACHER_CLEAN_TIME_DESC, MOLECULER_CACHER_CLEAN_TOTAL, MOLECULER_CACHER_CLEAN_TOTAL_DESC, MOLECULER_CACHER_DEL_TIME, MOLECULER_CACHER_DEL_TIME_DESC, MOLECULER_CACHER_DEL_TOTAL, MOLECULER_CACHER_DEL_TOTAL_DESC, MOLECULER_CACHER_EXPIRED_TOTAL, MOLECULER_CACHER_EXPIRED_TOTAL_DESC, MOLECULER_CACHER_FOUND_TOTAL, MOLECULER_CACHER_FOUND_TOTAL_DESC, MOLECULER_CACHER_GET_TIME, MOLECULER_CACHER_GET_TIME_DESC, MOLECULER_CACHER_GET_TOTAL, MOLECULER_CACHER_GET_TOTAL_DESC, MOLECULER_CACHER_SET_TIME, MOLECULER_CACHER_SET_TIME_DESC, MOLECULER_CACHER_SET_TOTAL, MOLECULER_CACHER_SET_TOTAL_DESC, MOLECULER_CIRCUIT_BREAKER_HALF_OPENED_ACTIVE, MOLECULER_CIRCUIT_BREAKER_HALF_OPENED_ACTIVE_DESC, MOLECULER_CIRCUIT_BREAKER_OPENED_ACTIVE, MOLECULER_CIRCUIT_BREAKER_OPENED_ACTIVE_DESC, MOLECULER_CIRCUIT_BREAKER_OPENED_TOTAL, MOLECULER_CIRCUIT_BREAKER_OPENED_TOTAL_DESC, MOLECULER_EVENT_BROADCAST_TOTAL, MOLECULER_EVENT_BROADCAST_TOTAL_DESC, MOLECULER_EVENT_BROADCASTLOCAL_TOTAL, MOLECULER_EVENT_BROADCASTLOCAL_TOTAL_DESC, MOLECULER_EVENT_EMIT_TOTAL, MOLECULER_EVENT_EMIT_TOTAL_DESC, MOLECULER_EVENT_RECEIVED_ACTIVE, MOLECULER_EVENT_RECEIVED_ACTIVE_DESC, MOLECULER_EVENT_RECEIVED_ERROR_TOTAL, MOLECULER_EVENT_RECEIVED_ERROR_TOTAL_DESC, MOLECULER_EVENT_RECEIVED_TIME, MOLECULER_EVENT_RECEIVED_TIME_DESC, MOLECULER_EVENT_RECEIVED_TOTAL, MOLECULER_EVENT_RECEIVED_TOTAL_DESC, MOLECULER_EXECUTOR, MOLECULER_REQUEST_ACTIVE, MOLECULER_REQUEST_ACTIVE_DESC, MOLECULER_REQUEST_ERROR_TOTAL, MOLECULER_REQUEST_ERROR_TOTAL_DESC, MOLECULER_REQUEST_LEVELS, MOLECULER_REQUEST_LEVELS_DESC, MOLECULER_REQUEST_RETRY_ATTEMPTS_TOTAL, MOLECULER_REQUEST_RETRY_ATTEMPTS_TOTAL_DESC, MOLECULER_REQUEST_TIME, MOLECULER_REQUEST_TIME_DESC, MOLECULER_REQUEST_TIMEOUT_TOTAL, MOLECULER_REQUEST_TIMEOUT_TOTAL_DESC, MOLECULER_REQUEST_TOTAL, MOLECULER_REQUEST_TOTAL_DESC, MOLECULER_SCHEDULER, MOLECULER_TRANSIT_CONNECTED, MOLECULER_TRANSIT_CONNECTED_DESC, MOLECULER_TRANSIT_ORPHAN_RESPONSE_TOTAL, MOLECULER_TRANSIT_ORPHAN_RESPONSE_TOTAL_DESC, MOLECULER_TRANSIT_REQUESTS_ACTIVE, MOLECULER_TRANSIT_REQUESTS_ACTIVE_DESC, MOLECULER_TRANSIT_STREAMS_RECEIVE_ACTIVE, MOLECULER_TRANSIT_STREAMS_RECEIVE_ACTIVE_DESC, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_BYTES, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_BYTES_DESC, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_TOTAL, MOLECULER_TRANSPORTER_PACKETS_RECEIVED_TOTAL_DESC, MOLECULER_TRANSPORTER_PACKETS_SENT_BYTES, MOLECULER_TRANSPORTER_PACKETS_SENT_BYTES_DESC, MOLECULER_TRANSPORTER_PACKETS_SENT_TOTAL, MOLECULER_TRANSPORTER_PACKETS_SENT_TOTAL_DESC -
Constructor Summary
ConstructorsConstructorDescriptionMemoryCacher(int capacityPerPartition, int defaultTtl) MemoryCacher(int capacityPerPartition, int defaultTtl, int cleanupSeconds) -
Method Summary
Modifier and TypeMethodDescriptionio.datatree.PromiseCleans this cache.io.datatree.PromiseDeletes a content from this cache.io.datatree.PromiseGets a cached content by a key.io.datatree.PromiseLists all keys of cached entries.intintprotected MemoryCacher.MemoryPartitiongetPartition(String prefix) intgetTtl()booleanprotected intpartitionPosition(String key, boolean throwErrorIfMissing) voidrun()io.datatree.PromiseSets a content by key into the cache.voidsetCapacity(int capacity) voidsetCleanup(int cleanup) voidsetTtl(int ttl) voidsetUseCloning(boolean useCloning) voidstarted(ServiceBroker broker) Initializes cacher instance.protected voidstartTimer(int entryTTL) voidstopped()Methods inherited from class Cacher
appendToKey, appendTree, getCacheKey, install, isCachingDisabledByTheContext, isDebug, logClean, serializeKey, setDebugMethods inherited from class MoleculerComponent
getBroker, getLogger, getName
-
Field Details
-
capacity
protected int capacityMaximum number of entries per partition -
ttl
protected int ttlExpire time, in SECONDS (0 = never expires) -
cleanup
protected int cleanupCleanup period time, in SECONDS (0 = disable cleanup process) -
useCloning
protected boolean useCloningDo 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
-
writeLock
-
partitions
-
timer
Cancelable timer -
timerStarted
-
timerStopped
-
counterExpired
-
counterGet
-
counterSet
-
counterDel
-
counterClean
-
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
Initializes cacher instance.- Specified by:
startedin interfaceMoleculerLifecycle- Overrides:
startedin classCacher- Parameters:
broker- parent ServiceBroker- Throws:
Exception
-
startTimer
protected void startTimer(int entryTTL) -
run
-
stopped
public void stopped()- Specified by:
stoppedin interfaceMoleculerLifecycle- Overrides:
stoppedin classMoleculerComponent
-
get
-
getPartition
-
set
Description copied from class:CacherSets a content by key into the cache. -
del
-
clean
-
partitionPosition
-
getCacheKeys
public io.datatree.Promise getCacheKeys()Lists all keys of cached entries.- Specified by:
getCacheKeysin classCacher- 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)
-