Class CacheManager

All Implemented Interfaces:
DBConstants, DBListener, JcmsConstants, PluginComponent, StoreListener, JaliosConstants, Serializable, EventListener, javax.servlet.http.HttpSessionBindingListener

public class CacheManager extends BasicDBListenerStoreListener implements javax.servlet.http.HttpSessionBindingListener, JcmsConstants, Serializable
This class manages the cache of content fragments.

Cache entries can be invalidated either manually (see clearContent() method), or when a timeout is reached or when a mutation occurs on a set of (Storable) classes.

Since:
jcms-3.0
See Also:
  • Field Details

    • SESSION_SEPARATOR

      public static final String SESSION_SEPARATOR
      See Also:
    • CACHES_DIR

      public static final String CACHES_DIR
      See Also:
    • SESSIONS_DIR

      public static final String SESSIONS_DIR
      See Also:
    • NOT_EXPIRED

      public static final int NOT_EXPIRED
      See Also:
    • defaultServerCacheLRU

      protected transient com.jalios.jcms.CacheManager.ServerCacheLRU defaultServerCacheLRU
      LRU of the default configuration
    • customServerCacheLRUMap

      protected transient Map<String,com.jalios.jcms.CacheManager.ServerCacheLRU> customServerCacheLRUMap
      LRU of the default configuration. Map of custom configuration name to ServerCacheLRU instance.
  • Constructor Details

    • CacheManager

      public CacheManager()
      Constructs a new CacheManager
    • CacheManager

      protected CacheManager(boolean failsafeMode)
  • Method Details

    • getFailSafeModeInstance

      public static CacheManager getFailSafeModeInstance()
    • reinitialize

      public void reinitialize()
      Reinitialize the CacheManager. Reload isDisabled and MaxServersCacheNbr values from properties.
    • isDisabled

      public boolean isDisabled()
      Returns:
      true if the cache manager is disabled.
    • getMaxServersCacheNbr

      public int getMaxServersCacheNbr()
      Returns:
      the maximum number of server's cache allowed.
    • getServerCacheIdMap

      protected Map<String,String> getServerCacheIdMap(String id)
      Retrieve the server Cache Map to use for the specified cache id
      Parameters:
      id - must not be null
      Returns:
      the custom cache map to use or the default cache map, never return null
    • buildEntryId

      public String buildEntryId(String contentId, String sessionId, String serverCacheConfiguration)
      Build the entry id given the content's id and the associated session id. Given this 2 parameter, the returned id will be always the same.
      Parameters:
      contentId - the content id
      sessionId - the http session id, exclusive with serverCacheConfiguration
      serverCacheConfiguration - the custom configuration to use for server configuration, exclusive with sessionId
      Returns:
      the id to be used for a cache entry.
    • addEntry

      public String addEntry(String contentId, javax.servlet.http.HttpSession session, boolean sessionScope, String content, Class<?>[] classes, long timeout, Map<?,?> extraInfoMap, String serverCacheConfig)
      Add a new cache entry
      Parameters:
      contentId - the id of content
      session - the session
      sessionScope - should the cache entry be stored for this session
      content - the content to be cached
      classes - an array of classes to listen. Each time a mutation occurs on a (visible) instance of the classes the content of the cache is cleared.
      timeout - the timeout of this entry (in millis)
      extraInfoMap - any contextual data to the cache being created
      serverCacheConfig - an optional custom server cache configuration to use (use only when sessionScope = false)
      Returns:
      the id of the entry (concatenation of id and session's id).
    • isCached

      public boolean isCached(String id)
      Check if their is a cache entry associated with the given id.
      Parameters:
      id - the CacheEntry's id
      Returns:
      true if there is a cache entry associated with this id, false otherwise.
    • getSize

      public int getSize()
      Get the size of the cache (ie the count of cache entries).
      Returns:
      the count of cache entries
    • getCacheEntries

      public Collection<CacheManager.CacheEntry> getCacheEntries()
      Get all the cache entries.
      Returns:
      a NEW collection of CacheEntry
    • getCacheEntry

      public CacheManager.CacheEntry getCacheEntry(String id)
      Get the CacheEntry from a given entry's id.
      Parameters:
      id - the entry's id
      Returns:
      the CacheEntry bound to the given id
    • removeEntry

      public void removeEntry(String id)
      Remove a cache entry referenced with is id.
      Parameters:
      id - the entry's id
    • removeAllEntries

      public void removeAllEntries()
      Clear all the cache entries.
    • removeAllSessionEntries

      public void removeAllSessionEntries()
      Clear all the session cache entries.
    • removeSessionEntries

      public void removeSessionEntries(javax.servlet.http.HttpSession session)
      Clear all the session cache entries for a given session
      Parameters:
      session - the session
      Since:
      jcms-4.0
    • invalidateClass

      public void invalidateClass(Class<?> clazz)
      Invalidate the content of all entries bound to the given class
      Parameters:
      clazz - the class for which the entries must be cleared
    • handleCreate

      public void handleCreate(Storable storable, boolean firstTime)
      Description copied from interface: StoreListener
      This method gets called when a storable is created.
      Specified by:
      handleCreate in interface StoreListener
      Overrides:
      handleCreate in class BasicDBListenerStoreListener
      Parameters:
      storable - the storable
      firstTime - true if it is the first time this operation is processed
      See Also:
    • handleCommitUpdate

      public void handleCommitUpdate(Storable storable, Storable oldStorable, boolean firstTime)
      Description copied from interface: StoreListener
      This method gets called when after a storable has been updated.
      Specified by:
      handleCommitUpdate in interface StoreListener
      Overrides:
      handleCommitUpdate in class BasicDBListenerStoreListener
      Parameters:
      storable - the storable, this instance is the object that was created at first and that will be stored in jcms until deletion of the storable with this ID. You should always manipulate this object if you maintain any index.
      oldStorable - the previous version of the storable, it is a clone, do not use in any index, it should only be used to access old values.
      firstTime - true if it is the first time this operation is processed
      See Also:
    • handleDelete

      public void handleDelete(Storable storable, boolean firstTime)
      Description copied from interface: StoreListener
      This method gets called when a storable is deleted.
      Specified by:
      handleDelete in interface StoreListener
      Overrides:
      handleDelete in class BasicDBListenerStoreListener
      Parameters:
      storable - the storable
      firstTime - true if it is the first time this operation is processed
      See Also:
    • onPostInsert

      public void onPostInsert(DBData data, org.hibernate.event.PostInsertEvent event, boolean firstTime)
      Description copied from interface: DBListener
      This method is called after a DBData has been created.
      Specified by:
      onPostInsert in interface DBListener
      Overrides:
      onPostInsert in class BasicDBListener
      Parameters:
      data - the data.
      event - the event.
      firstTime - true on the JSync replica on which the operation took place and false on the other replicas.
      See Also:
    • onPostUpdate

      public void onPostUpdate(DBData data, org.hibernate.event.PostUpdateEvent event, boolean firstTime)
      Description copied from interface: DBListener
      This method is called after a DBData has been updated.
      Specified by:
      onPostUpdate in interface DBListener
      Overrides:
      onPostUpdate in class BasicDBListener
      Parameters:
      data - the data.
      event - the event.
      firstTime - true on the JSync replica on which the operation took place and false on the other replicas.
      See Also:
    • onPostDelete

      public void onPostDelete(DBData data, org.hibernate.event.PostDeleteEvent event, boolean firstTime)
      Description copied from interface: DBListener
      This method is called after a DBData has been deleted.
      Specified by:
      onPostDelete in interface DBListener
      Overrides:
      onPostDelete in class BasicDBListener
      Parameters:
      data - the deleted DBData. WARNING! In a JSync Cluster, when firstTime is false, this data will be null. In this case, to get information about this deleted data, you must use the event and in particular event.getId(), which returns the JPlatform ID of the data, and event.getDeletedState() which returns the attributes of the data.
      event - the event
      firstTime - true on the JSync replica on which the operation took place and false on the other replicas.
      See Also:
    • valueBound

      public void valueBound(javax.servlet.http.HttpSessionBindingEvent event)
      Specified by:
      valueBound in interface javax.servlet.http.HttpSessionBindingListener
    • valueUnbound

      public void valueUnbound(javax.servlet.http.HttpSessionBindingEvent event)
      Specified by:
      valueUnbound in interface javax.servlet.http.HttpSessionBindingListener