Repository URL to install this package:
|
Version:
2.0.11 ▾
|
/**
* @file @name KeyValueCache THIS EXPORTS A CLASS, NOT AN INSTANCE
*
*
* @see https://github.com/apollographql/apollo-server/blob/master/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts
* @see https://github.com/apollographql/apollo-server/blob/master/packages/apollo-datasource-rest/src/HTTPCache.ts
*
* @tutorial https://www.apollographql.com/docs/engine/proxy/guides.html#caching
*/
export interface KeyValueCacheType {
get(key: string): Promise<string | undefined>;
set(key: string, value: string, options?: { ttl?: number }): Promise<void>;
}
export class KeyValueCache implements KeyValueCacheType {
store: Map<string, string> = new Map()
async get(key: string): Promise<string | undefined> {
const value = this.store.get(key)
console.log('[cache] get: ' + key)
// @todo could get size of this instead
// console.debug('[cache] get->value: ', value)
return Promise.resolve(value)
}
async set(
key: string,
value: string,
options: { ttl?: number } = {}
): Promise<void> {
console.log('[cache] set: ' + key)
this.store.set(key, value)
return Promise.resolve()
}
}