forked from apollographql/apollo-server
-
Notifications
You must be signed in to change notification settings - Fork 2
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 apollographql#2109 from apollographql/abernix/cach…
…ing-tweak-prep-two Improve InMemoryLRUCache implementation.
- Loading branch information
Showing
9 changed files
with
105 additions
and
30 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
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,24 +1,46 @@ | ||
import LRU from 'lru-cache'; | ||
import { KeyValueCache } from './KeyValueCache'; | ||
|
||
export class InMemoryLRUCache implements KeyValueCache { | ||
private store: LRU.Cache<string, string>; | ||
export class InMemoryLRUCache<V = string> implements KeyValueCache<V> { | ||
private store: LRU.Cache<string, V>; | ||
|
||
// FIXME: Define reasonable default max size of the cache | ||
constructor({ maxSize = Infinity }: { maxSize?: number } = {}) { | ||
this.store = new LRU({ | ||
max: maxSize, | ||
length: item => item.length, | ||
length(item) { | ||
if (Array.isArray(item) || typeof item === 'string') { | ||
return item.length; | ||
} | ||
|
||
// If it's an object, we'll use the length to get an approximate, | ||
// relative size of what it would take to store it. It's certainly not | ||
// 100% accurate, but it's a very, very fast implementation and it | ||
// doesn't require bringing in other dependencies or logic which we need | ||
// to maintain. In the future, we might consider something like: | ||
// npm.im/object-sizeof, but this should be sufficient for now. | ||
if (typeof item === 'object') { | ||
return JSON.stringify(item).length; | ||
} | ||
|
||
// Go with the lru-cache default "naive" size, in lieu anything better: | ||
// https://github.com/isaacs/node-lru-cache/blob/a71be6cd/index.js#L17 | ||
return 1; | ||
}, | ||
}); | ||
} | ||
|
||
async get(key: string) { | ||
return this.store.get(key); | ||
} | ||
async set(key: string, value: string) { | ||
this.store.set(key, value); | ||
async set(key: string, value: V, options?: { ttl?: number }) { | ||
const maxAge = options && options.ttl && options.ttl * 1000; | ||
this.store.set(key, value, maxAge); | ||
} | ||
async delete(key: string) { | ||
this.store.del(key); | ||
} | ||
async flush(): Promise<void> { | ||
this.store.reset(); | ||
} | ||
} |
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,5 @@ | ||
export interface KeyValueCache { | ||
get(key: string): Promise<string | undefined>; | ||
set(key: string, value: string, options?: { ttl?: number }): Promise<void>; | ||
export interface KeyValueCache<V = string> { | ||
get(key: string): Promise<V | undefined>; | ||
set(key: string, value: V, options?: { ttl?: number }): Promise<void>; | ||
delete(key: string): Promise<boolean | void>; | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/apollo-server-caching/src/__tests__/InMemoryLRUCache.test.ts
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,11 @@ | ||
import { | ||
testKeyValueCache_Basics, | ||
testKeyValueCache_Expiration, | ||
} from '../../../apollo-server-caching/src/__tests__/testsuite'; | ||
import { InMemoryLRUCache } from '../InMemoryLRUCache'; | ||
|
||
describe('InMemoryLRUCache', () => { | ||
const cache = new InMemoryLRUCache(); | ||
testKeyValueCache_Basics(cache); | ||
testKeyValueCache_Expiration(cache); | ||
}); |
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