UNPKG

hazelcast-client

Version:

Hazelcast - open source In-Memory Data Grid - client for NodeJS

188 lines 6.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Config_1 = require("../Config"); var Util_1 = require("../Util"); var DataStoreHashMap_1 = require("../DataStoreHashMap"); var DataRecord = (function () { function DataRecord(key, value, creationTime, ttl) { this.key = key; this.value = value; if (creationTime) { this.creationTime = creationTime; } else { this.creationTime = new Date().getTime(); } if (ttl) { this.expirationTime = this.creationTime + ttl * 1000; } else { this.expirationTime = undefined; } this.lastAccessTime = this.creationTime; this.accessHit = 0; } DataRecord.lruComp = function (x, y) { return x.lastAccessTime - y.lastAccessTime; }; DataRecord.lfuComp = function (x, y) { return x.accessHit - y.accessHit; }; DataRecord.randomComp = function (x, y) { return Math.random() - 0.5; }; DataRecord.prototype.isExpired = function (maxIdleSeconds) { var now = new Date().getTime(); if ((this.expirationTime > 0 && this.expirationTime < now) || (maxIdleSeconds > 0 && this.lastAccessTime + maxIdleSeconds * 1000 < now)) { return true; } else { return false; } }; DataRecord.prototype.setAccessTime = function () { this.lastAccessTime = new Date().getTime(); }; DataRecord.prototype.hitRecord = function () { this.accessHit++; }; return DataRecord; }()); exports.DataRecord = DataRecord; var NearCacheImpl = (function () { function NearCacheImpl(nearCacheConfig, serializationService) { this.evictedCount = 0; this.expiredCount = 0; this.missCount = 0; this.hitCount = 0; this.serializationService = serializationService; this.name = nearCacheConfig.name; this.invalidateOnChange = nearCacheConfig.invalidateOnChange; this.maxIdleSeconds = nearCacheConfig.maxIdleSeconds; this.inMemoryFormat = nearCacheConfig.inMemoryFormat; this.timeToLiveSeconds = nearCacheConfig.timeToLiveSeconds; this.evictionPolicy = nearCacheConfig.evictionPolicy; this.evictionMaxSize = nearCacheConfig.evictionMaxSize; this.evictionSamplingCount = nearCacheConfig.evictionSamplingCount; this.evictionSamplingPoolSize = nearCacheConfig.evictionSamplingPoolSize; if (this.evictionPolicy === Config_1.EvictionPolicy.LFU) { this.compareFunc = DataRecord.lfuComp; } else if (this.evictionPolicy === Config_1.EvictionPolicy.LRU) { this.compareFunc = DataRecord.lruComp; } else if (this.evictionPolicy === Config_1.EvictionPolicy.RANDOM) { this.compareFunc = DataRecord.randomComp; } else { this.compareFunc = undefined; } this.evictionCandidatePool = []; this.internalStore = new DataStoreHashMap_1.DataKeyedHashMap(); } NearCacheImpl.prototype.put = function (key, value) { this.doEvictionIfRequired(); if (this.inMemoryFormat === Config_1.InMemoryFormat.OBJECT) { value = this.serializationService.toObject(value); } else { value = this.serializationService.toData(value); } var dr = new DataRecord(key, value, undefined, this.timeToLiveSeconds); this.internalStore.set(key, dr); }; NearCacheImpl.prototype.get = function (key) { var dr = this.internalStore.get(key); if (dr === undefined) { this.missCount++; return undefined; } if (dr.isExpired(this.maxIdleSeconds)) { this.expireRecord(key); this.missCount++; return undefined; } dr.setAccessTime(); dr.hitRecord(); this.hitCount++; if (this.inMemoryFormat === Config_1.InMemoryFormat.BINARY) { return this.serializationService.toObject(dr.value); } else { return dr.value; } }; NearCacheImpl.prototype.invalidate = function (key) { this.internalStore.delete(key); }; NearCacheImpl.prototype.clear = function () { this.internalStore.clear(); }; NearCacheImpl.prototype.isEvictionRequired = function () { return this.evictionPolicy !== Config_1.EvictionPolicy.NONE && this.evictionMaxSize <= this.internalStore.size; }; NearCacheImpl.prototype.doEvictionIfRequired = function () { if (!this.isEvictionRequired()) { return; } var internalSize = this.internalStore.size; if (this.recomputeEvictionPool() > 0) { return; } else { this.evictRecord(this.evictionCandidatePool[0].key); this.evictionCandidatePool = this.evictionCandidatePool.slice(1); } }; NearCacheImpl.prototype.recomputeEvictionPool = function () { var arr = Array.from(this.internalStore.values()); Util_1.shuffleArray(arr); var newCandidates = arr.slice(0, this.evictionSamplingCount); var cleanedNewCandidates = newCandidates.filter(this.filterExpiredRecord, this); var expiredCount = newCandidates.length - cleanedNewCandidates.length; if (expiredCount > 0) { return expiredCount; } (_a = this.evictionCandidatePool).push.apply(_a, cleanedNewCandidates); this.evictionCandidatePool.sort(this.compareFunc); this.evictionCandidatePool = this.evictionCandidatePool.slice(0, this.evictionSamplingPoolSize); return 0; var _a; }; NearCacheImpl.prototype.filterExpiredRecord = function (candidate) { if (candidate.isExpired(this.maxIdleSeconds)) { this.expireRecord(candidate.key); return false; } else { return true; } }; NearCacheImpl.prototype.expireRecord = function (key) { if (this.internalStore.delete(key)) { this.expiredCount++; } }; NearCacheImpl.prototype.evictRecord = function (key) { if (this.internalStore.delete(key)) { this.evictedCount++; } }; NearCacheImpl.prototype.isInvalidatedOnChange = function () { return this.invalidateOnChange; }; NearCacheImpl.prototype.getStatistics = function () { var stats = { evictedCount: this.evictedCount, expiredCount: this.expiredCount, missCount: this.missCount, hitCount: this.hitCount, entryCount: this.internalStore.size }; return stats; }; return NearCacheImpl; }()); exports.NearCacheImpl = NearCacheImpl; //# sourceMappingURL=NearCache.js.map