UNPKG

adonis5-cache

Version:
50 lines (49 loc) 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class MemcachedStorage { constructor(memcachedClient) { this.memcachedClient = memcachedClient; } async get(context, key) { const cacheValue = (await this.memcachedClient.get(key)) || null; return cacheValue !== null ? context.deserialize(cacheValue) : cacheValue; } async getMany(context, keys) { const cachedValues = await this.memcachedClient.getMulti(keys); return keys.map((key) => { return cachedValues[key] ? context.deserialize(cachedValues[key]) : null; }); } async put(context, key, value, ttl) { await this.memcachedClient.set(key, context.serialize(value), ttl); } async putMany(context, cacheDictionary, ttl) { await Promise.all(Object.entries(cacheDictionary).map(([key, value]) => this.put(context, key, value, ttl))); } async flush() { await this.memcachedClient.flush(); } async forget(key) { const result = await this.memcachedClient.del(key); return Boolean(result); } async addTag(tag, tagData) { const tagName = this.buildMemcachedTagName(tag); const tagRecords = (await this.memcachedClient.get(tagName)) || []; tagRecords.push(tagData); await this.memcachedClient.set(tagName, tagRecords, 0); } async readTag(tag) { return ((await this.memcachedClient.get(this.buildMemcachedTagName(tag))) || []); } async removeTag(tag) { await this.memcachedClient.del(this.buildMemcachedTagName(tag)); } buildMemcachedTagName(userTagName) { return ['memcached-tag', userTagName].join(':'); } resolveTtl(ttlInMl) { return Math.round(ttlInMl / 1000); } } exports.default = MemcachedStorage;