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

mm_cache

v1.4.8

Published

一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的API接口,实现无缝切换和兼容

Readme

mm_cache

项目信息

简介

mm_cache是一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的API接口,实现无缝切换和兼容。本模块作为上层抽象,整合了多种缓存实现,让开发者可以使用统一的方式操作不同的缓存系统。

特点

  • 统一API接口:为Redis、MongoDB、CacheBase、Memory提供完全一致的接口
  • 无缝切换:支持在不同缓存实现之间快速切换,无需修改业务代码
  • 完整的方法支持:包括核心缓存方法、原子操作、字符串操作、批量操作、过期时间操作、键操作、哈希表操作、数组/列表操作等
  • 高兼容性:所有缓存方式支持相同的参数格式和返回值类型
  • 灵活配置:支持多种配置方式,适应不同的应用场景
  • 一致性保证:确保不同缓存实现的行为一致性,特别是在原子操作和过期时间处理方面

安装

npm install mm_cache

使用方法

基础用法

const { Cache } = require('mm_cache');

// 初始化Redis缓存
const redisCache = new Cache({
  way: 'redis',
  redis: {
    host: '127.0.0.1',
    port: 6379,
    password: 'your_password',
    db: 0
  }
});

// 初始化MongoDB缓存
const mongodbCache = new Cache({
  way: 'mongodb',
  mongodb: {
    url: 'mongodb://localhost:27017',
    database: 'cache_db',
    collection: 'cache_collection'
  }
});

// 初始化CacheBase缓存
const cacheBaseCache = new Cache({
  way: 'cachebase',
  cachebase: {
    enabled: true,
    cache_dir: './cache'
  }
});

// 初始化Memory缓存
const memoryCache = new Cache({
  way: 'memory'
});

// 使用统一的API操作不同的缓存
async function testCache(cache) {
  // 设置缓存
  await cache.set('key', 'value');
  
  // 获取缓存
  const value = await cache.get('key');
  $.log.debug(value); // 'value'
  
  // 删除缓存
  await cache.del('key');
  
  // 检查缓存是否存在
  const exists = await cache.has('key');
  $.log.debug(exists); // false
}

// 使用Redis缓存
await testCache(redisCache);

// 使用MongoDB缓存(无需修改代码)
await testCache(mongodbCache);

// 使用CacheBase缓存(无需修改代码)
await testCache(cacheBaseCache);

// 使用Memory缓存(无需修改代码)
await testCache(memoryCache);

高级用法

设置过期时间

// 设置缓存并设置过期时间为60秒
await cache.set('key', 'value', 60);

// 为已存在的键设置过期时间
await cache.expire('key', 60);

// 移除过期时间
await cache.persist('key');

// 获取剩余过期时间
const ttl = await cache.ttl('key');
$.log.debug(ttl); // 剩余过期时间(秒),-1表示无过期时间,-2表示键不存在

原子操作

// 原子递增
const newValue = await cache.incr('counter');
$.log.debug(newValue); // 1, 2, 3...

// 增加指定整数值
const result = await cache.incrby('counter', 10);

// 增加指定浮点数值
const floatResult = await cache.incrbyfloat('counter', 0.5);

// 减少指定整数值
const decrResult = await cache.decrby('counter', 5);

哈希表操作

// 设置哈希表字段
await cache.hset('user:1', 'name', '张三');
await cache.hset('user:1', 'age', 25);

// 获取哈希表字段
const name = await cache.hget('user:1', 'name');
$.log.debug(name); // '张三'

// 获取所有哈希表字段
const user = await cache.hgetall('user:1');
$.log.debug(user); // { name: '张三', age: '25' }

// 批量设置哈希表字段
await cache.hmset('user:2', {
  name: '李四',
  age: 30,
  email: '[email protected]'
});

// 批量获取哈希表字段
const values = await cache.hmget('user:2', ['name', 'age']);
$.log.debug(values); // ['李四', '30']

// 删除哈希表字段
await cache.hdel('user:1', 'age');

// 获取哈希表字段数量
const count = await cache.hlen('user:1');
$.log.debug(count); // 1

数组/列表操作

// 设置列表
await cache.setForList('users', ['张三', '李四', '王五']);

// 向列表添加元素
await cache.addForList('users', '赵六');

// 检查元素是否在列表中
const exists = await cache.hasForList('users', '李四');
$.log.debug(exists); // true

// 获取列表元素(支持分页)
const users = await cache.getForList('users', 0, 2);
$.log.debug(users); // ['张三', '李四', '王五']

// 清空列表
await cache.clearForList('users');

方法兼容性

mm_cache确保所有支持的缓存方式(Redis、MongoDB、CacheBase、Memory)都实现了相同的API接口,具体兼容性情况请参考项目中的compatibility_table.md文件。

配置说明

全局配置选项

const cache = new Cache({
  // 缓存方式: redis, mongodb, cachebase, memory
  way: 'redis',
  
  // Redis配置
  redis: {
    host: '127.0.0.1',
    port: 6379,
    password: 'your_password',
    db: 0
  },
  
  // MongoDB配置
  mongodb: {
    url: 'mongodb://localhost:27017',
    database: 'cache_db',
    collection: 'cache_collection'
  },
  
  // CacheBase配置
  cachebase: {
    enabled: true,
    cache_dir: './cache',
    file_ext: '.cache.json',
    memory_limit: 5000,
    save_interval: 5000,
    file_mode: 384
  }
});

贡献指南

  1. Fork项目:在Gitee上Fork本项目到您的账户
  2. 创建分支:在您的仓库中创建功能分支
  3. 提交修改:提交您的代码修改和详细的提交信息
  4. 运行测试:确保所有测试通过
  5. 发起PR:向原项目提交Pull Request,描述您的修改内容和目的

问题反馈

如有任何问题或建议,请通过以下方式联系我们:

鸣谢

感谢以下项目和工具对本模块的支持:

许可证

本项目使用ISC许可证 - 详情请查看 LICENSE 文件