UNPKG

ram64

Version:

Multi-threaded 64bit memory cache database inspired by redis-like features

27 lines (26 loc) 1.04 kB
"use strict"; exports.evict = evict; exports.isLRUEnabled = void 0; var _workerData = require("./worker-data"); var _shards = require("./shards"); var _process = require("process"); const maxMemory = _workerData.workerData.maxMemory ?? Infinity; const EVICTIONS_PER_CYCLE = 200; // should be sufficiently aggressive with minimal impact on performance const isLRUEnabled = (_workerData.workerData.maxMemory ?? 0) > 0; exports.isLRUEnabled = isLRUEnabled; function evict() { let shardI = 0; if ((0, _process).memoryUsage().rss > maxMemory) { let cycle = 0; while(cycle < EVICTIONS_PER_CYCLE){ let { map } = _shards.Shards[shardI]; const nextKey = map.keys().next().value; if (nextKey !== undefined) { map.delete(nextKey); } cycle++; // next shard regardless if there was a delete -- avoids poor memory settings causing infinite loops shardI = (shardI + 1) % _shards.Shards.length; } } } //# sourceMappingURL=lru.js.map