cachelot
Version:
locking based inMemory cache
382 lines • 15.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewFrom = exports.New = exports.Cache = void 0;
var async_mutex_1 = require("async-mutex");
var mutex = new async_mutex_1.Mutex();
var DefaultExpiration = 0;
var NoExpiration = -1;
var Item = /** @class */ (function () {
function Item(Object, Expiration) {
this.Object = Object;
this.Expiration = Expiration;
}
Item.prototype.Expired = function () {
if (this.Expiration == 0) {
return false;
}
return Date.now() > this.Expiration;
};
return Item;
}());
var Cache = /** @class */ (function () {
function Cache(defaultExpiration, items) {
this.defaultExpiration = defaultExpiration;
this.items = items;
}
Cache.prototype.Set = function (k, x, d) {
return __awaiter(this, void 0, void 0, function () {
var e, release;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
// if d == 0 set to cache's default expiration
if (!d || d === DefaultExpiration) {
d = this.defaultExpiration;
}
if (d > 0) {
// TODO : proper date, now in ms epoch
e = Date.now() + d;
}
return [4 /*yield*/, mutex.acquire()];
case 1:
release = _a.sent();
this.items[k] = new Item(x, e);
release();
return [2 /*return*/];
}
});
});
};
Cache.prototype.set = function (k, x, d) {
var e;
// if d == 0 set to cache's default expiration
if (!d || d === DefaultExpiration) {
d = this.defaultExpiration;
}
if (d > 0) {
// TODO : proper date, now in ms epoch
e = Date.now() + d;
}
// no lock cuz callee is using the lock
this.items[k] = new Item(x, e);
};
Cache.prototype.SetDefault = function (k, x) {
this.Set(k, x, DefaultExpiration);
};
Cache.prototype.Get = function (k) {
return __awaiter(this, void 0, void 0, function () {
var release, item;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _a.sent();
item = this.items[k];
if (!item) {
release();
return [2 /*return*/, [null, false]];
}
if (item.Expiration > 0) {
if (Date.now() > item.Expiration) {
release();
return [2 /*return*/, [null, false]];
}
}
release();
return [2 /*return*/, [item.Object, true]];
}
});
});
};
Cache.prototype.GetWithExpiration = function (k) {
return __awaiter(this, void 0, void 0, function () {
var release, item;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _a.sent();
item = this.items[k];
if (!item) {
release();
return [2 /*return*/, [null, 0, false]];
}
if (item.Expiration > 0) {
if (Date.now() > item.Expiration) {
release();
return [2 /*return*/, [null, 0, false]];
}
return [2 /*return*/, [item.Object, item.Expiration, true]];
}
release();
return [2 /*return*/, [item.Object, 0, true]];
}
});
});
};
Cache.prototype.get = function (k) {
var item = this.items[k];
if (!item) {
return [null, false];
}
if (item.Expiration > 0) {
if (Date.now() > item.Expiration) {
return [null, false];
}
}
return [item.Object, true];
};
Cache.prototype.Add = function (k, x, d) {
return __awaiter(this, void 0, void 0, function () {
var release, _a, _, found;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _b.sent();
_a = this.get(k), _ = _a[0], found = _a[1];
if (found) {
release();
throw new Error("Item " + k + " already exists");
}
this.set(k, x, d);
release();
return [2 /*return*/, null];
}
});
});
};
Cache.prototype.Replace = function (k, x, d) {
return __awaiter(this, void 0, void 0, function () {
var release, _a, _, found;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _b.sent();
_a = this.get(k), _ = _a[0], found = _a[1];
if (!found) {
release();
throw new Error("Item " + k + " doesn't exist");
}
this.set(k, x, d);
release();
return [2 /*return*/, null];
}
});
});
};
Cache.prototype.Delete = function (k) {
return __awaiter(this, void 0, void 0, function () {
var release, _a, obj, evicted;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _b.sent();
_a = this.delete(k), obj = _a[0], evicted = _a[1];
release();
if (evicted) {
this.onEvicted(k, obj);
}
return [2 /*return*/];
}
});
});
};
Cache.prototype.delete = function (k) {
if (this.onEvicted) {
var item = this.items[k];
if (item) {
delete this.items[k];
return [item.Object, true];
}
}
delete this.items[k];
return [null, false];
};
Cache.prototype.DeleteExpired = function () {
return __awaiter(this, void 0, void 0, function () {
var evictedItems, now, release, k, v, _a, ov, evicted, i, v;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
evictedItems = [];
now = Date.now();
return [4 /*yield*/, mutex.acquire()];
case 1:
release = _b.sent();
for (k in this.items) {
v = this.items[k];
if (v.Expiration > 0 && now > v.Expiration) {
_a = this.delete(k), ov = _a[0], evicted = _a[1];
if (evicted) {
evictedItems.push({ key: k, value: v });
}
}
}
release();
for (i = 0; i < evictedItems.length; i += 1) {
v = evictedItems[i];
this.onEvicted(v.key, v.value);
}
return [2 /*return*/];
}
});
});
};
Cache.prototype.OnEvicted = function (fn) {
return __awaiter(this, void 0, void 0, function () {
var release;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _a.sent();
this.onEvicted = fn;
release();
return [2 /*return*/];
}
});
});
};
Cache.prototype.Items = function () {
return __awaiter(this, void 0, void 0, function () {
var release, validItems, now, k, v;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _a.sent();
validItems = {};
now = Date.now();
for (k in this.items) {
v = this.items[k];
if (v.Expiration > 0 && now > v.Expiration) {
continue;
}
validItems[k] = v;
}
release();
return [2 /*return*/, validItems];
}
});
});
};
Cache.prototype.ItemCount = function () {
return __awaiter(this, void 0, void 0, function () {
var release, length;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _a.sent();
length = Object.keys(this.items).length;
release();
return [2 /*return*/, length];
}
});
});
};
Cache.prototype.Flush = function () {
return __awaiter(this, void 0, void 0, function () {
var release;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, mutex.acquire()];
case 1:
release = _a.sent();
this.items = {};
release();
return [2 /*return*/];
}
});
});
};
Cache.prototype.stopJanitor = function () {
this.janitor.stop = true;
};
Cache.prototype.runJanitor = function (interval) {
this.janitor = new Janitor(interval, false);
this.janitor.Run(this);
};
return Cache;
}());
exports.Cache = Cache;
var Janitor = /** @class */ (function () {
function Janitor(Interval, stop) {
this.Interval = Interval;
this.stop = stop;
}
Janitor.prototype.Run = function (cache) {
var _this = this;
var ticker = setInterval(function () {
if (_this.stop) {
clearInterval(ticker);
return;
}
cache.DeleteExpired();
}, this.Interval);
};
return Janitor;
}());
function newCache(de, m) {
if (de === 0) {
de = NoExpiration;
}
var c = new Cache(de, m);
return c;
}
function newCacheWithJanitor(de, ci, m) {
var c = newCache(de, m);
if (ci > 0) {
c.runJanitor(ci);
}
return c;
}
function New(defaultExpiration, cleanUpInterval) {
var items = {};
return newCacheWithJanitor(defaultExpiration, cleanUpInterval, items);
}
exports.New = New;
function NewFrom(defaultExpiration, cleanUpInterval, items) {
return newCacheWithJanitor(defaultExpiration, cleanUpInterval, items);
}
exports.NewFrom = NewFrom;
//# sourceMappingURL=index.js.map