Class SerializingCache<K extends Serializable,​V extends Serializable>

  • All Implemented Interfaces:
    Map<K,​V>, Cache<K,​V>

    public class SerializingCache<K extends Serializable,​V extends Serializable>
    extends Object
    implements Cache<K,​V>
    A Cache implementation that stores data in serialized form. The primary benefit of usage of this cache is that the cached data is stored without any references to their classes. This allows cache content to remain usable after the classes that instantiate the data get reloaded. This is of particular interest when the cache is used to store data provided by Openfire plugins (as these classes get loaded by a class loader that is replaced when a plugin gets reloaded or upgraded). As compared to other caches, usage of this cache will require more system resources, as the serialized representation of an object typically is (much) larger than its original (unserialized) form. An instance is backed by a backing Cache that is used as a delegate. This implementation serializes data before storing it in this delegate, and deserializes it when it is retrieved. If problems occur during serialization or deserializing of data, the various methods in this class will throw runtime exceptions.
    Author:
    Guus der Kinderen, guus.der.kinderen@gmail.com
    See Also:
    Issue OF-2239: Make it easier to cache plugin class instances
    • Method Detail

      • marshall

        @Nonnull
        protected String marshall​(@Nullable
                                  Object object)
      • setName

        public void setName​(String name)
        Description copied from interface: Cache
        Sets the name of the cache
        Specified by:
        setName in interface Cache<K extends Serializable,​V extends Serializable>
        Parameters:
        name - the name of the cache
      • getCacheHits

        public long getCacheHits()
        Description copied from interface: Cache
        Returns the number of cache hits. A cache hit occurs every time the get method is called and the cache contains the requested object.

        Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.

        Specified by:
        getCacheHits in interface Cache<K extends Serializable,​V extends Serializable>
        Returns:
        the number of cache hits.
      • getCacheMisses

        public long getCacheMisses()
        Description copied from interface: Cache
        Returns the number of cache misses. A cache miss occurs every time the get method is called and the cache does not contain the requested object.

        Keeping track of cache hits and misses lets one measure how efficient the cache is; the higher the percentage of hits, the more efficient.

        Specified by:
        getCacheMisses in interface Cache<K extends Serializable,​V extends Serializable>
        Returns:
        the number of cache hits.
      • getLongCacheSize

        public long getLongCacheSize()
        Description copied from interface: Cache
        Returns the size of the cache contents in bytes. This value is only a rough approximation, so cache users should expect that actual VM memory used by the cache could be significantly higher than the value reported by this method.
        Specified by:
        getLongCacheSize in interface Cache<K extends Serializable,​V extends Serializable>
        Returns:
        the size of the cache contents in bytes.
      • getMaxCacheSize

        public long getMaxCacheSize()
        Description copied from interface: Cache
        Returns the maximum size of the cache in bytes. If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.
        Specified by:
        getMaxCacheSize in interface Cache<K extends Serializable,​V extends Serializable>
        Returns:
        the maximum size of the cache in bytes.
      • setMaxCacheSize

        public void setMaxCacheSize​(long maxSize)
        Description copied from interface: Cache
        Sets the maximum size of the cache in bytes. If the cache grows larger than the max size, the least frequently used items will be removed. If the max cache size is set to -1, there is no size limit.

        Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created

        Specified by:
        setMaxCacheSize in interface Cache<K extends Serializable,​V extends Serializable>
        Parameters:
        maxSize - the maximum size of the cache in bytes.
      • getMaxLifetime

        public long getMaxLifetime()
        Description copied from interface: Cache
        Returns the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.
        Specified by:
        getMaxLifetime in interface Cache<K extends Serializable,​V extends Serializable>
        Returns:
        the maximum number of milliseconds before objects are expired.
      • setMaxLifetime

        public void setMaxLifetime​(long maxLifetime)
        Description copied from interface: Cache
        Sets the maximum number of milliseconds that any object can live in cache. Once the specified number of milliseconds passes, the object will be automatically expired from cache. If the max lifetime is set to -1, then objects never expire.

        Note: If using the Hazelcast clustering plugin, this will not take effect until the next time the cache is created

        Specified by:
        setMaxLifetime in interface Cache<K extends Serializable,​V extends Serializable>
        Parameters:
        maxLifetime - the maximum number of milliseconds before objects are expired.
      • addClusteredCacheEntryListener

        public String addClusteredCacheEntryListener​(@Nonnull
                                                     ClusteredCacheEntryListener<K,​V> listener,
                                                     boolean includeValues,
                                                     boolean includeEventsFromLocalNode)
        Description copied from interface: Cache
        Registers a listener to receive entry events for this cache. To optimize the amount of data that is being processed, this method allows the listener to be registered in a way that suppresses values from being sent to it. In that case, only keys will be included in the event listener invocation.
        Specified by:
        addClusteredCacheEntryListener in interface Cache<K extends Serializable,​V extends Serializable>
        Parameters:
        listener - the listener to be registered.
        includeValues - defines if the listener should receive the values associated with the events.
        includeEventsFromLocalNode - defines if the listener should be invoked for events that originate on the local node.
        Returns:
        a unique identifier for the listener which is used as a key to remove the listener
      • removeClusteredCacheEntryListener

        public void removeClusteredCacheEntryListener​(@Nonnull
                                                      String listenerId)
        Description copied from interface: Cache
        Removes a previously registered event listener.
        Specified by:
        removeClusteredCacheEntryListener in interface Cache<K extends Serializable,​V extends Serializable>
        Parameters:
        listenerId - the identifier of the listener to be removed.