Interface RMapCache<K,V>

Type Parameters:
K - key
V - value
All Superinterfaces:
ConcurrentMap<K,V>, Map<K,V>, RDestroyable, RExpirable, RExpirableAsync, RMap<K,V>, RMapAsync<K,V>, RMapCacheAsync<K,V>, RObject, RObjectAsync
All Known Implementing Classes:
RedissonMapCache, RedissonTransactionalMapCache

public interface RMapCache<K,V> extends RMap<K,V>, RMapCacheAsync<K,V>

Map-based cache with ability to set TTL for each entry via put(Object, Object, long, TimeUnit) or putIfAbsent(Object, Object, long, TimeUnit) And therefore has an complex lua-scripts inside.

Current redis implementation doesnt have map entry eviction functionality. Thus entries are checked for TTL expiration during any key/value/entry read operation. If key/value/entry expired then it doesn't returns. Expired tasks cleaned by EvictionScheduler. This scheduler deletes expired entries in time interval between 5 seconds to 2 hours.

If eviction is not required then it's better to use RedissonMap.

Author:
Nikita Koksharov
  • Method Details

    • setMaxSize

      void setMaxSize(int maxSize)
      Sets max size of the map and overrides current value. Superfluous elements are evicted using LRU algorithm.
      Parameters:
      maxSize - - max size If 0 the cache is unbounded (default).
    • setMaxSize

      void setMaxSize(int maxSize, EvictionMode mode)
      Sets max size of the map and overrides current value. Superfluous elements are evicted using defined algorithm.
      Parameters:
      maxSize - - max size
      mode - - eviction mode
    • trySetMaxSize

      boolean trySetMaxSize(int maxSize)
      Tries to set max size of the map. Superfluous elements are evicted using LRU algorithm.
      Parameters:
      maxSize - - max size
      Returns:
      true if max size has been successfully set, otherwise false. If 0 the cache is unbounded (default).
    • trySetMaxSize

      boolean trySetMaxSize(int maxSize, EvictionMode mode)
      Tries to set max size of the map. Superfluous elements are evicted using defined algorithm.
      Parameters:
      maxSize - - max size
      mode - - eviction mode
      Returns:
      true if max size has been successfully set, otherwise false.
    • putIfAbsent

      V putIfAbsent(K key, V value, long ttl, TimeUnit ttlUnit)
      If the specified key is not already associated with a value, associate it with the given value.

      Stores value mapped by key with specified time to live. Entry expires after specified time to live.

      Parameters:
      key - - map key
      value - - map value
      ttl - - time to live for key\value entry. If 0 then stores infinitely.
      ttlUnit - - time unit
      Returns:
      current associated value
    • putIfAbsent

      V putIfAbsent(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit)
      If the specified key is not already associated with a value, associate it with the given value.

      Stores value mapped by key with specified time to live and max idle time. Entry expires when specified time to live or max idle time has expired.

      Parameters:
      key - - map key
      value - - map value
      ttl - - time to live for key\value entry. If 0 then time to live doesn't affect entry expiration.
      ttlUnit - - time unit
      maxIdleTime - - max idle time for key\value entry. If 0 then max idle time doesn't affect entry expiration.
      maxIdleUnit - - time unit

      if maxIdleTime and ttl params are equal to 0 then entry stores infinitely.

      Returns:
      current associated value
    • put

      V put(K key, V value, long ttl, TimeUnit unit)
      Stores value mapped by key with specified time to live. Entry expires after specified time to live.

      If the map previously contained a mapping for the key, the old value is replaced by the specified value.

      Parameters:
      key - - map key
      value - - map value
      ttl - - time to live for key\value entry. If 0 then stores infinitely.
      unit - - time unit
      Returns:
      previous associated value
    • put

      V put(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit)
      Stores value mapped by key with specified time to live and max idle time. Entry expires when specified time to live or max idle time has expired.

      If the map previously contained a mapping for the key, the old value is replaced by the specified value.

      Parameters:
      key - - map key
      value - - map value
      ttl - - time to live for key\value entry. If 0 then time to live doesn't affect entry expiration.
      ttlUnit - - time unit
      maxIdleTime - - max idle time for key\value entry. If 0 then max idle time doesn't affect entry expiration.
      maxIdleUnit - - time unit

      if maxIdleTime and ttl params are equal to 0 then entry stores infinitely.

      Returns:
      previous associated value
    • fastPut

      boolean fastPut(K key, V value, long ttl, TimeUnit ttlUnit)
      Stores value mapped by key with specified time to live. Entry expires after specified time to live.

      If the map previously contained a mapping for the key, the old value is replaced by the specified value.

      Works faster than usual put(Object, Object, long, TimeUnit) as it not returns previous value.

      Parameters:
      key - - map key
      value - - map value
      ttl - - time to live for key\value entry. If 0 then stores infinitely.
      ttlUnit - - time unit
      Returns:
      true if key is a new key in the hash and value was set. false if key already exists in the hash and the value was updated.
    • fastPut

      boolean fastPut(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit)
      Stores value mapped by key with specified time to live and max idle time. Entry expires when specified time to live or max idle time has expired.

      If the map previously contained a mapping for the key, the old value is replaced by the specified value.

      Works faster than usual put(Object, Object, long, TimeUnit, long, TimeUnit) as it not returns previous value.

      Parameters:
      key - - map key
      value - - map value
      ttl - - time to live for key\value entry. If 0 then time to live doesn't affect entry expiration.
      ttlUnit - - time unit
      maxIdleTime - - max idle time for key\value entry. If 0 then max idle time doesn't affect entry expiration.
      maxIdleUnit - - time unit

      if maxIdleTime and ttl params are equal to 0 then entry stores infinitely.

      Returns:
      true if key is a new key in the hash and value was set. false if key already exists in the hash and the value was updated.
    • fastPutIfAbsent

      boolean fastPutIfAbsent(K key, V value, long ttl, TimeUnit ttlUnit)
      If the specified key is not already associated with a value, associate it with the given value.

      Stores value mapped by key with specified time to live. Entry expires after specified time to live.

      Works faster than usual putIfAbsent(Object, Object, long, TimeUnit) as it not returns previous value.

      Parameters:
      key - - map key
      value - - map value
      ttl - - time to live for key\value entry. If 0 then stores infinitely.
      ttlUnit - - time unit
      Returns:
      true if key is a new key in the hash and value was set. false if key already exists in the hash
    • fastPutIfAbsent

      boolean fastPutIfAbsent(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit)
      If the specified key is not already associated with a value, associate it with the given value.

      Stores value mapped by key with specified time to live and max idle time. Entry expires when specified time to live or max idle time has expired.

      Works faster than usual putIfAbsent(Object, Object, long, TimeUnit, long, TimeUnit) as it not returns previous value.

      Parameters:
      key - - map key
      value - - map value
      ttl - - time to live for key\value entry. If 0 then time to live doesn't affect entry expiration.
      ttlUnit - - time unit
      maxIdleTime - - max idle time for key\value entry. If 0 then max idle time doesn't affect entry expiration.
      maxIdleUnit - - time unit

      if maxIdleTime and ttl params are equal to 0 then entry stores infinitely.

      Returns:
      true if key is a new key in the hash and value was set. false if key already exists in the hash.
    • putAll

      void putAll(Map<? extends K,? extends V> map, long ttl, TimeUnit ttlUnit)
      Associates the specified value with the specified key in batch.

      If MapWriter is defined then new map entries will be stored in write-through mode.

      Parameters:
      map - - mappings to be stored in this map
      ttl - - time to live for all key\value entries. If 0 then stores infinitely.
      ttlUnit - - time unit
    • updateEntryExpiration

      @Deprecated boolean updateEntryExpiration(K key, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit)
      Deprecated.
      Parameters:
      key - - map key
      ttl - - time to live for key\value entry. If 0 then time to live doesn't affect entry expiration.
      ttlUnit - - time unit
      maxIdleTime - - max idle time for key\value entry. If 0 then max idle time doesn't affect entry expiration.
      maxIdleUnit - - time unit

      if maxIdleTime and ttl params are equal to 0 then entry stores infinitely.

      Returns:
      returns false if entry already expired or doesn't exist, otherwise returns true.
    • expireEntry

      boolean expireEntry(K key, Duration ttl, Duration maxIdleTime)
      Updates time to live and max idle time of specified entry by key. Entry expires when specified time to live or max idle time was reached.

      Returns false if entry already expired or doesn't exist, otherwise returns true.

      Parameters:
      key - map key
      ttl - time to live for key\value entry. If 0 then time to live doesn't affect entry expiration.
      maxIdleTime - max idle time for key\value entry. If 0 then max idle time doesn't affect entry expiration.

      if maxIdleTime and ttl params are equal to 0 then entry stores infinitely.

      Returns:
      returns false if entry already expired or doesn't exist, otherwise returns true.
    • expireEntries

      int expireEntries(Set<K> keys, Duration ttl, Duration maxIdleTime)
      Updates time to live and max idle time of specified entries by keys. Entries expires when specified time to live or max idle time was reached.

      Returns amount of updated entries.

      Parameters:
      keys - map keys
      ttl - time to live for key\value entries. If 0 then time to live doesn't affect entry expiration.
      maxIdleTime - max idle time for key\value entries. If 0 then max idle time doesn't affect entry expiration.

      if maxIdleTime and ttl params are equal to 0 then entries are stored infinitely.

      Returns:
      amount of updated entries.
    • expireEntryIfNotSet

      boolean expireEntryIfNotSet(K key, Duration ttl, Duration maxIdleTime)
      Sets time to live and max idle time of specified entry by key. If these parameters weren't set before. Entry expires when specified time to live or max idle time was reached.

      Returns false if entry already has expiration time or doesn't exist, otherwise returns true.

      Parameters:
      key - map key
      ttl - time to live for key\value entry. If 0 then time to live doesn't affect entry expiration.
      maxIdleTime - max idle time for key\value entry. If 0 then max idle time doesn't affect entry expiration.

      if maxIdleTime and ttl params are equal to 0 then entry stores infinitely.

      Returns:
      returns false if entry already has expiration time or doesn't exist, otherwise returns true.
    • expireEntriesIfNotSet

      int expireEntriesIfNotSet(Set<K> keys, Duration ttl, Duration maxIdleTime)
      Sets time to live and max idle time of specified entries by keys. If these parameters weren't set before. Entries expire when specified time to live or max idle time was reached.

      Returns amount of updated entries.

      Parameters:
      keys - map keys
      ttl - time to live for key\value entry. If 0 then time to live doesn't affect entry expiration.
      maxIdleTime - max idle time for key\value entry. If 0 then max idle time doesn't affect entry expiration.

      if maxIdleTime and ttl params are equal to 0 then entry stores infinitely.

      Returns:
      amount of updated entries.
    • getWithTTLOnly

      V getWithTTLOnly(K key)
      Returns the value mapped by defined key or null if value is absent.

      If map doesn't contain value for specified key and MapLoader is defined then value will be loaded in read-through mode.

      NOTE: Idle time of entry is not taken into account. Entry last access time isn't modified if map limited by size.

      Parameters:
      key - the key
      Returns:
      the value mapped by defined key or null if value is absent
    • getAllWithTTLOnly

      Map<K,V> getAllWithTTLOnly(Set<K> keys)
      Returns map slice contained the mappings with defined keys.

      If map doesn't contain value/values for specified key/keys and MapLoader is defined then value/values will be loaded in read-through mode.

      NOTE: Idle time of entry is not taken into account. Entry last access time isn't modified if map limited by size.

      Parameters:
      keys - map keys
      Returns:
      Map slice
    • size

      int size()
      Returns the number of entries in cache. This number can reflects expired entries too due to non realtime cleanup process.
      Specified by:
      size in interface Map<K,V>
    • addListener

      int addListener(MapEntryListener listener)
      Adds map entry listener
      Parameters:
      listener - - entry listener
      Returns:
      listener id
      See Also:
    • removeListener

      void removeListener(int listenerId)
      Removes map entry listener
      Specified by:
      removeListener in interface RObject
      Parameters:
      listenerId - - listener id
    • remainTimeToLive

      long remainTimeToLive(K key)
      Remaining time to live of map entry associated with a key.
      Parameters:
      key - - map key
      Returns:
      time in milliseconds -2 if the key does not exist. -1 if the key exists but has no associated expire.