npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@iocore/cache-dispenser-memory

v1.0.1

Published

Default Monorepo template

Readme

@iocore/cache-dispenser-memory

npm version

注意: 包名可能是 memory 的拼写错误。

IoCore 的内存缓存分发器。

这是 @iocore/cache 模块 CacheDispenser 抽象类的一个具体实现,将缓存数据存储在 Node.js 进程的内存中。支持设置缓存过期时间。

安装

npm install @iocore/cache-dispenser-memory @iocore/cache @iocore/component --save
# or
yarn add @iocore/cache-dispenser-memory @iocore/cache @iocore/component

依赖

  • @iocore/cache: 提供 CacheDispenser 抽象类。
  • @iocore/component: IoCore 核心组件系统。

配置

该模块没有外部配置项。

使用

memoryCacheDispenser 类是一个 IoCore Application 组件(因为它有 initializeterminate 来管理过期检查定时器),通常作为其他需要缓存的组件(如 @iocore/configs)的依赖项注入。

import { Application } from '@iocore/component';
import { CacheDispenser } from '@iocore/cache';
import memoryCacheDispenser from '@iocore/cache-dispenser-memory';

// 模拟一个需要缓存的服务
@Application.Injectable()
class MyCachedService extends Component {
  // 注入内存缓存分发器
  @Application.Inject(memoryCacheDispenser)
  private cache: CacheDispenser<string>; // 类型参数 R 定义了缓存值的类型

  private async expensiveOperation(userId: number): Promise<string> {
    console.log(`Performing expensive operation for user ${userId}...`);
    await new Promise(res => setTimeout(res, 100)); // 模拟耗时
    return `Data for user ${userId}`;
  }

  async getUserData(userId: number): Promise<string> {
    const cacheKey = `user:${userId}:data`;

    if (await this.cache.has(cacheKey)) {
      console.log(`Cache hit for key: ${cacheKey}`);
      const ttl = await this.cache.ttl(cacheKey);
      console.log(`TTL: ${ttl} seconds`);
      return this.cache.get(cacheKey);
    }

    console.log(`Cache miss for key: ${cacheKey}`);
    const data = await this.expensiveOperation(userId);
    // 缓存数据 60 秒
    await this.cache.set(cacheKey, data, 60);
    return data;
  }

  async clearUserData(userId: number): Promise<void> {
    const cacheKey = `user:${userId}:data`;
    await this.cache.delete(cacheKey);
    console.log(`Cache cleared for key: ${cacheKey}`);
  }
}

// 启动应用
// 确保 memoryCacheDispenser 被 IoCore 管理
@Application.Inject(memoryCacheDispenser, MyCachedService)
class BootApp extends Application {
  @Application.Inject(MyCachedService)
  service: MyCachedService;

  async main() {
    console.log('--- First call ---');
    await this.service.getUserData(123);
    console.log('\n--- Second call (should hit cache) ---');
    await this.service.getUserData(123);

    console.log('\n--- Clearing cache ---');
    await this.service.clearUserData(123);

    console.log('\n--- Third call (should miss cache) ---');
    await this.service.getUserData(123);
  }

  // Application 抽象类要求实现 initialize 和 terminate
  initialize() {}
  terminate() {}
}

Application.start(BootApp);

memoryCacheDispenser<R = any>

继承自 CacheDispenser

  • initialize(): void: 启动一个 setInterval 定时器 (每秒检查一次),用于清理过期的缓存条目。IoCore 会自动调用。
  • terminate(): void: 清除过期检查定时器。IoCore 会自动调用。
  • set(key: string, value: R, time?: number): Promise<R>: 设置缓存。
    • key: 缓存键。
    • value: 要缓存的值。
    • time (可选): 过期时间(秒)。如果为 0 或不提供,则永不过期。
    • 返回 value
  • has(key: string): Promise<boolean>: 检查缓存键是否存在(且未过期)。
  • get(key: string): Promise<R>: 获取缓存值。如果键不存在或已过期,行为未定义(可能返回 undefined)。应先用 has 检查。
  • ttl(key: string): Promise<number>: 获取缓存键的剩余生存时间(秒)。如果键不存在或永不过期,返回 undefined0 (根据实现,当前实现对于永不过期返回 0,不存在可能出错)。
  • delete(key: string): Promise<void>: 删除缓存键。

贡献

欢迎提交 Pull Request。对于重大更改,请先开一个 Issue 来讨论您想要更改的内容。

许可证

MIT