Class PortalCache<T>

  • Type Parameters:
    T - Type of the objects that will be cached.
    Direct Known Subclasses:
    InterfaceInstanceHtmlCache

    @StableMinor(version="16.0",
                 sinceVersion="4.0")
    public class PortalCache<T>
    extends java.lang.Object
    Memory cache for portal objects. The identifier of a portal cache instance (given in the constructor) must be unique.
    Since:
    4.0.0
    Version:
    $Revision: 25808 $ $Date: 2023-07-04 15:20:55 -0300 (Tue, 04 Jul 2023) $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_AGE_INFINITE
      Value that indicates that there is no maximum age for entries in this cache.
    • Constructor Summary

      Constructors 
      Constructor Description
      PortalCache​(java.lang.String cacheId)  
      PortalCache​(java.lang.String cacheId, boolean autoClone)  
      PortalCache​(java.lang.String cacheId, ICacheDataProvider<T> defaultCacheDataProvider)
      Creates a portal cache instance with the specified identifier and cache data provider.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()  
      void clear​(ITransaction transaction)
      Clears the cache and disables it until the given transaction ends.
      static void clearAll()  
      void destroy()  
      T fetch​(java.lang.String key)
      Obtain the value associated with the specified key.
      T fetch​(java.lang.String key, int maxAgeSeconds)
      Obtain the value associated with the specified key.
      T fetch​(java.lang.String key, int maxAgeSeconds, java.lang.String... groups)
      Obtains the value associated with the specified key and groups.
      T fetch​(java.lang.String key, java.lang.String... groups)
      Obtain the value associated with the specified key and groups.
      T fetch​(java.lang.String key, ICacheDataProvider<T> cacheDataProvider)
      Obtain the value associated with the specified key.
      T fetch​(java.lang.String key, ICacheDataProvider<T> cacheDataProvider, java.lang.String... groups)
      Obtain the value associated with the specified key and groups.
      T get​(java.lang.String key)
      Retrieves the cached object using the configured max age in seconds or null if there's no cached object at the given key.
      T get​(java.lang.String key, int maxAgeSeconds)
      Retrieves the cached object using the given max age in seconds or null if there's no cached object at the given key.
      static PortalCache getCache​(java.lang.String cacheId)  
      static java.util.Collection<java.lang.String> getCacheIds()  
      int getCapacity()
      Returns the current capacity of this portal cache.
      java.lang.String getId()  
      int getMaxAgeSeconds()
      Returns the current maximum age for entries in this cache.
      int getSize()
      Returns the number of entries currently stored in this cache.
      ICacheStatistics getStatistics()  
      void put​(java.lang.String key, T value)  
      void put​(java.lang.String key, T value, java.lang.String... groups)
      Stores the key value along with the groups association.
      void remove​(java.lang.String key)  
      void remove​(java.lang.String key, ITransaction transaction)
      Removes an entry from the cache now and after the given transaction ends.
      void removeGroup​(java.lang.String group)
      Removes all keys associated with the given group.
      void setCapacity​(int newCapacity)
      Sets the capacity for this portal cache.
      PortalCache<T> setDefaultCacheMultiDataProvider​(ICacheMultiDataProvider<T> cacheMultiDataProvider)
      Sets the default cache multi-data provider.
      void setMaxAgeSeconds​(int maxAgeSeconds)
      Sets the maximum age for entries in this cache.
      protected void setReplicateOnPut()
      Experimental feature - Configure this cache to replicate an entry when it is added to the cache.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • MAX_AGE_INFINITE

        public static final int MAX_AGE_INFINITE
        Value that indicates that there is no maximum age for entries in this cache.
        Since:
        4.2.1
        See Also:
        Constant Field Values
    • Constructor Detail

      • PortalCache

        public PortalCache​(java.lang.String cacheId)
      • PortalCache

        public PortalCache​(java.lang.String cacheId,
                           ICacheDataProvider<T> defaultCacheDataProvider)
        Creates a portal cache instance with the specified identifier and cache data provider.
        Parameters:
        cacheId - the cache identifier.
        defaultCacheDataProvider - the default cache data provider for this cache instance.
        Since:
        4.2.0
      • PortalCache

        public PortalCache​(java.lang.String cacheId,
                           boolean autoClone)
    • Method Detail

      • clearAll

        public static void clearAll()
      • getId

        public java.lang.String getId()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • getMaxAgeSeconds

        public int getMaxAgeSeconds()
        Returns the current maximum age for entries in this cache.
        Returns:
        the maximum age in seconds, or MAX_AGE_INFINITE to indicate there is no maximum age.
        Since:
        4.2.1
      • setMaxAgeSeconds

        public void setMaxAgeSeconds​(int maxAgeSeconds)
        Sets the maximum age for entries in this cache. When the specified time has passed since an entry was added, it will no longer be returned by this cache.
        Parameters:
        maxAgeSeconds - the maximum age in seconds, or MAX_AGE_INFINITE to indicate there is no maximum age.
        Since:
        4.2.1
      • setDefaultCacheMultiDataProvider

        public PortalCache<T> setDefaultCacheMultiDataProvider​(ICacheMultiDataProvider<T> cacheMultiDataProvider)
        Sets the default cache multi-data provider. It will replace any previously set default ICacheDataProvider or ICacheMultiDataProvider in this cache.
        Parameters:
        cacheMultiDataProvider - the cache multi-data provider to set as default.
        Returns:
        this portal cache for operation chaining.
        Since:
        10.0.0
      • get

        public T get​(java.lang.String key)
        Retrieves the cached object using the configured max age in seconds or null if there's no cached object at the given key.
        Parameters:
        key - the cache key
        Returns:
        the cached object using the configured max age in seconds or null if there's no cached object at the given key.
        Since:
        16.0.0
      • get

        public T get​(java.lang.String key,
                     int maxAgeSeconds)
        Retrieves the cached object using the given max age in seconds or null if there's no cached object at the given key.
        Parameters:
        key - the cache key
        maxAgeSeconds - the max age in seconds
        Returns:
        the cached object using the given max age in seconds or null if there's no cached object at the given key.
        Since:
        16.0.0
      • fetch

        public T fetch​(java.lang.String key)
                throws PortalException
        Obtain the value associated with the specified key. If the value is not available in the cache, the default ICacheDataProvider or ICacheMultiDataProvider of this PortalCache is used to read the value, and the returned values are cached.
        Parameters:
        key - the key.
        Returns:
        the value.
        Throws:
        java.lang.UnsupportedOperationException - if there is no ICacheDataProvider or ICacheMultiDataProvider specified for this cache instance.
        PortalException - if the loadData method of the data provider used throws it.
        java.lang.RuntimeException - any runtime exception thrown by the loadData method of the data provider used is thrown by this method.
        Since:
        4.2.0
      • fetch

        public T fetch​(java.lang.String key,
                       int maxAgeSeconds)
                throws PortalException
        Obtain the value associated with the specified key. If the value is not available in the cache, the default ICacheDataProvider or ICacheMultiDataProvider of this PortalCache is used to read the value, and the returned values are cached.
        Parameters:
        key - the key.
        maxAgeSeconds - the maximum age in seconds the returned value must have. If the specified time has passed since the entry found was added, it will be reloaded as if it did not exist. The value MAX_AGE_INFINITE indicates there is no maximum age limit.
        Returns:
        the value.
        Throws:
        java.lang.UnsupportedOperationException - if there is no ICacheDataProvider or ICacheMultiDataProvider specified for this cache instance.
        PortalException - if the loadData method of the data provider used throws it.
        java.lang.RuntimeException - any runtime exception thrown by the loadData method of the data provider used is thrown by this method.
        Since:
        14.0.0
      • fetch

        public T fetch​(java.lang.String key,
                       java.lang.String... groups)
                throws PortalException
        Obtain the value associated with the specified key and groups. If the value is not available in the cache, the ICacheDataProvider of this PortalCache is used to read the value, and it is cached.
        Parameters:
        key - the key.
        groups - the groups associated with the key.
        Returns:
        the value.
        Throws:
        java.lang.UnsupportedOperationException - if there is no ICacheDataProvider or ICacheMultiDataProvider specified for this cache instance.
        PortalException - if the loadData method of the data provider used throws it.
        java.lang.RuntimeException - any runtime exception thrown by the loadData method of the data provider used is thrown by this method.
        Since:
        6.2.0
      • fetch

        public T fetch​(java.lang.String key,
                       int maxAgeSeconds,
                       java.lang.String... groups)
                throws PortalException
        Obtains the value associated with the specified key and groups. If the value is not available in the cache, the ICacheDataProvider of this PortalCache is used to read the value, and it is cached.
        Parameters:
        key - the key.
        maxAgeSeconds - the maximum age in seconds the returned value must have. If the specified time has passed since the entry found was added, it will be reloaded as if it did not exist. The value MAX_AGE_INFINITE indicates there is no maximum age limit.
        groups - the groups associated with the key.
        Returns:
        the value.
        Throws:
        java.lang.UnsupportedOperationException - if there is no ICacheDataProvider or ICacheMultiDataProvider specified for this cache instance.
        PortalException - if the loadData method of the data provider used throws it.
        java.lang.RuntimeException - any runtime exception thrown by the loadData method of the data provider used is thrown by this method.
        Since:
        14.0.0
      • fetch

        public T fetch​(java.lang.String key,
                       ICacheDataProvider<T> cacheDataProvider,
                       java.lang.String... groups)
                throws PortalException
        Obtain the value associated with the specified key and groups.
        Parameters:
        key - the key.
        cacheDataProvider - the data provider used if the value is not currently available in the cache.
        groups - the groups associated with the key.
        Returns:
        the value.
        Throws:
        PortalException - if ICacheDataProvider.loadData(String) throws it.
        java.lang.RuntimeException - any runtime exception thrown by ICacheDataProvider.loadData(String) is thrown by this method.
        Since:
        6.2.0
      • put

        public void put​(java.lang.String key,
                        T value)
      • put

        public void put​(java.lang.String key,
                        T value,
                        java.lang.String... groups)
        Stores the key value along with the groups association.
        Parameters:
        key - the key.
        value - the key value.
        groups - one or more groups.
        Since:
        6.2.0
      • remove

        public void remove​(java.lang.String key)
      • removeGroup

        public void removeGroup​(java.lang.String group)
        Removes all keys associated with the given group.
        Parameters:
        group - the group identifier.
        Since:
        6.2.0
      • remove

        public void remove​(java.lang.String key,
                           ITransaction transaction)
                    throws PortalException
        Removes an entry from the cache now and after the given transaction ends. The entry is not stored in the cache until the transaction ends (commits or rollsback).
        Parameters:
        key - the entry key.
        transaction - the transaction.
        Throws:
        PortalException
        Since:
        4.0.10
      • clear

        public void clear()
      • clear

        public void clear​(ITransaction transaction)
                   throws PortalException
        Clears the cache and disables it until the given transaction ends. No entry is stored in the cache until the transaction ends (commits or rollsback).
        Parameters:
        transaction - the transaction.
        Throws:
        PortalException
        Since:
        4.1.0
      • getSize

        public int getSize()
        Returns the number of entries currently stored in this cache.
        Returns:
        the number of entries currently stored in this cache.
        Since:
        4.0.11
      • getCapacity

        public int getCapacity()
        Returns the current capacity of this portal cache.
        Returns:
        the current capacity of this portal cache, in number of entries.
        Since:
        4.0.11
      • setCapacity

        public void setCapacity​(int newCapacity)
        Sets the capacity for this portal cache.
        Since:
        4.0.11
      • destroy

        public void destroy()
      • setReplicateOnPut

        protected void setReplicateOnPut()
        Experimental feature - Configure this cache to replicate an entry when it is added to the cache.
        Since:
        7.0.0
      • getCache

        public static PortalCache getCache​(java.lang.String cacheId)
      • getCacheIds

        public static java.util.Collection<java.lang.String> getCacheIds()