LRUCache class that extends BasicCache to implement the Least Recently Used (LRU) cache eviction policy. Items that are accessed recently are moved to the front, and the least recently used items are removed when the cache reaches its capacity.

Type Parameters

  • T

    The type of items that the cache will store.

Hierarchy

  • BasicCache<T>
    • LRUCache

Implements

Constructors

  • Creates an instance of the LRUCache class with a specified capacity.

    Type Parameters

    • T

    Parameters

    • capacity: number

      The maximum number of items the cache can hold.

    Returns LRUCache<T>

Properties

head: CacheItem<T>
tail: CacheItem<T>

Methods

  • Parameters

    • item: CacheItem<T>

    Returns void

  • Returns the maximum capacity of the cache.

    Returns number

    The maximum number of items the cache can hold.

  • Returns the current number of items in the cache.

    Returns number

    The number of items currently stored in the cache.

  • Removes an item from the cache based on its key.

    Parameters

    • key: string

      The key associated with the item to be removed.

    Returns void

  • Retrieves an item from the cache based on its key.

    Parameters

    • key: string

      The key associated with the item to be retrieved.

    Returns undefined | T

    The value of the item if it exists, or undefined if it doesn't.

  • Protected function that allow caller to query a cache item by its key.

    Parameters

    • key: string

      The key for querying.

    Returns undefined | CacheItem<T>

    The cache item if it is found, otherwise returns undefined.

  • Parameters

    • item: CacheItem<T>

    Returns void

  • Protected method that insert a cache item just after another item in the linked list.

    Parameters

    • item: CacheItem<T>

      The item which already is in the list. The new item will be inserted at the position right after this item.

    • newItem: CacheItem<T>

      The item that will be inserted into the list.

    Returns void

  • Adds an item to the cache or updates an existing item's value. If the cache reaches its capacity, it evicts an item based on the specific cache's eviction policy.

    Parameters

    • key: string

      The key associated with the item.

    • value: T

      The value to be stored in the cache.

    • Optional expiry: number

      Optional expiry time for the item.

    Returns void

  • Protected method that remove a cache item from the linked list.

    Parameters

    • item: CacheItem<T>

      Item being to be removed.

    Returns void

Generated using TypeDoc