@brokerloop/ttlcache

中文文档
3.2.3发布于 2020/05/25MIT近 30 天下载 5.6万

Evented LRU TTL cache for NodeJS and browsers.

原始使用文档
文档目录7 节
  1. Installation
  2. Usage (TypeScript)
  3. Options
  4. Properties
  5. Methods
  6. Events (via Signal )
  7. License

@brokerloop/ttlcache

Build Status Coverage Status Dependency Status npm version

Evented LRU TTL cache for Node.js and browsers

  • A cache with a configurable number of expiring entries.
  • Reading an expired entry removes it from the cache.
  • When the cache is full, Least-Recently-Used entries are evicted first.

Installation

npm install @brokerloop/ttlcache --save

Usage (TypeScript)

import { TTLCache } from '@brokerloop/ttlcache';

const cache = new TTLCache<string, number>({
  ttl:   5000,
  max:   10,
  clock: Date
});

cache.set('a', 123);
cache.has('a');      // true
cache.get('a');      // 123
cache.get('b');      // undefined
cache.delete('a');   // 123

cache.empty.on(() => {
  // cache is empty
});

cache.full.on(() => {
  // cache is full
});

cache.evict.on(({ key, val }) => {
  // entry evicted
});

Options

{
  ttl:   1000,         // default entry TTL in ms
  max:   Infinity,     // max number of entries in cache
  clock: Date as Clock // cache-relative clock
}

clock

By default, the cache uses Date.now() to expire entries. This works while the system date/time do not change. You can provide your own implementation of the Clock interface:

interface Clock {
  now:         () => number; // must be monotonically increasing
  [_: string]: any;
}
const clock = (() => {
  let time = 0;

  const clock: Clock = {
    now:  () => time,
    pass: (ms: number) => time += ms
  };

  return clock;
})();

Properties

size

Returns the size of the cache, including expired entries. Run cleanup() first to obtain valid cache size.

Methods

keys(): Iterator<K>

Returns an iterator over valid cache entry keys, from newest to oldest. Expired entries are not evicted.

values(): Iterator<V>

Returns an iterator over valid cache entry values, from newest to oldest. Expired entries are not evicted.

entries(): Iterator<{ key: K, val: V }>

Returns an iterator over valid cache entries, from newest to oldest. Expired entries are not evicted.

has(key: K): boolean

Checks if key exists in the cache. Does not evict the entry if expired.

get(key: K): V|undefined

Finds an entry by the given key. Returns undefined if not found or if the entry is expired, also evicting it.

set(key: K, val: V): void

Creates an entry at key, evicting the cache's LRU entry if the cache is full. If an expired entry already exists at key, its LRU-age is refreshed but it is not evicted.

delete(key: K): V|undefined

Finds and removes an entry at key. Returns the entry value if it was removed, or undefined otherwise.

cleanup(opts = { emit: true }): void

Evicts all expired entries from the cache. Pass emit: false to not emit evict events.

resize(max: number, opts = { emit: true }): void

Resizes the cache to the given max size. When growing, no entries are evicted. When shrinking, entries are evicted as needed, by oldest LRU-age, until the new max is reached. Pass emit: false to not emit evict events.

clear(): void

Empties the cache, removing all entries, without firing signals.

Events (via Signal)

empty

Signal fired after cache becomes empty. Does not fire on clear().

full

Signal fired after cache becomes full.

evict

Signal fired after a cache entry is evicted. The evicted entry { key: K, val: V } is passed as an argument. Does not fire on delete() and clear().

License

LICENSE

版本与发布记录

@brokerloop/ttlcache 共发布 29 个版本,首次发布于 2018/06/28;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.2.3latest2020/05/256 年前
3.2.22020/04/076 年前
3.2.12020/02/266 年前
3.2.02019/12/266 年前
3.1.12019/12/086 年前
3.1.02019/12/086 年前

关于 @brokerloop/ttlcache 的常见问题

@brokerloop/ttlcache 是什么?

Evented LRU TTL cache for NodeJS and browsers.

如何安装 @brokerloop/ttlcache?

在项目目录运行 npm install @brokerloop/ttlcache,或使用 pnpm add / yarn add / bun add。

@brokerloop/ttlcache 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/brokerloop__ttlcache 是否存在。

@brokerloop/ttlcache 可以商用吗?

@brokerloop/ttlcache 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brokerloop/ttlcache 的下载量有多少?

根据 npm 官方下载统计,@brokerloop/ttlcache 在 2026/10/09 – 2026/11/07 这 30 天内被下载 56,329 次,日均约 1,878 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brokerloop/ttlcache 还在维护吗?

最新版本 3.2.3 发布于 2020/05/25(6 年前);至今共发布 29 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/06/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。