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

Commit

Permalink
fix(cache): Update cache decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Emile Filteau committed Jun 29, 2022
1 parent 18ba287 commit 1bcd841
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 41 deletions.
43 changes: 8 additions & 35 deletions src/cache/cache.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,23 @@
import { applyDecorators, SetMetadata } from '@nestjs/common';
import { ClassConstructor } from 'class-transformer';

export const CACHE_KEY = 'CACHE_KEY';
export const CACHE_INSTANCE = 'CACHE_INSTANCE';
export const CACHE_TTL = 'CACHE_TTL';
export const CACHE_LOCAL_TTL = 'CACHE_LOCAL_TTL';
export const CACHE_FORCE_UPDATE = 'CACHE_FORCE_UPDATE';
export const CACHE_DESERIALIZE_INTO = 'CACHE_DESERIALIZE_INTO';
export const CACHE_SHOULD_CACHE_NULL = 'CACHE_SHOULD_CACHE_NULL';

type DeserializeIntoArray<T = unknown> = [ClassConstructor<T>];
type DeserializeInto<T = unknown> = DeserializeIntoArray<T> | ClassConstructor<T>;
export const CACHE_DECORATOR = 'CACHE_DECORATOR';

type CacheKeyBuilder = (...args: any) => string;
type CacheTtlBuilder = (...args: any) => number;
type CacheForceUpdateBuilder = (...args: any) => boolean;

export type CacheOptions = {
instance?: 'user' | 'business';
key: string | CacheKeyBuilder;
/** In seconds */
ttl?: number | null | CacheTtlBuilder;
/** In seconds */
localTtl?: number | null | CacheTtlBuilder;
forceUpdate?: boolean | CacheForceUpdateBuilder;
deserializeInto?: DeserializeInto;
cacheNull?: boolean;
ttl?: number | CacheTtlBuilder;
};

export const Cache = (options: CacheOptions) => {
const {
ttl = null,
instance = 'business',
localTtl = null,
forceUpdate = false,
deserializeInto = undefined,
cacheNull = false,
} = options;

export const Cache = ({ key, ttl, instance }: CacheOptions) => {
return applyDecorators(
SetMetadata(CACHE_KEY, options.key),
SetMetadata(CACHE_INSTANCE, instance),
SetMetadata(CACHE_TTL, ttl),
SetMetadata(CACHE_LOCAL_TTL, localTtl),
SetMetadata(CACHE_FORCE_UPDATE, forceUpdate),
SetMetadata(CACHE_DESERIALIZE_INTO, deserializeInto),
SetMetadata(CACHE_SHOULD_CACHE_NULL, cacheNull),
SetMetadata(CACHE_DECORATOR, {
key,
ttl,
instance: instance || 'business',
}),
);
};
10 changes: 4 additions & 6 deletions src/cache/cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
import Cache from 'file-system-cache';
import { isNil } from 'lodash';

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

@Injectable()
export class CacheService implements OnModuleInit {
Expand Down Expand Up @@ -41,19 +40,18 @@ export class CacheService implements OnModuleInit {
private registerCache(instance: any, methodName: string) {
const logger = this.logger;
const methodRef = instance[methodName];
const rawCacheKey: CacheOptions['key'] = this.reflector.get(CACHE_KEY, methodRef);
const rawCacheTtl: CacheOptions['ttl'] = this.reflector.get(CACHE_TTL, methodRef);
const options = this.reflector.get<CacheOptions>(CACHE_DECORATOR, methodRef);

// Don't register cache on interval when missing parameters
if (!rawCacheKey || !isNil(rawCacheTtl)) return;
if (!options) return;

// Service references
const cacheManager = this.cacheManager;
const extractKey = this.extractKey;

// Augment the method to be cached with caching mechanism
instance[methodName] = async function (...args: any[]) {
const cacheKey = extractKey(rawCacheKey, args);
const cacheKey = extractKey(options.key, args);
const cachedValue = await cacheManager.get(cacheKey);

if (cachedValue) {
Expand Down

0 comments on commit 1bcd841

Please sign in to comment.