0.0.5 • Published 2 years ago
fastcar-cache v0.0.5
内存缓存
使用场景
提供基础的缓存功能(比如 增删改查和设置缓存失效)
用于频繁存储数据 减缓对数据库写操作的压力
允许自定义不同的客户端,拓展其数据持久化能力
快速安装
npm install fastcar-cache
使用案例
- 开启缓存功能
@EnableCache
class APP {
app!: FastCarApplication;
}
- 无客户端持久化缓存示例
//配置操作
@CacheMapping
class NoclientMapping implements CacheConfig {
store: string = "noclientStore";
initSync: boolean = false;
}
export default NoclientMapping;
//调用操作
let cacheApplication: CacheApplication = appInsatcne.app.getComponentByTarget(CacheApplication);
cacheApplication.set("noclientStore", "hello", "world"); //存储
let world = cacheApplication.get("noclientStore", "hello"); //拉取
assert(world == "world"); //期望为true
//设置缓存过期时间
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;
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 = 60; //60秒后过期 0为永久有效
dbClient: DBClientService;
failNum: number; //持久化失败后重试次数 默认三次
@CallDependency
private cacheMapper!: CacheMapper;
constructor() {
//自定义构造一个存储器
this.dbClient = {
mset: async (list: Item[]) => {
await this.cacheMapper.saveORUpdate(
list.map((item) => {
return new CacheModel(Object.assign(item, { updateTime: DateUtil.toDateTime() }));
})
);
return true;
},
mget: async () => {
let list = await this.cacheMapper.select({});
return list;
},
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内