-
-
Notifications
You must be signed in to change notification settings - Fork 282
feat: add monitored cache wrapper and use #915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
| export type CacheLookupOptions = { | ||
| recordMetrics?: boolean | ||
| } | ||
|
|
||
| export type CacheLookupOutcome = 'hit' | 'miss' | 'stale' | ||
|
|
||
| export type CacheLookupResult<V> = { | ||
| value: V | undefined | ||
| outcome: CacheLookupOutcome | ||
| } | ||
|
|
||
| export type CacheStats = { | ||
| entries: number | ||
| sizeBytes: number | ||
| } | ||
|
|
||
| export interface Cache<K, V, SetOptions = undefined> { | ||
| get(key: K, options?: CacheLookupOptions): V | undefined | ||
| set(key: K, value: V, options?: SetOptions): void | ||
| delete(key: K): boolean | ||
| } | ||
|
|
||
| export interface InspectableCache<K, V, SetOptions = undefined> extends Cache<K, V, SetOptions> { | ||
| getStats(): CacheStats | ||
| } | ||
|
|
||
| export interface OutcomeAwareCache<K, V, SetOptions = undefined> | ||
| extends InspectableCache<K, V, SetOptions> { | ||
| getWithOutcome(key: K): CacheLookupResult<V> | ||
| } | ||
|
|
||
| export interface Disposable { | ||
| dispose(): void | ||
| } | ||
|
|
||
| export interface DisposableCache<K, V, SetOptions = undefined> | ||
| extends OutcomeAwareCache<K, V, SetOptions>, | ||
| Disposable {} |
This file contains hidden or 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,4 @@ | ||
| export * from './adapter' | ||
| export * from './lru' | ||
| export * from './names' | ||
| export * from './ttl' |
This file contains hidden or 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,100 @@ | ||
| import { LRUCache as BaseLruCache } from 'lru-cache' | ||
| import { CacheLookupOptions, CacheLookupOutcome, DisposableCache } from './adapter' | ||
| import { monitorCache, withCacheEvictionMetrics } from './monitoring' | ||
| import { CacheName } from './names' | ||
|
|
||
| export type LruCacheSetOptions<K extends {}, V extends {}> = BaseLruCache.SetOptions<K, V, unknown> | ||
|
|
||
| export type LruCacheOptions<K extends {}, V extends {}> = BaseLruCache.Options<K, V, unknown> & { | ||
| purgeStaleIntervalMs?: number | ||
| } | ||
|
|
||
| export const DEFAULT_CACHE_PURGE_STALE_INTERVAL_MS = 1000 * 60 // 1 minute | ||
|
|
||
| export class LruCache<K extends {}, V extends {}> | ||
| implements DisposableCache<K, V, LruCacheSetOptions<K, V>> | ||
| { | ||
| private readonly cache: BaseLruCache<K, V> | ||
| private readonly purgeStaleTimer?: ReturnType<typeof setInterval> | ||
|
|
||
| constructor(options: LruCacheOptions<K, V>) { | ||
| const { purgeStaleIntervalMs, ...cacheOptions } = options | ||
|
|
||
| this.cache = new BaseLruCache<K, V>({ | ||
| ...cacheOptions, | ||
| }) | ||
|
|
||
| if (purgeStaleIntervalMs) { | ||
| this.purgeStaleTimer = setInterval(() => { | ||
| this.cache.purgeStale() | ||
| }, purgeStaleIntervalMs) | ||
| this.purgeStaleTimer.unref?.() | ||
| } | ||
| } | ||
|
|
||
| get(key: K, options?: CacheLookupOptions): V | undefined { | ||
|
ferhatelmas marked this conversation as resolved.
ferhatelmas marked this conversation as resolved.
|
||
| return this.getWithOutcome(key).value | ||
| } | ||
|
|
||
| getWithOutcome(key: K) { | ||
| const status: BaseLruCache.Status<V> = {} | ||
| const value = this.cache.get(key, { status }) | ||
| const outcome = (status.get || (value === undefined ? 'miss' : 'hit')) as CacheLookupOutcome | ||
|
|
||
| return { value, outcome } | ||
| } | ||
|
|
||
| set(key: K, value: V, options?: LruCacheSetOptions<K, V>): void { | ||
| this.cache.set(key, value, options) | ||
| } | ||
|
|
||
| delete(key: K): boolean { | ||
| return this.cache.delete(key) | ||
| } | ||
|
|
||
| getStats() { | ||
| return { | ||
| entries: this.cache.size, | ||
| sizeBytes: this.cache.calculatedSize, | ||
| } | ||
| } | ||
|
|
||
| purgeStale(): boolean { | ||
| return this.cache.purgeStale() | ||
| } | ||
|
|
||
| dispose(): void { | ||
| if (this.purgeStaleTimer) { | ||
| clearInterval(this.purgeStaleTimer) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export function createLruCache<K extends {}, V extends {}>( | ||
| options: LruCacheOptions<K, V> | ||
| ): LruCache<K, V> | ||
| export function createLruCache<K extends {}, V extends {}>( | ||
| name: CacheName, | ||
| options: LruCacheOptions<K, V> | ||
| ): DisposableCache<K, V, LruCacheSetOptions<K, V>> | ||
| export function createLruCache<K extends {}, V extends {}>( | ||
| nameOrOptions: CacheName | LruCacheOptions<K, V>, | ||
| maybeOptions?: LruCacheOptions<K, V> | ||
| ) { | ||
| if (typeof nameOrOptions !== 'string') { | ||
| return new LruCache(nameOrOptions) | ||
| } | ||
|
|
||
| const cacheName = nameOrOptions | ||
| const options = maybeOptions as LruCacheOptions<K, V> | ||
| const cache = new LruCache<K, V>({ | ||
| ...options, | ||
| disposeAfter: withCacheEvictionMetrics(cacheName, options.disposeAfter), | ||
| }) | ||
|
|
||
| return monitorCache(cacheName, cache, { | ||
| purgeStale: () => { | ||
| cache.purgeStale() | ||
| }, | ||
| }) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.