import type { KeyValueCache , KeyValueCacheSetOptions , } from 'apollo-server-caching' ; import Memcached from 'memcached' ; import { promisify } from 'util' ; export class MemcachedCache implements KeyValueCache { readonly client : any ; readonly defaultSetOptions : KeyValueCacheSetOptions = { ttl : 300 , } ; constructor ( serverLocation , options ? ) { const client = new Memcached ( serverLocation , options ) ; client . del = promisify ( client . del ) . bind ( client ) ; client . get = promisify ( client . get ) . bind ( client ) ; client . set = promisify ( client . set ) . bind ( client ) ; client . flush = promisify ( client . flush ) . bind ( client ) ; this . client = client ; } async set ( key , value , options ? ) { const { ttl } = Object . assign ( { } , this . defaultSetOptions , options ) ; if ( typeof ttl === 'number' ) { await this . client . set ( key , value , ttl ) ; } else { await this . client . set ( key , value , 0 ) ; } } async get ( key ) { return await this . client . get ( key ) ; } async delete ( key ) { return await this . client . del ( key ) ; } async flush ( ) { await this . client . flush ( ) ; } async close ( ) { this . client . end ( ) ; } }