Package org.h2.util

Class CacheLRU

  • All Implemented Interfaces:
    Cache

    public class CacheLRU
    extends java.lang.Object
    implements Cache
    A cache implementation based on the last recently used (LRU) algorithm.
    • Field Detail

      • fifo

        private final boolean fifo
        Use First-In-First-Out (don't move recently used items to the front of the queue).
      • mask

        private final int mask
      • recordCount

        private int recordCount
      • len

        private final int len
        The number of cache buckets.
      • maxMemory

        private long maxMemory
        The maximum memory, in words (4 bytes each).
      • memory

        private long memory
        The current memory used in this cache, in words (4 bytes each).
    • Constructor Detail

      • CacheLRU

        CacheLRU​(CacheWriter writer,
                 int maxMemoryKb,
                 boolean fifo)
    • Method Detail

      • getCache

        public static Cache getCache​(CacheWriter writer,
                                     java.lang.String cacheType,
                                     int cacheSize)
        Create a cache of the given type and size.
        Parameters:
        writer - the cache writer
        cacheType - the cache type
        cacheSize - the size
        Returns:
        the cache object
      • clear

        public void clear()
        Description copied from interface: Cache
        Clear the cache.
        Specified by:
        clear in interface Cache
      • put

        public void put​(CacheObject rec)
        Description copied from interface: Cache
        Add an element to the cache. Other items may fall out of the cache because of this. It is not allowed to add the same record twice.
        Specified by:
        put in interface Cache
        Parameters:
        rec - the object
      • update

        public CacheObject update​(int pos,
                                  CacheObject rec)
        Description copied from interface: Cache
        Update an element in the cache. This will move the item to the front of the list.
        Specified by:
        update in interface Cache
        Parameters:
        pos - the unique key of the element
        rec - the element
        Returns:
        the element
      • removeOldIfRequired

        private void removeOldIfRequired()
      • removeOld

        private void removeOld()
      • addToFront

        private void addToFront​(CacheObject rec)
      • removeFromLinkedList

        private void removeFromLinkedList​(CacheObject rec)
      • remove

        public boolean remove​(int pos)
        Description copied from interface: Cache
        Remove an object from the cache.
        Specified by:
        remove in interface Cache
        Parameters:
        pos - the unique key of the element
        Returns:
        true if the key was in the cache
      • find

        public CacheObject find​(int pos)
        Description copied from interface: Cache
        Get an element from the cache if it is available. This will not move the item to the front of the list.
        Specified by:
        find in interface Cache
        Parameters:
        pos - the unique key of the element
        Returns:
        the element or null
      • get

        public CacheObject get​(int pos)
        Description copied from interface: Cache
        Get an element in the cache if it is available. This will move the item to the front of the list.
        Specified by:
        get in interface Cache
        Parameters:
        pos - the unique key of the element
        Returns:
        the element or null
      • getAllChanged

        public java.util.ArrayList<CacheObject> getAllChanged()
        Description copied from interface: Cache
        Get all objects in the cache that have been changed.
        Specified by:
        getAllChanged in interface Cache
        Returns:
        the list of objects
      • setMaxMemory

        public void setMaxMemory​(int maxKb)
        Description copied from interface: Cache
        Set the maximum memory to be used by this cache.
        Specified by:
        setMaxMemory in interface Cache
        Parameters:
        maxKb - the maximum size in KB
      • getMaxMemory

        public int getMaxMemory()
        Description copied from interface: Cache
        Get the maximum memory to be used.
        Specified by:
        getMaxMemory in interface Cache
        Returns:
        the maximum size in KB
      • getMemory

        public int getMemory()
        Description copied from interface: Cache
        Get the used size in KB.
        Specified by:
        getMemory in interface Cache
        Returns:
        the current size in KB