@supuwoerc/toolkit
    Preparing search index...

    Class LFUCache<K, V>

    LFU (Least Frequently Used) 缓存类 LFU (Least Frequently Used) Cache Class

    基于最少使用次数进行淘汰的缓存实现。 Cache implementation that evicts items with the lowest access frequency.

    Type Parameters

    • K extends keyof any

      缓存键的类型 / Type of cache keys

    • V

      缓存值的类型 / Type of cache values

    Index

    Constructors

    Accessors

    Methods

    Constructors

    • 创建 LFU 缓存实例 Create an LFU cache instance

      Type Parameters

      • K extends string | number | symbol

        缓存键的类型 / Type of cache keys

      • V

        缓存值的类型 / Type of cache values

      Parameters

      • capacity: number

        缓存容量,必须是正整数 / Cache capacity, must be a positive integer

      Returns LFUCache<K, V>

      当容量不是正整数时抛出 / Thrown when capacity is not a positive integer

    Accessors

    • get capacity(): number

      获取缓存的最大容量 Get the maximum capacity of the cache

      Returns number

      缓存最大容量 / Maximum cache capacity

    • get size(): number

      获取当前缓存中的元素数量 Get the number of items currently stored in the cache

      Returns number

      当前缓存大小 / Current cache size

    Methods

    • 清空缓存中的所有数据 Clear all data in the cache

      Returns void

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

      删除成功时会从对应频次链表中移除节点,并在需要时更新最小频次。 When deletion succeeds, the node is removed from its frequency list and min frequency may be updated.

      Parameters

      • key: K

        要删除的键 / Key to delete

      Returns boolean

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

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

      获取操作会增加该键的访问频率(LFU 特性)。 The get operation increases the access frequency of this key (LFU behavior).

      Parameters

      • key: K

        要获取的键 / Key to get

      Returns V | undefined

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

    • 检查缓存中是否存在指定的键 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 exists: update its value and increase its frequency
      • If the key does not exist: evict one least frequently used item when full, then insert

      Parameters

      • key: K

        要设置的键 / Key to set

      • value: V

        要设置的值 / Value to set

      Returns LFUCache<K, V>

      当前缓存实例(支持链式调用)/ Current cache instance (for chaining)