@supuwoerc/toolkit
    Preparing search index...

    Class LFUCacheWithTTL<K, V>

    带有 TTL(生存时间)的 LFU 缓存类 LFU (Least Frequently Used) cache with TTL (Time To Live) support.

    基于最少使用次数进行淘汰,并为每个元素设置生存时间。 Evicts items with the lowest access frequency, each item having a time-to-live.

    Type Parameters

    • K extends keyof any

      缓存键的类型 / Type of cache keys

    • V

      缓存值的类型 / Type of cache values

    Index

    Constructors

    Accessors

    Methods

    Constructors

    • 创建带 TTL 的 LFU 缓存实例 Create an LFU cache instance with TTL support

      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

      • ttl: number

        生存时间(毫秒),必须是正整数 / Time-to-live in milliseconds, must be a positive integer

      Returns LFUCacheWithTTL<K, V>

      当 capacity 或 ttl 不是正整数时抛出 / Thrown when capacity or ttl 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

      获取操作会增加该键的访问频率并重置其 TTL。 The get operation increases the access frequency and resets the TTL of this key.

      Parameters

      • key: K

        要获取的键 / Key to get

      Returns V | undefined

      如果键存在且未过期返回对应的值,否则返回 undefined / Returns the value if key exists and is not expired, otherwise undefined

    • 检查缓存中是否存在指定的键且未过期 Check if the specified key exists in the cache and is not expired

      Parameters

      • key: K

        要检查的键 / Key to check

      Returns boolean

      如果键存在且未过期返回 true,否则返回 false / Returns true if key exists and is not expired, false otherwise

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

      • 如果键已存在:更新其值(连同 TTL)并提升访问频率
      • 如果键不存在:在容量不足时淘汰一个最少使用项后再插入新项
      • If the key exists: update its value (with TTL) 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 LFUCacheWithTTL<K, V>

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