跳到主要内容
版本:3.0.0

缓存

Midway Cache 是为了方便开发者进行缓存操作的组件,它有利于改善项目的性能。它为我们提供了一个数据中心以便进行高效的数据访问。

相关信息:

描述
可用于标准项目
可用于 Serverless
可用于一体化
包含独立主框架
包含独立日志

安装

首先安装相关的组件模块。

$ npm i @midwayjs/cache@3 cache-manager --save
$ npm i @types/cache-manager --save-dev

或者在 package.json 中增加如下依赖后,重新安装。

{
"dependencies": {
"@midwayjs/cache": "^3.0.0",
"cache-manager": "^3.4.1",
// ...
},
"devDependencies": {
"@types/cache-manager": "^3.4.0",
// ...
}
}

使用 Cache

Midway 为不同的 cache 存储提供了统一的 API。默认内置了一个基于内存数据存储的数据中心。如果想要使用别的数据中心,开发者也可以切换到例如 mongodb、fs 等模式。

首先,引入 Cache 组件,在 configuration.ts 中导入:

import { Configuration, App } from '@midwayjs/core';
import * as cache from '@midwayjs/cache';
import { join } from 'path'

@Configuration({
imports: [
// ...
cache // 导入 cache 组件
],
importConfigs: [
join(__dirname, 'config')
]
})
export class MainConfiguration {
}

然后在业务代码中即可注入使用。

import { Inject, Provide } from '@midwayjs/core';
import { IUserOptions } from '../interface';
import { CacheManager } from '@midwayjs/cache';

@Provide()
export class UserService {

@Inject()
cacheManager: CacheManager; // 依赖注入 CacheManager
}

通过提供的 API 来设置,获取缓存数据。

import { Inject, Provide } from '@midwayjs/core';
import { IUserOptions } from '../interface';
import { CacheManager } from '@midwayjs/cache';

@Provide()
export class UserService {

@Inject()
cacheManager: CacheManager;

async getUser(options: IUserOptions) {
// 设置缓存内容
await this.cacheManager.set(`name`, 'stone-jin');

// 获取缓存内容
let result = await this.cacheManager.get(`name`);

return result;
}

async getUser2(){
//获取缓存内容
let result = await this.cacheManager.get(`name`);
return result;
}

async reset(){
await this.cacheManager.reset(); // 清空对应 store 的内容
}
}

设置缓存

我们通过 await this.cache.set(key, value) 方法进行设置,此处默认过期时间是10s。

你也可以手动设置 TTL(过期时间),如下:

await this.cacheManager.set(key, value, {ttl: 1000});   // ttl的单位为秒

如果你想要禁止 Cache 不过期,则将 TTL 设置为 null 即可。

await this.cacheManager.set(key, value, {ttl: null});

同时你也可以通过全局的 config.default.ts 中进行设置。

export default {
// ...
cache: {
store: 'memory',
options: {
max: 100,
ttl: 10, // 修改默认的ttl配置
},
}
}

获取缓存

const value = await this.cacheManager.get(key);

如果获取不到,则为 undefined。

移除缓存

移除缓存,可以通过 del 方法。

await this.cacheManager.del(key);

清空整体store数据(此处是整体清除,需要重点⚠️)

比如用户设置了某个 redis 为 store,调用的话,包括非 cache 模块设置的也会清除。

await this.cacheManager.reset(); // 这块需要注意

全局配置

当我们引用了这个 cache 组件后,我们能对其进行全局的配置。配置方法跟别的组件类似。

默认的配置:

export default {
// ...
cache: {
store: 'memory',
options: {
max: 100,
ttl: 10,
},
}
}

例如用户可以修改默认的 TTL,也就是过期时间。

其他Cache

用户也可以修改 store 方式,在 config.default.ts 中进行组件的配置:

import * as redisStore from 'cache-manager-ioredis';

export default {
// ...
cache: {
store: redisStore,
options: {
host: 'localhost', // default value
port: 6379, // default value
password: '',
db: 0,
keyPrefix: 'cache:',
ttl: 100
},
}
}

或者修改为 mongodb 的 cache。

危险

再次注意⚠️:使用 redis 作为 cache 的时候,代码里面慎用 reset 方法,因为会把整个 redis 给 flushdb,简称数据清空。

相关文档

由于 Midway Cache 是基于 cache-manager 封装,所以相关资料用户也可以查询:cache-manger

常见问题

1、set 和 get 无法得到相同值?

用户使用了 cache 模块,默认是内存式的,例如在本地用 dev 模式,由于是单进程的,那 set 和 ge t最终能达到相同的值。但是用户部署到服务器上面后,由于会有多 worker,相当于第一次请求,落在进程1上,然后第二次落在进程2上,这样获得到空了。

解决办法:参考 其他 Cache 的章节,配置 store 为分布式,例如 redis 的 store 等方式。