5.0.2 • Published 8 months ago
@zenweb/cache v5.0.2
ZenWeb Cache Module
基于 Redis 的对象缓存工具
功能如下:
- 对象缓存
- 大对象自动压缩
- JSON 序列直接输出
- 防止缓存击穿
- 单例执行
安装
yarn add @zenweb/cache
import { create } from 'zenweb';
import modCache from '@zenweb/cache';
const app = create();
app.setup(modCache());
app.start();
使用
import { Context, mapping } from "zenweb";
import { $cache, cached } from "@zenweb/cache";
export class CacheController {
/**
* 一般使用
*/
@mapping()
async index() {
const result = await $cache.lockGet('TEST', function() {
return {
cacheAt: new Date(),
};
});
return result;
}
/**
* 大对象压缩存储
*/
@mapping()
async big() {
const result = await $cache.lockGet('TEST-GZ', function() {
return longData;
});
return result;
}
/**
* 大对象直接输出 - 1
*/
@mapping()
async big_direct_out(ctx: Context) {
const result = await $cache.lockGet('TEST-GZ', function() {
return longData;
}, { parse: false, decompress: false });
if (result.compressed) {
ctx.set('Content-Encoding', 'gzip');
}
ctx.type = 'json';
ctx.body = result.data;
}
/**
* 使用缓存中间件
* - 自动处理是否需要解压缩对象
*/
@mapping({
middleware: cached('TEST-middleware'),
})
async cached_middleware() {
return longData;
}
}
5.0.2
8 months ago
5.0.1
8 months ago
5.0.0
8 months ago
4.7.1
9 months ago
4.5.0
10 months ago
4.7.0
9 months ago
4.6.0
10 months ago
4.4.0
11 months ago
4.3.1
1 year ago
4.3.0
2 years ago
4.2.2
2 years ago
4.2.1
2 years ago
4.2.0
2 years ago
4.1.3
2 years ago
4.1.2
2 years ago
4.1.1
2 years ago
4.1.0
2 years ago
4.0.1-2
2 years ago
4.0.1
2 years ago
4.0.0
2 years ago