UNPKG

ram64

Version:

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

56 lines (55 loc) 1.44 kB
"use strict"; exports.processRequest = processRequest; var _commands = require("./commands"); var _getShard = require("./get-shard"); var _isRam64Message = require("../client/is-ram64-message"); var _lru = require("./lru"); let msgSinceEvict = 0; async function processRequest(port, req) { if (!(0, _isRam64Message).isRAM64Message(req)) return; const { key , commandIndex , requestId , args } = req; const cmd = _commands.commands[commandIndex ?? -1]; if (!cmd?.fn) return void port.postMessage({ ram64: true, requestId, error: `Command '${commandIndex}' not implemented` }); let shard, value, error; if (key) { shard = (0, _getShard).getShardFromKey(key); } if (cmd.promise) { value = await cmd.fn({ shard, key, args }).catch((err)=>{ error = err; }); } else { try { value = cmd.fn({ shard, key, args }); } catch (err) { error = err; } } const response = { ram64: true, requestId, value, error }; port.postMessage(response); if (_lru.isLRUEnabled) { msgSinceEvict++; if (msgSinceEvict > 100) { msgSinceEvict = 0; (0, _lru).evict(); } } } //# sourceMappingURL=process-request.js.map