Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 4168929

Browse files
authored
feat(caching): Add file system cache (#316)
1 parent f968926 commit 4168929

File tree

7 files changed

+82
-10
lines changed

7 files changed

+82
-10
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ dist
33
tmp
44
/out-tsc
55

6+
# cache
7+
.cache
8+
69
# dependencies
710
node_modules
811
/.pnp

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"bitcoin-address-validation": "^2.2.1",
2828
"esbuild": "^0.14.27",
2929
"esbuild-runner": "^2.2.1",
30+
"file-system-cache": "^1.0.5",
3031
"fs-extra": "^10.0.1",
3132
"inquirer": "^8.2.2",
3233
"lodash": "^4.17.21",

pnpm-lock.yaml

+43-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache/cache-on-interval.service.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { CACHE_MANAGER, Inject, Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
1+
import { Inject, Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
22
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
3-
import { Cache } from 'cache-manager';
3+
import Cache from 'file-system-cache';
44

55
import {
66
CacheOnIntervalOptions,
@@ -13,15 +13,20 @@ export class CacheOnIntervalService implements OnModuleInit, OnModuleDestroy {
1313
private readonly intervals: NodeJS.Timer[] = [];
1414
private readonly registeredCacheKeys: string[] = [];
1515
private logger = new Logger(CacheOnIntervalService.name);
16+
private cacheManager = Cache({
17+
basePath: './.cache',
18+
ns: '@CacheOnInterval',
19+
});
1620

1721
constructor(
18-
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
1922
@Inject(DiscoveryService) private readonly discoveryService: DiscoveryService,
2023
@Inject(MetadataScanner) private readonly metadataScanner: MetadataScanner,
2124
@Inject(Reflector) private readonly reflector: Reflector,
2225
) {}
2326

24-
onModuleInit() {
27+
async onModuleInit() {
28+
await this.cacheManager.load();
29+
2530
const instanceWrappers = this.discoveryService.getProviders();
2631
instanceWrappers
2732
.filter(wrapper => wrapper.isDependencyTreeStatic() && !!wrapper.instance)

src/cache/cache.service.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
1-
import { CACHE_MANAGER, Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
1+
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
22
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
3-
import { Cache } from 'cache-manager';
3+
import Cache from 'file-system-cache';
44
import { isNil } from 'lodash';
55

66
import { CacheOptions, CACHE_KEY, CACHE_TTL } from './cache.decorator';
77

88
@Injectable()
99
export class CacheService implements OnModuleInit {
1010
private logger = new Logger(CacheService.name);
11+
private cacheManager = Cache({
12+
basePath: './.cache',
13+
ns: '@Cache',
14+
});
1115

1216
constructor(
13-
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
1417
@Inject(DiscoveryService) private readonly discoveryService: DiscoveryService,
1518
@Inject(MetadataScanner) private readonly metadataScanner: MetadataScanner,
1619
@Inject(Reflector) private readonly reflector: Reflector,
1720
) {}
1821

19-
onModuleInit() {
22+
async onModuleInit() {
23+
await this.cacheManager.load();
24+
2025
const instanceWrappers = this.discoveryService.getProviders();
2126
instanceWrappers
2227
.filter(wrapper => wrapper.isDependencyTreeStatic() && !!wrapper.instance)

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"baseUrl": "./src",
2323
"paths": {
2424
"~*": ["./*"]
25-
}
25+
},
26+
"typeRoots": ["types", "node_modules/@types"]
2627
},
2728
"include": ["./src/**/*"]
2829
}

types/file-system-cache/index.d.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare module 'file-system-cache' {
2+
interface CacheManager {
3+
get(key: string): Promise<string>;
4+
getSync(key: string): string;
5+
set(key: string, value: string): Promise<void>;
6+
setSync(key: string, value: string): void;
7+
remove(key: string): Promise<void>;
8+
clear(): Promise<void>;
9+
load(): Promise<void>;
10+
}
11+
12+
type factoryOpts = { basePath?: string; ns?: string };
13+
function factory(opts?: factoryOpts): CacheManager;
14+
export default factory;
15+
}

0 commit comments

Comments
 (0)