@akashbabu/lfu-cache

中文文档
1.1.0发布于 2022/09/09MITTypeScriptESM + CJS近 30 天下载 2356

LFU cache implementation with a complexity of O(1) for all transactions

原始使用文档
文档目录4 节
  1. Installation
  2. Example
  3. API Documentation
  4. Contribution

lfu-cache Coverage Status Build Status Maintainability

LFU cache implementation with a complexity of O(1) for all transactions, as described in the paper "An O(1) algorithm for implementing the LFU cache eviction scheme" by K. Shah, A. Mitra and D. Matani

What's the motivation behind creating this library(inspite of other currently existing LFU cache libraries out there) ?

  • Most importantly it's the Typescript support
  • Implementation of O(1) for all transactions (even though I din create the algorithm for it 😜)
  • Maintainable code

If you ever wanna peek into the source code, I would suggest you to read the algorithm in above mentioned paper first and then look into code, for better understanding or suggestions.

Installation

npm i @akashbabu/lfu-cache -S

Example

import LFUCache from '@akashbabu/lfu-cache';

const lfu = new LFUCache<string>();

lfu.set('foo', 'bar');
console.log(lfu.size) // => 1

console.log(lfu.get('foo')) // => bar
console.log(lfu.peek('foo')) // => bar

lfu.delete('foo')
console.log(lfu.get('foo')) // => undefined


lfu.set('item1', 'foo')
lfu.set('item1', 'bar')
console.log(lfu.get('item1')) // => bar

lfu.clear();
console.log(lfu.size) // => 0

lfu.set('item1', 'foo')
lfu.set('item2', 'bar')
lfu.set('item3', 'baz')

lfu.forEach(([key, val], i) => {
  console.log(`${i + 1})`, key, val)
})
// => 1) item1 foo
// => 2) item2 bar
// => 3) item3 baz

console.log(lfu.map<string>(([key, val], i) => `${i + 1}) ${key} -> ${val}`))
// => ["1) item1 foo", "2) item2 bar", "3) item3 baz"]

API Documentation

new LFUCache<T>(options? = {})

Instantiates LFU cache

T -> Type of the value being stored

ParamDescription
options.max (optional)Specifies the maximum number item to accumulate in the cache. Defaults to 100
options.evictCount (optional)Specifies the number of items to be evicted once the cache is full. If options.max is specified and options.evictCount is not specified, then it defaults to 10% of options.max else it defaults to 1
options.maxAge (optional)If specified, then lazily evicts the keys after the specified maxAge. Please note that this is in milliseconds (ms)

.size

Returns the total number of items in the cache.
This is a readonly property.

.set(key: string, value: T): void

Caches the given key-value pair and evicts LFU keys if the cache is full.

.get(key: string): T | undefined

Returns the cached value if present or if NOT expired, else returns undefined

.delete(key: string): boolean

Removes the given key from cache. Returns true if the given key was present and it has been removed successfully else returns false

.peek(key: string): T | undefined

Returns the cached value for the given key without increasing the access frequency of the given key. Returns undefined if the given keys if NOT present in the cache

.forEach(cb: ([key: string, val: T], i: number) => void): void

Iterates through the entire cache

.map(cb: ([key: string, val: T], i: number) => U): U[]

Iterates through the entire cache and returns the resultant array

U -> Denotes the return type when cb is called

.clear(): void

Clears all the data in the cache

Contribution

We beleive there is nothing we've left out in v1.0.0, but there is always scope for development, hence if you find any bug or have any suggestion to make, then please proceed to github and raise an Issue or PR for the same.

版本与发布记录

@akashbabu/lfu-cache 共发布 4 个版本,首次发布于 2019/12/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2022/09/094 年前
1.0.22020/01/296 年前
1.0.12019/12/296 年前
1.0.02019/12/226 年前

关于 @akashbabu/lfu-cache 的常见问题

@akashbabu/lfu-cache 是什么?

LFU cache implementation with a complexity of O(1) for all transactions

如何安装 @akashbabu/lfu-cache?

在项目目录运行 npm install @akashbabu/lfu-cache,或使用 pnpm add / yarn add / bun add。

@akashbabu/lfu-cache 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@akashbabu/lfu-cache 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@akashbabu/lfu-cache 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@akashbabu/lfu-cache 可以商用吗?

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

@akashbabu/lfu-cache 的下载量有多少?

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

@akashbabu/lfu-cache 还在维护吗?

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