wangrong
7 天以前 55c6624407e8ae9d8ba9dd4c65d4c1d3b0ffaed0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { DemoCacheService } from '../../service/cache';
import { Inject, Post, Provide, Get, InjectClient } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { CachingFactory, MidwayCache } from '@midwayjs/cache-manager';
 
/**
 * 缓存
 */
@CoolController()
export class OpenDemoCacheController extends BaseController {
  @InjectClient(CachingFactory, 'default')
  midwayCache: MidwayCache;
 
  @Inject()
  demoCacheService: DemoCacheService;
 
  /**
   * 设置缓存
   * @returns
   */
  @Post('/set', { summary: '设置缓存' })
  async set() {
    await this.midwayCache.set('a', 1);
    // 缓存10秒
    await this.midwayCache.set('a', 1, 10 * 1000);
    return this.ok(await this.midwayCache.get('a'));
  }
 
  /**
   * 获得缓存
   * @returns
   */
  @Get('/get', { summary: '获得缓存' })
  async get() {
    return this.ok(await this.demoCacheService.get());
  }
}