配置缓存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { Module } from '@nestjs/common'
import { CacheModule } from '@nestjs/cache-manager'
import * as fsHashStore from 'cache-manager-fs-hash'
import { FileCacheService } from './file-cache.service'

@Module({
imports: [
CacheModule.register({
store: fsHashStore,
path: 'cache',
}),
],
providers: [FileCacheService],
exports: [CacheModule, FileCacheService],
})
export class FileCacheModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { Inject, Injectable, Logger } from '@nestjs/common'
import { CACHE_MANAGER } from '@nestjs/cache-manager'
import { Cache } from 'cache-manager'

@Injectable()
export class FileCacheService {
private readonly logger = new Logger(FileCacheService.name)

constructor(@Inject(CACHE_MANAGER) private fileCache: Cache) {}

async get(cacheKey: string) {
return this.fileCache.get(cacheKey)
}
async set(key: string, value: any, cacheTime?: number) {
await this.fileCache.set(key, value, cacheTime)
}
}