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

feat(caching): Add file system cache #316

Merged
merged 5 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ dist
tmp
/out-tsc

# cache
.cache

# dependencies
node_modules
/.pnp
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"bitcoin-address-validation": "^2.2.1",
"esbuild": "^0.14.27",
"esbuild-runner": "^2.2.1",
"file-system-cache": "^1.0.5",
"fs-extra": "^10.0.1",
"inquirer": "^8.2.2",
"lodash": "^4.17.21",
Expand Down
44 changes: 43 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions src/cache/cache-on-interval.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CACHE_MANAGER, Inject, Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { Inject, Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
import { Cache } from 'cache-manager';
import Cache from 'file-system-cache';

import {
CacheOnIntervalOptions,
Expand All @@ -13,15 +13,20 @@ export class CacheOnIntervalService implements OnModuleInit, OnModuleDestroy {
private readonly intervals: NodeJS.Timer[] = [];
private readonly registeredCacheKeys: string[] = [];
private logger = new Logger(CacheOnIntervalService.name);
private cacheManager = Cache({
basePath: './.cache',
ns: '@CacheOnInterval',
});

constructor(
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
@Inject(DiscoveryService) private readonly discoveryService: DiscoveryService,
@Inject(MetadataScanner) private readonly metadataScanner: MetadataScanner,
@Inject(Reflector) private readonly reflector: Reflector,
) {}

onModuleInit() {
async onModuleInit() {
await this.cacheManager.load();

const instanceWrappers = this.discoveryService.getProviders();
instanceWrappers
.filter(wrapper => wrapper.isDependencyTreeStatic() && !!wrapper.instance)
Expand Down
13 changes: 9 additions & 4 deletions src/cache/cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { CACHE_MANAGER, Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
import { Cache } from 'cache-manager';
import Cache from 'file-system-cache';
import { isNil } from 'lodash';

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

@Injectable()
export class CacheService implements OnModuleInit {
private logger = new Logger(CacheService.name);
private cacheManager = Cache({
basePath: './.cache',
ns: '@Cache',
});

constructor(
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
@Inject(DiscoveryService) private readonly discoveryService: DiscoveryService,
@Inject(MetadataScanner) private readonly metadataScanner: MetadataScanner,
@Inject(Reflector) private readonly reflector: Reflector,
) {}

onModuleInit() {
async onModuleInit() {
await this.cacheManager.load();

const instanceWrappers = this.discoveryService.getProviders();
instanceWrappers
.filter(wrapper => wrapper.isDependencyTreeStatic() && !!wrapper.instance)
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"baseUrl": "./src",
"paths": {
"~*": ["./*"]
}
},
"typeRoots": ["types", "node_modules/@types"]
},
"include": ["./src/**/*"]
}
15 changes: 15 additions & 0 deletions types/file-system-cache/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
declare module 'file-system-cache' {
interface CacheManager {
get(key: string): Promise<string>;
getSync(key: string): string;
set(key: string, value: string): Promise<void>;
setSync(key: string, value: string): void;
remove(key: string): Promise<void>;
clear(): Promise<void>;
load(): Promise<void>;
}

type factoryOpts = { basePath?: string; ns?: string };
function factory(opts?: factoryOpts): CacheManager;
export default factory;
}