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 🙏

© 2025 – Pkg Stats / Ryan Hefner

msh-multi-storage

v0.0.1

Published

支持内存,Redis等多种形式的存储器,可以用来作为缓存,支持多级缓存

Readme

Multi-Storage

支持多个存取器的缓存系统,一个使用包括内存,Redis在内的多种存储方案作为一个缓存系统.支持内存缓存和Redis缓存,提供灵活的存储策略和发布订阅功能。

特性

  • 多级缓存策略,支持内存、Redis等多种存储方式
  • 支持TTL(过期时间)设置
  • 提供发布订阅功能
  • 支持多种Redis客户端实现(node-redis和ioredis)
  • 完全使用TypeScript编写,提供完整类型定义
  • 模块化设计,易于扩展

安装

npm install msh-multi-storage
# 或者使用yarn
yarn add msh-multi-storage
# 或者使用pnpm
pnpm add msh-multi-storage

基本用法

创建简单的内存缓存

import { MemoryStorage } from 'msh-multi-storage';

async function example() {
  const storage = new MemoryStorage();
  
  // 设置缓存
  await storage.set('key1', 'value1');
  
  // 获取缓存
  const value = await storage.get('key1');
  console.log(value); // 输出: value1
  
  // 设置带过期时间的缓存(单位:秒)
  await storage.set('key2', 'value2', 60); // 60秒后过期
  
  // 检查键是否存在
  const exists = await storage.has('key1');
  console.log(exists); // 输出: true
  
  // 删除缓存
  await storage.del('key1');
  
  // 清空所有缓存
  await storage.clear();
}

使用多级缓存

import { MultiLevelStorageProvider, NodeCacheStorage, NodeRedisStorage } from 'msh-multi-storage';

async function example() {
  // 创建多级缓存提供者
  const provider = new MultiLevelStorageProvider(NodeCacheStorage);
  
  // 添加Redis作为第二级缓存
  provider.addLevel(NodeRedisStorage, { 
    host: 'localhost', 
    port: 6379 
  });
  
  // 获取主存储实例
  const storage = provider.getStore();
  
  // 设置缓存(会自动同步到所有级别)
  await storage.set('user:1', { id: 1, name: 'John' });
  
  // 获取缓存(会按顺序查找各级缓存)
  const user = await storage.get('user:1');
  console.log(user); // 输出: { id: 1, name: 'John' }
}

使用发布订阅功能

import { IORedisPubsubStorage } from 'msh-multi-storage';

async function example() {
  const pubsub = new IORedisPubsubStorage({
    host: 'localhost',
    port: 6379
  });

  // 订阅频道
  await pubsub.subscribe('news', (message) => {
    console.log('收到消息:', message);
  });

  // 发布消息
  await pubsub.publish('news', '这是一条新闻消息');

  // 取消订阅
  await pubsub.unsubscribe('news');

  // 关闭连接
  await pubsub.close();
}

示例代码

项目包含多个示例代码,展示了不同功能的使用方法:

单元测试

项目包含完整的单元测试,可以参考这些测试来了解各个组件的用法:

存储测试

提供者测试

API参考

完整的API文档可以在以下位置找到:

主要接口

  • IStorage - 基础存储接口,定义了缓存存储的基本操作
  • IRemoteStorage - 远程存储接口,扩展基础存储接口,添加连接管理功能
  • IPubSub - 发布订阅接口,定义了发布订阅的基本操作
  • IRemotePubSubStorage - 远程发布订阅存储接口,结合远程存储和发布订阅功能

主要实现类

  • MemoryStorage - 内存存储实现
  • MemoryPubsubStorage - 内存发布订阅存储实现
  • NodeCacheStorage - 基于node-cache的存储实现
  • NodeRedisStorage - 基于node-redis的远程存储实现
  • NodeRedisPubsubStorage - 基于node-redis的发布订阅存储实现
  • IORedisStorage - 基于ioredis的远程存储实现
  • IORedisPubsubStorage - 基于ioredis的发布订阅存储实现

提供者类

  • StorageProvider - 基础存储提供者
  • PubsubStorageProvider - 发布订阅存储提供者
  • MultiLevelStorageProvider - 多级存储提供者

许可证

MIT