@supuwoerc/toolkit
    Preparing search index...

    Class LRUCache<K, V>

    LRU (Least Recently Used) 缓存类 LRU (Least Recently Used) Cache Class

    基于最近最少使用原则的缓存实现,当缓存达到容量上限时, 会自动移除最久未被访问的项。

    Implementation of cache based on Least Recently Used principle. When the cache reaches its capacity limit, it automatically removes the least recently accessed item.

    Type Parameters

    • K extends keyof any
    • V
    Index

    Constructors

    Accessors

    Methods

    Constructors

    • 构造函数,初始化 LRU 缓存 Constructor, initializes LRU cache

      Type Parameters

      • K extends string | number | symbol
      • V

      Parameters

      • capacity: number

        缓存的最大容量,必须是正整数 Maximum capacity of the cache, must be a positive integer

      Returns LRUCache<K, V>

      如果 capacity 不是整数,抛出 'cache capacity must be integer' If capacity is not an integer, throws 'cache capacity must be integer'

      如果 capacity <= 0,抛出 'cache capacity must be greater than 0' If capacity <= 0, throws 'cache capacity must be greater than 0'

    Accessors

    • get capacity(): number

      获取缓存的容量限制 Get the capacity limit of the cache

      Returns number

      缓存的最大容量 Maximum capacity of the cache

    • get size(): number

      获取当前缓存的大小(已存储的项数) Get the current size of the cache (number of stored items)

      Returns number

      缓存中当前存储的项数 Number of items currently stored in the cache

    Methods

    • 清空缓存中的所有项 Clear all items from the cache

      Returns void

    • 删除缓存中指定的键 Delete the specified key from the cache

      Parameters

      • key: K

        要删除的键 Key to delete

      Returns boolean

      如果键存在并被删除返回 true,否则返回 false Returns true if key existed and was deleted, false otherwise

    • 从缓存中获取指定键的值 Get the value of the specified key from the cache

      获取操作会使该键成为最近使用的项(LRU 特性)。

      The get operation makes this key the most recently used item (LRU feature).

      Parameters

      • key: K

        要获取的键 Key to get

      Returns V | undefined

      如果键存在返回对应的值,否则返回 undefined Returns the corresponding value if key exists, undefined otherwise

    • 检查缓存中是否存在指定的键 Check if the specified key exists in the cache

      Parameters

      • key: K

        要检查的键 Key to check

      Returns boolean

      如果键存在返回 true,否则返回 false Returns true if key exists, false otherwise

    • 向缓存中添加或更新键值对 Add or update a key-value pair in the cache

      如果键已存在,会先删除旧记录再添加新记录, 这样能确保该键成为最近使用的项。

      If the key already exists, it will delete the old record before adding the new one, ensuring this key becomes the most recently used item.

      如果添加后缓存大小超过容量限制,会自动移除最久未使用的项。

      If the cache size exceeds capacity after adding, it automatically removes the least recently used item.

      Parameters

      • key: K

        要设置的键 Key to set

      • value: V

        要设置的值 Value to set

      Returns LRUCache<K, V>