@melchyore/adonis-cache
Version:
Cache package for AdonisJS V5
125 lines (124 loc) • 4.16 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const luxon_1 = require("luxon");
const path_1 = require("path");
const BaseStore_1 = __importDefault(require("./BaseStore"));
const Utils_1 = __importDefault(require("../Utils"));
class File extends BaseStore_1.default {
constructor(drive) {
super();
this.drive = drive;
}
async get(key) {
try {
return (await this.getPayload(key)).value;
}
catch {
return null;
}
}
async many(keys) {
const records = {};
const values = await Promise.all(keys.map((key) => this.get(key)));
for (let i = 0; i < values.length; ++i) {
const value = values[i];
records[keys[i]] = value ? value : null;
}
return records;
}
async has(key) {
return (await this.get(key)) && (await this.drive.exists(this.path(key)));
}
async put(key, value, ttl) {
try {
await this.drive.put(this.path(key), String(luxon_1.DateTime.now().toMillis() + ttl) + this.serialize(value));
return true;
}
catch (e) {
console.error(e);
return false;
}
}
async putMany(list, ttl) {
const promiseArray = [];
for (const [key, value] of Object.entries(list)) {
promiseArray.push(this.put(key, value, ttl));
}
return Promise.all(promiseArray);
}
async increment(key, value) {
return await this.incrementOrDecrement(key, (currentValue) => {
return currentValue + value;
});
}
async decrement(key, value) {
return this.incrementOrDecrement(key, (currentValue) => {
return currentValue - value;
});
}
async putManyForever(list) {
const promiseArray = [];
for (const [key, value] of Object.entries(list)) {
promiseArray.push(this.forever(key, value));
}
return Promise.all(promiseArray);
}
async forever(key, value) {
return await this.put(key, value, luxon_1.DateTime.now().plus({ years: 5 }).toMillis());
}
async forget(_key) {
const key = this.path(_key);
if (await this.drive.exists(key)) {
await this.drive.delete(key);
return true;
}
return false;
}
async flush() {
const path = './cache';
if (await this.drive.exists(path)) {
await this.drive.delete(path);
return true;
}
return false;
}
async getPayload(key) {
const contents = (await this.drive.get(this.path(key))).toString();
const expiration = parseInt(contents.substring(0, 13));
if (this.isStaleRecord({ value: contents, expiration })) {
await this.forget(key);
return null;
}
return {
expiration,
value: this.deserialize(contents.substring(13, contents.length))
};
}
async incrementOrDecrement(key, callback) {
try {
const record = await this.getPayload(key);
if (record) {
const currentValue = parseInt(this.deserialize(record.value));
if (isNaN(currentValue)) {
return false;
}
const newValue = callback(currentValue);
await this.drive.put(this.path(key), String(record.expiration) + this.serialize(newValue));
return newValue;
}
return false;
}
catch {
return false;
}
}
path(key) {
const hash = Utils_1.default.sha1(this.buildKey(key));
const parts = Utils_1.default.chunks(hash, 2).slice(0, 2);
return (0, path_1.join)('cache', ...parts, hash);
}
}
exports.default = File;