apollo-datasource-grpc
Version:
gRPC implementation of Apollo Server's Datasources. Makes it possible to use Partial Query Caching
27 lines (19 loc) • 697 B
text/typescript
import { InMemoryLRUCache, KeyValueCache } from 'apollo-server-caching';
class GRPCCache {
public keyValueCache: KeyValueCache<string>;
constructor(keyValueCache: KeyValueCache<string>) {
this.keyValueCache = keyValueCache;
if (!this.keyValueCache) this.keyValueCache = new InMemoryLRUCache();
}
async get(key: string) {
const entry = await this.keyValueCache.get(`grpccache:${key}`);
if (entry) return JSON.parse(entry);
return null;
}
async set(key: string, value: any, ttl: number) {
if (ttl > 0) {
await this.keyValueCache.set(`grpccache:${key}`, JSON.stringify(value), { ttl });
}
}
}
export default GRPCCache;