Class CacheMap

java.lang.Object
com.codename1.io.CacheMap

public class CacheMap extends Object
A cache map is essentially a hashtable that indexes entries based on age and is limited to a fixed size. Hence when an entry is placed into the cache map and the cache size needs to increase, the least referenced entry is removed. A cache hit is made both on fetching and putting, hence frequently fetched elements will never be removed from a sufficiently large cache. Cache can work purely in memory or swap data into storage based on user definitions. Notice that this class isn't threadsafe.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor
    CacheMap(String prefix)
    Creates a cache map with a prefix string
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the caches for this cache object
    void
    Clears the memory cache
    void
    Clears the storage cache
    void
    Deletes a cached entry
    get(Object key)
    Returns the object matching the given key
    A prefix prepended to storage entries to differentiate them
    int
    Indicates the size of the memory cache after which the cache won't grow further Size is indicated by number of elements stored and not by KB or similar benchmark!
    Returns the keys for all the objects currently in cache, this is useful to traverse all the objects and refresh them without actually deleting the cache and fetching them from scratch.
    int
    Indicates the size of the storage cache after which the cache won't grow further Size is indicated by number of elements stored and not by KB or similar benchmark!
    boolean
    When set to true indicates that all entries should be persisted to storage for a constantly persisting cache
    void
    put(Object key, Object value)
    Puts the given key/value pair in the cache
    void
    setAlwaysStore(boolean alwaysStore)
    When set to true indicates that all entries should be persisted to storage for a constantly persisting cache
    void
    setCachePrefix(String cachePrefix)
    A prefix prepended to storage entries to differentiate them
    void
    setCacheSize(int cacheSize)
    Indicates the size of the memory cache after which the cache won't grow further Size is indicated by number of elements stored and not by KB or similar benchmark!
    void
    setStorageCacheSize(int storageCacheSize)
    Indicates the size of the storage cache after which the cache won't grow further Size is indicated by number of elements stored and not by KB or similar benchmark!

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CacheMap

      public CacheMap()
      Default constructor
    • CacheMap

      public CacheMap(String prefix)

      Creates a cache map with a prefix string

      Parameters
      • prefix: string to prepend to the cache entries in storage
  • Method Details

    • getCacheSize

      public int getCacheSize()

      Indicates the size of the memory cache after which the cache won't grow further Size is indicated by number of elements stored and not by KB or similar benchmark!

      Returns

      the cacheSize

    • setCacheSize

      public void setCacheSize(int cacheSize)

      Indicates the size of the memory cache after which the cache won't grow further Size is indicated by number of elements stored and not by KB or similar benchmark!

      Parameters
      • cacheSize: the cacheSize to set
    • put

      public void put(Object key, Object value)

      Puts the given key/value pair in the cache

      Parameters
      • key: the key

      • value: the value

    • delete

      public void delete(Object key)

      Deletes a cached entry

      Parameters
      • key: entry to remove from the cache
    • get

      public Object get(Object key)

      Returns the object matching the given key

      Parameters
      • key: key object
      Returns

      value from a previous put or null

    • clearAllCache

      public void clearAllCache()
      Clears the caches for this cache object
    • clearMemoryCache

      public void clearMemoryCache()
      Clears the memory cache
    • getKeysInCache

      public Vector getKeysInCache()

      Returns the keys for all the objects currently in cache, this is useful to traverse all the objects and refresh them without actually deleting the cache and fetching them from scratch.

      Important this vector is a copy of a current state, keys might not exist anymore or might change, others might be added in the interim.

      Returns
      Returns:
      a vector containing a snapshot of the current elements within the cache.
    • clearStorageCache

      public void clearStorageCache()
      Clears the storage cache
    • getStorageCacheSize

      public int getStorageCacheSize()

      Indicates the size of the storage cache after which the cache won't grow further Size is indicated by number of elements stored and not by KB or similar benchmark!

      Returns

      the storageCacheSize

    • setStorageCacheSize

      public void setStorageCacheSize(int storageCacheSize)

      Indicates the size of the storage cache after which the cache won't grow further Size is indicated by number of elements stored and not by KB or similar benchmark!

      Parameters
      • storageCacheSize: the storageCacheSize to set
    • getCachePrefix

      public String getCachePrefix()

      A prefix prepended to storage entries to differentiate them

      Returns

      the cachePrefix

    • setCachePrefix

      public void setCachePrefix(String cachePrefix)

      A prefix prepended to storage entries to differentiate them

      Parameters
      • cachePrefix: the cachePrefix to set
    • isAlwaysStore

      public boolean isAlwaysStore()

      When set to true indicates that all entries should be persisted to storage for a constantly persisting cache

      Returns

      the alwaysStore

    • setAlwaysStore

      public void setAlwaysStore(boolean alwaysStore)

      When set to true indicates that all entries should be persisted to storage for a constantly persisting cache

      Parameters
      • alwaysStore: the alwaysStore to set