Class MetaCache


  • final class MetaCache
    extends java.lang.Object
    A meta-cache that tracks multiple JexlCache instances via weak references.

    Each JexlCache created by this MetaCache is held via a WeakReference, allowing it to be garbage collected as soon as no strong references exist.

    This allows for collective management of multiple caches, in particular clearing all caches at once. This operation is typically called when the uberspect class loader needs to change.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.function.IntFunction<JexlCache<?,​?>> factory  
      private java.lang.ref.ReferenceQueue<JexlCache<?,​?>> queue  
      private java.util.Set<java.lang.ref.Reference<JexlCache<?,​?>>> references  
    • Constructor Summary

      Constructors 
      Constructor Description
      MetaCache​(java.util.function.IntFunction<JexlCache<?,​?>> factory)
      Constructs a MetaCache with the given cache factory.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int cleanUp()
      Cleans up all references whose referent (the cache) has been garbage collected.
      (package private) void clearCaches()
      Clears all caches tracked by this MetaCache.
      (package private) <K,​V>
      JexlCache<K,​V>
      createCache​(int capacity)  
      (package private) int size()
      Gets the number of live caches currently tracked by this MetaCache.
      • Methods inherited from class java.lang.Object

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

      • factory

        private final java.util.function.IntFunction<JexlCache<?,​?>> factory
      • references

        private final java.util.Set<java.lang.ref.Reference<JexlCache<?,​?>>> references
      • queue

        private final java.lang.ref.ReferenceQueue<JexlCache<?,​?>> queue
    • Constructor Detail

      • MetaCache

        MetaCache​(java.util.function.IntFunction<JexlCache<?,​?>> factory)
        Constructs a MetaCache with the given cache factory.
        Parameters:
        factory - The factory function to create JexlCache instances given a capacity.
    • Method Detail

      • createCache

        <K,​V> JexlCache<K,​V> createCache​(int capacity)
      • clearCaches

        void clearCaches()
        Clears all caches tracked by this MetaCache.
      • cleanUp

        private int cleanUp()
        Cleans up all references whose referent (the cache) has been garbage collected.

        This method must be invoked while holding the lock on references.

        Returns:
        The remaining number of caches.
      • size

        int size()
        Gets the number of live caches currently tracked by this MetaCache.

        Any cache that is no longer strongly reference will get removed from the tracked set.

        Returns:
        The number of caches.