UNPKG

angular4-hal

Version:

This Angular module offers a HAL/JSON http-client to easily interact with a Spring Data Rest API or any API that implements the Spring Data Rest resource model

104 lines (103 loc) 4.01 kB
import * as hash from 'hash.js'; export var EvictStrategy; (function (EvictStrategy) { EvictStrategy[EvictStrategy["EvictTrivial"] = 0] = "EvictTrivial"; EvictStrategy[EvictStrategy["EvictSmart"] = 1] = "EvictSmart"; })(EvictStrategy || (EvictStrategy = {})); var CacheHelper = /** @class */ (function () { function CacheHelper() { } CacheHelper.initClearCacheProcess = function () { var _this = this; if (this.isActive) { setInterval(function () { Date.now(); if (CacheHelper.evictStrategy == EvictStrategy.EvictTrivial) _this.evictAll(); else if (CacheHelper.evictStrategy == EvictStrategy.EvictSmart) { _this.cacheMap.forEach(function (value, key) { if (value.expire > 0 && Date.now() > value.expire) _this.evict(key); }); } }, 15 * 60 * 1000); } }; CacheHelper.ifPresent = function (link, body, params, isActiveLocal) { if (isActiveLocal === void 0) { isActiveLocal = true; } if (!this.isActive || !isActiveLocal) return false; return this.cacheMap.has(CacheHelper.key(link, body, params)); }; CacheHelper.getArray = function (link, body, params) { return this.cacheMap.get(CacheHelper.key(link, body, params)).entity; }; CacheHelper.putArray = function (link, array, expireMs, body, params) { if (expireMs === void 0) { expireMs = 10 * 60 * 1000; } if (this.isActive) { var resourceExpire = { entity: array, expire: CacheHelper.expireDate(expireMs) }; this.cacheMap.set(CacheHelper.key(link, body, params), resourceExpire); } }; CacheHelper.get = function (link, body, params) { return this.cacheMap.get(CacheHelper.key(link, body, params)).entity; }; CacheHelper.put = function (link, array, expireMs, body, params) { if (expireMs === void 0) { expireMs = 10 * 60 * 1000; } if (this.isActive) { var resourceExpire = { entity: array, expire: CacheHelper.expireDate(expireMs) }; this.cacheMap.set(CacheHelper.key(link, body, params), resourceExpire); } }; CacheHelper.expireDate = function (expireMs) { if (expireMs == 0) return 0; return Date.now() + expireMs; }; CacheHelper.key = function (link, body, halOptions) { var k = link; if (body) k += body; if (halOptions) k += CacheHelper.toStringParams(halOptions); var key = hash.sha256().update(k).digest('hex'); return key; }; CacheHelper.toStringParams = function (options) { var s = ''; if (options.size) { s = 'size=' + options.size.toString() + '&'; } if (options.notPaged) { s += 'notPaged=true&'; } if (options.params) { options.params.forEach(function (param) { s += param.key + '=' + param.value + '&'; }); } if (options.sort) { options.sort.forEach(function (sortInfo) { var sortString = ''; sortString = sortInfo.path ? sortString.concat(sortInfo.path) : sortString; sortString = sortInfo.order ? sortString.concat(',').concat(sortInfo.order) : sortString; s += 'sort' + sortString + '&'; }); } return s; }; CacheHelper.evict = function (key) { this.cacheMap.delete(key); }; CacheHelper.evictAll = function () { this.cacheMap.clear(); }; CacheHelper.cacheMap = new Map(); CacheHelper.isActive = true; // TODO CacheHelper.maxEntries = 100; CacheHelper.evictStrategy = EvictStrategy.EvictTrivial; CacheHelper.defaultExpire = 10 * 60 * 1000; //10 minutes return CacheHelper; }()); export { CacheHelper };