@supuwoerc/toolkit
    Preparing search index...

    Class LRUCacheWithTTL<K, V>

    LRU (Least Recently Used) cache with TTL (Time To Live) support. 支持TTL(生存时间)的LRU(最近最少使用)缓存。

    Type Parameters

    • K extends keyof any

      The type of keys in the cache. 缓存键的类型。

    • V

      The type of values in the cache. 缓存值的类型。

    Index

    Constructors

    • Creates an instance of LRUCacheWithTTL. 创建LRUCacheWithTTL的实例。

      Type Parameters

      • K extends string | number | symbol

        The type of keys in the cache. 缓存键的类型。

      • V

        The type of values in the cache. 缓存值的类型。

      Parameters

      • capacity: number

        Maximum number of items the cache can hold. Must be a positive integer. 缓存可容纳的最大项目数。必须为正整数。

      • ttl: number

        Time to live for cache items in milliseconds. Must be a positive integer. 缓存项的生存时间(毫秒)。必须为正整数。

      Returns LRUCacheWithTTL<K, V>

      If capacity or ttl is not a positive integer. 如果capacity或ttl不是正整数。

    Accessors

    • get capacity(): number

      Gets the maximum capacity of the cache. 获取缓存的最大容量。

      Returns number

      The cache capacity. 缓存容量。

    • get size(): number

      Gets the current number of items in the cache. 获取缓存中当前的项目数量。

      Returns number

      The number of items in the cache. 缓存中的项目数量。

    Methods

    • Removes all expired items from the cache. 从缓存中移除所有过期的项目。

      Returns number

      The number of items removed. 移除的项目数量。

    • Clears all items from the cache. 清除缓存中的所有项目。

      Returns void

    • Deletes a specific key from the cache. 从缓存中删除特定的键。

      Parameters

      • key: K

        The key to delete. 要删除的键。

      Returns boolean

      True if the key was deleted, false if it didn't exist. 如果键被删除则返回true,如果键不存在则返回false。

    • Gets the value associated with a key, updating its access time. 获取与键关联的值,并更新其访问时间。

      Parameters

      • key: K

        The key to retrieve. 要检索的键。

      Returns V | undefined

      The value if found and not expired, undefined otherwise. 如果找到且未过期则返回值,否则返回undefined。

    • Checks if a key exists in the cache and is not expired. 检查键是否存在于缓存中且未过期。

      Parameters

      • key: K

        The key to check. 要检查的键。

      Returns boolean

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

    • Sets a key-value pair in the cache. 在缓存中设置键值对。

      Parameters

      • key: K

        The key to set. 要设置的键。

      • value: V

        The value to associate with the key. 与键关联的值。

      Returns LRUCacheWithTTL<K, V>