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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fastcar/cache

v0.0.9

Published

应用内缓存

Downloads

10

Readme

内存缓存

使用场景

  • 提供基础的缓存功能(比如 增删改查和设置缓存失效)

  • 用于频繁存储数据 减缓对数据库写操作的压力

  • 允许自定义不同的客户端,拓展其数据持久化能力

快速安装

npm install @fastcar/cache

使用案例

  • 开启缓存功能
@EnableCache
class APP {
 app!: FastCarApplication;
}
  • 无客户端持久化缓存示例
import {CacheMapping} from "@fastcar/cache";
//配置操作
@CacheMapping
class NoclientMapping implements CacheConfig {
 store: string = "noclientStore";
 initSync: boolean = false;
}

export default NoclientMapping;
//调用操作
cacheApplication.set("noclientStore", "hello", "world");
let world = cacheApplication.get("noclientStore", "hello");
assert(world == "world");

//设置缓存过期时间
cacheApplication.set("noclientStore", "hellottl", "worldttl", { ttl: 2 }); //2秒后消失
  //两秒判断是否存在key
setTimeout(() => {
    assert(!cacheApplication.has("noclientStore", "hellottl")); //可能会有100ms左右延迟
}, 2200);
  • 文件客户端缓存示例
//配置操作
@CacheMapping
export default class FileClientMapping implements CacheConfig {
 store: string = "fileStore";
 initSync: boolean = true;
 syncTimer: number = 5; //5秒钟同步一次
 dbClient: DBClientService<String>;

 constructor() {
  this.dbClient = new FSClient(path.join(__dirname, "../", "filedb", "filedb.json"));
 }
}
 cacheApplication.set("fileStore", "hello", "world", { flush: true }); //立即存储
 cacheApplication.set("fileStore", "hello", "worldss"); //同步后存储
  • mysql数据库操作示例
//配置操作
@CacheMapping
export default class MySqlClientMapping implements CacheConfig {
 store: string = "mysqlStore";
 initSync: boolean = true;
 syncTimer: number = 10;
 ttl: number = 20; //60秒后过期
 dbClient: DBClientService<String>;
 dbSync: boolean = false;

 @CallDependency
 private cacheMapper!: CacheMapper;

 constructor() {
  //自定义构造一个存储器
  this.dbClient = {
   mset: async (list: Item<String>[]) => {
    await this.cacheMapper.saveORUpdate(
     list.map((item) => {
      return new CacheModel(Object.assign(item, { updateTime: DateUtil.toDateTime() }));
     })
    );
    return true;
   },
   mget: async (): Promise<Item<String>[]> => {
    let list = await this.cacheMapper.select({});
    return list.map((item) => {
     return {
      key: item.key,
      value: item.value,
      ttl: Math.floor((item.updateTime.getTime() - Date.now()) / 1000),
     };
    });
   },
   mdelete: async (keys: string[]) => {
    await this.cacheMapper.delete({
     where: {
      key: keys,
     },
    });
    return true;
   },
  };
 }
}
  cacheApplication.set("mysqlStore", "hello", "world"); //10s秒存储 60秒后进行删除

缺陷

  • 在项目中限定了每100ms扫描计划操作一次,所以当设置立即存储时 仍然有100ms左右的延迟

  • 设置ttl后,数据库客户端的数据,如果程序重启了,则需要重新计算一次ttl时效性

  • 不支持分布式缓存,如果需要实现分布式,请确保路由到每个服务时需满足一致性hash规则

更多用法

参考项目git地址 @fastcar/cache/test 下的simple内

项目开源地址