@pengzhanbo/utils - v3.7.0
    Preparing search index...

    Class LRUCache<K, V>

    LRU Cache

    const cache = new LRUCache({ maxSize: 10 })
    cache.set('a', 1)
    cache.set('b', 2)
    cache.get('a') // 1
    cache.get('b') // 2
    cache.size // 2
    cache.maxSize // 10
    cache.has('a') // true - item exists 项目是否存在
    cache.peek('a') // 1 - (not marked as recently used) (不标记为最近使用)
    cache.evict(1) // Delete the least recently used item. 删除最近最少使用的一项

    Type Parameters

    • K

      item key type 项目的键的类型

    • V

      item value type 项目的值的类型

    Hierarchy

    Implements

    Index

    Constructors

    Properties

    "[species]": MapConstructor

    Accessors

    • get maxAge(): number

      The maximum age of items in the cache. 缓存中项目的最大年龄(毫秒)。

      Returns number

    • get maxSize(): number

      The maximum size of the cache. 缓存的最大大小。

      Returns number

    Methods

    • Delete an item.

      删除一个项目。

      Parameters

      • key: K

        item key 项目的键

      Returns boolean

      boolean 是否成功删除项目

    • Evict the least recently used items from the cache.

      It will always keep at least one item in the cache.

      从缓存中移除最近最少使用的项目。

      缓存中始终至少保留一个项目。

      Parameters

      • Optionalcount: number

        The number of items to evict. Defaults to 1. 移除的项目数量。默认值为 1。

      Returns void

    • Get the remaining time to live (in milliseconds) for the given item, or undefined when the item is not in the cache.

      • Does not mark the item as recently used.
      • Does not trigger lazy expiration or remove the entry when it is expired.
      • Returns Infinity if the item has no expiration.
      • May return a negative number if the item is already expired but not yet lazily removed.

      获取指定项目的剩余存活时间(以毫秒为单位),如果项目不在缓存中则返回 undefined

      • 不会将该项目标记为最近使用。
      • 不会触发惰性过期,也不会在项目过期时移除该条目。
      • 如果项目没有过期时间,则返回 Infinity
      • 如果项目已过期但尚未被惰性移除,可能会返回负数。

      Parameters

      • key: K

        item key 项目的键

      Returns number | undefined

      Remaining time to live in milliseconds when set, Infinity when there is no expiration, or undefined when the item does not exist. 设置后剩余的存活时间(毫秒),若无过期时间则为Infinity,若项目不存在则为undefined

    • Get an item. Returns the value if the item exists, or undefined if it does not.

      获取一个项目。如果项目存在,则返回项目的值;否则返回 undefined

      Parameters

      • key: K

        item key 项目的键

      Returns V | undefined

      item value 项目的值

    • Returns a specified element from the Map object. If no element is associated with the specified key, a new element with the value defaultValue will be inserted into the Map and returned.

      Parameters

      • key: K
      • defaultValue: V

      Returns V

      The element associated with the specified key, which will be defaultValue if no element previously existed.

    • Returns a specified element from the Map object. If no element is associated with the specified key, the result of passing the specified key to the callback function will be inserted into the Map and returned.

      Parameters

      • key: K
      • callback: (key: K) => V

      Returns V

      The element associated with the specific key, which will be the newly computed value if no element previously existed.

    • Check if an item exists.

      检查项目是否存在。

      Parameters

      • key: K

        item key 项目的键

      Returns boolean

      boolean 项目是否存在

    • Get an item without marking it as recently used.

      获取一个项目,不将其标记为最近使用。

      Parameters

      • key: K

        item key 项目的键

      Returns V | undefined

      item value 项目的值

    • Update the maxSize in-place, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee. Useful for on-the-fly tuning of cache sizes in live systems.

      就地更新 maxSize,必要时丢弃项目。插入顺序基本保留,但这不是一个强保证。 适用于在线系统中动态调整缓存大小。

      Parameters

      • Optionalsize: number

        cache size 缓存大小

      Returns void

    • Set an item. Returns the instance.

      Individual expiration of an item can be specified with the maxAge option. If not specified, the global maxAge value will be used in case it is specified in the constructor; otherwise the item will never expire.

      设置一个项目。返回实例。

      可以使用 maxAge 选项指定单个项目的过期时间。 如果未指定,当构造函数中指定了全局 maxAge 值时,将使用该值;否则项目永不过期。

      Parameters

      • key: K

        item key 项目的键

      • value: V

        item value 项目的值

      • Optionaloptions: LRUCacheSetOptions

        item options 项目的选项

        LRU Cache Set Options

        LRU 缓存 Set 选项

        • OptionalmaxAge?: number

          The maximum number of milliseconds an item should remain in the cache.

          项目在缓存中保留的最大毫秒数。

          Infinity
          

      Returns this

      instance 实例

    • Groups members of an iterable according to the return value of the passed callback.

      Type Parameters

      • K
      • T

      Parameters

      • items: Iterable<T>

        An iterable.

      • keySelector: (item: T, index: number) => K

        A callback which will be invoked for each item in items.

      Returns Map<K, T[]>