缓存
缓存分类
模块a-cache
提供了两种缓存:
内存缓存
:仅对当前进程有效数据库缓存
:可跨进程使用
内存缓存
模块a-cache
通过中间件机制向ctx
注入了对象cache.mem
a-cache/backend/src/controller/test.js
set
// set(name, value, timeout)
this.ctx.cache.mem.set('name', 'zhennann', 1000);
名称 | 缺省值 | 说明 |
---|---|---|
name | 缓存名称,属于当前模块 | |
value | 缓存值 | |
timeout | 0 | 缓存超时 |
has
let res = this.ctx.cache.mem.has('name');
get
let value = this.ctx.cache.mem.get('name');
remove
this.ctx.cache.mem.remove('name');
跨模块访问
可访问其他模块定义的缓存
// other module's cache
const moduleCache = this.ctx.cache.mem.module('a-cache');
// get
let value = moduleCache.get('name');
数据库缓存
模块a-cache
通过中间件机制向ctx
注入了对象cache.db
a-cache/backend/src/controller/test.js
set
// set(name, value, timeout)
await this.ctx.cache.db.set('name', 'zhennann', 1000);
名称 | 缺省值 | 说明 |
---|---|---|
name | 缓存名称,属于当前模块 | |
value | 缓存值 | |
timeout | 0 | 缓存超时 |
has
let res = await this.ctx.cache.db.has('name');
get
let value = await this.ctx.cache.db.get('name');
remove
await this.ctx.cache.db.remove('name');
clear
await this.ctx.cache.db.clear();
跨模块访问
可访问其他模块定义的缓存
// other module's cache
const moduleCache = this.ctx.cache.db.module('a-cache');
let value = await moduleCache.get('name');
评论: