UNPKG

@melchyore/adonis-cache

Version:
181 lines (178 loc) 5.81 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("@poppinss/utils"); const luxon_1 = require("luxon"); const DynamoDB_1 = __importDefault(require("../Models/DynamoDB")); const BaseStore_1 = __importDefault(require("./BaseStore")); class DynamoDB extends BaseStore_1.default { constructor(client, tableName) { super(); this.client = client; this.tableName = tableName; this.store = (0, DynamoDB_1.default)(this.client, this.tableName); } /** * In DynamoDB, TTL is expressed in seconds. */ calculateTTL(ttlInMilliseconds) { return ttlInMilliseconds / 1000; } async get(key) { try { //const record = await this.store.primaryKey.get(this.buildKey(key)) const record = await this.store.primaryKey.query({ Key: this.buildKey(key), ExpiresAt: ['>=', luxon_1.DateTime.now().toSeconds()] }); if (record.length > 0) { const serializedRecord = record[0].toJSON(); /*const expiration = serializedRecord.ExpiresAt if (DateTime.now().toSeconds() >= expiration) { await this.forget(key) return null }*/ return this.deserialize(serializedRecord.Value); } } catch { } return null; } async many(keys) { const records = {}; for (const key of keys) { records[key] = await this.get(key); } return records; } async has(key) { return ((await this.store.primaryKey.query({ Key: this.buildKey(key), ExpiresAt: ['>=', luxon_1.DateTime.now().toSeconds()] })).length > 0); } async put(key, value, ttl) { try { await this.store .new({ Key: this.buildKey(key), Value: this.serialize(value), ExpiresAt: luxon_1.DateTime.now().toSeconds() + ttl }) .save(); return true; } catch { return false; } } async add(key, value, ttl) { try { await this.store .new({ Key: this.buildKey(key), Value: this.serialize(value), ExpiresAt: luxon_1.DateTime.now().toSeconds() + ttl }) .save({ conditions: { Key: ['not exists'] } }); return true; } catch { return false; } } async putMany(list, ttl) { const records = []; try { for (const [key, value] of Object.entries(list)) { records.push(this.store.new({ Key: this.buildKey(key), Value: this.serialize(value), ExpiresAt: luxon_1.DateTime.now().toSeconds() + ttl })); } await this.store.documentClient.batchPut(records); } catch { return Object.keys(list).map(() => false); } return Object.keys(list).map(() => true); } async increment(key, value) { return await this.incrementOrDecrement(key, (currentValue) => { return currentValue + value; }); } async decrement(key, value) { return await this.incrementOrDecrement(key, (currentValue) => { return currentValue - value; }); } async putManyForever(list) { const results = []; for (const [key, value] of Object.entries(list)) { results.push(this.forever(key, value)); } return Promise.all(results); } async forever(key, value) { return this.put(key, value, 60 * 60 * 24 * 365 * 5); } async forget(key) { try { await this.store.primaryKey.delete(this.buildKey(key)); return true; } catch { return false; } } async flush() { throw new utils_1.Exception('DynamoDb does not support flushing an entire table. Please delete then create the cache table'); } async createTable(tableName = 'Cache') { const model = (0, DynamoDB_1.default)(this.client, tableName); try { await model.describeTable(); return null; } catch { try { return await model.createTable(); } catch (e) { throw new utils_1.Exception(e); } } } async incrementOrDecrement(key, callback) { try { const previousValue = await this.get(key); if (previousValue) { const currentValue = parseInt(previousValue); if (isNaN(currentValue)) { return false; } const newValue = callback(currentValue); await this.store.primaryKey .fromKey(this.buildKey(key)) .set('Value', newValue.toString()) .save({ operator: 'update' }); return newValue; } return false; } catch { return false; } } } exports.default = DynamoDB;