DecayCache class that extends BasicCache to implement a decay-based cache eviction policy. Cache items have a score which decays over time. The score increases with each access, and the item's position in the cache is adjusted based on its score relative to other items.

Type Parameters

  • T

    The type of items that the cache will store.

Hierarchy

  • BasicCache<T>
    • DecayCache

Implements

Constructors

  • Creates an instance of the DecayCache class with a specified capacity and half-life.

    Type Parameters

    • T

    Parameters

    • capacity: number

      The maximum number of items the cache can hold.

    • Optional halfLife: number = 300000

      The time (in milliseconds) after which the score of a cache item is halved.

    Returns DecayCache<T>

Properties

halfLife: number
head: CacheItem<T>
tail: CacheItem<T>

Methods

  • Parameters

    • item: CacheItem<T>

    Returns void

  • Parameters

    • item: CacheItem<T>

      Calculate decay of an item.

    • ts: number

      Timestamp of the time when this item is calculated.

    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.

  • A special method for DecayCache class.

    Parameters

    • key: string

      The key to be hit

    Returns number

    The decay score of the key.

  • 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