-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from ckb-cell/fix/query-performance
- Loading branch information
Showing
43 changed files
with
597 additions
and
675 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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
npm |
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
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
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,3 +1,4 @@ | ||
export const ONE_MONTH_MS = 30 * 24 * 60 * 60 * 1000; | ||
export const ONE_HOUR_MS = 60 * 60 * 1000; | ||
export const TEN_MINUTES_MS = 10 * 60 * 1000; | ||
export const ONE_MINUTE_MS = 60 * 1000; |
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
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
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
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
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
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,5 +1,3 @@ | ||
import { RPC } from '@ckb-lumos/rpc'; | ||
|
||
export interface CellDep { | ||
dep_type: string; | ||
out_point: { | ||
|
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
import { Cache, CACHE_MANAGER } from '@nestjs/cache-manager'; | ||
import { Inject, Logger } from '@nestjs/common'; | ||
import { CacheableRegisterOptions, Cacheable as _Cacheable } from 'nestjs-cacheable'; | ||
import { cacheableHandle, generateComposedKey } from 'nestjs-cacheable/dist/cacheable.helper'; | ||
|
||
export interface CustomCacheableRegisterOptions extends CacheableRegisterOptions { | ||
shouldCache?: (result: any, target: any) => boolean | Promise<boolean>; | ||
} | ||
|
||
const logger = new Logger('Cacheable'); | ||
|
||
/** | ||
* Cacheable decorator with custom options, based on the original Cacheable decorator from the nestjs-cacheable package. | ||
* Adds a shouldCache option to determine whether the result should be cached. | ||
* | ||
* @example | ||
* @Cacheable({ | ||
* ttl: 1000, | ||
* key: (args: any[]) => args[0], | ||
* shouldCache: (result: any) => result !== null, | ||
* }); | ||
*/ | ||
export function Cacheable(options: CustomCacheableRegisterOptions): MethodDecorator { | ||
const injectCacheService = Inject(CACHE_MANAGER); | ||
|
||
return function(target, propertyKey, descriptor) { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
const originalMethod = descriptor.value as unknown as Function; | ||
|
||
injectCacheService(target, '__cacheManager'); | ||
return { | ||
...descriptor, | ||
value: async function(...args: any[]) { | ||
const cacheManager = this.__cacheManager as Cache; | ||
if (!cacheManager) return originalMethod.apply(this, args); | ||
const composeOptions: Parameters<typeof generateComposedKey>[0] = { | ||
methodName: String(propertyKey), | ||
key: options.key, | ||
namespace: options.namespace, | ||
args, | ||
}; | ||
const [key] = generateComposedKey(composeOptions); | ||
const returnVal = await cacheableHandle( | ||
key, | ||
() => originalMethod.apply(this, args), | ||
options.ttl, | ||
); | ||
|
||
// Remove the cache if shouldCache returns false | ||
const shouldCache = options.shouldCache | ||
? await options.shouldCache(returnVal, this) | ||
: true; | ||
if (!shouldCache) { | ||
logger.debug(`Removing cache for key: ${key}`); | ||
await cacheManager.del(key); | ||
} | ||
return returnVal; | ||
} as any, | ||
}; | ||
}; | ||
} |
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
Oops, something went wrong.