This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Emile Filteau
committed
Jun 29, 2022
1 parent
18ba287
commit 1bcd841
Showing
2 changed files
with
12 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters