{"_id":"@cababunga/lru","name":"@cababunga/lru","dist-tags":{"latest":"0.5.1"},"versions":{"0.5.1":{"name":"@cababunga/lru","version":"0.5.1","description":"In process cache with least-recently-used eviction logic, O(1) complexity for all operations and optional compression.","scripts":{"test":"mocha *.test.js","bump":"npm version ${BUMP:=patch} && git push && git push --tags"},"repository":{"type":"git","url":"git+https://github.com/cababunga/base-lru.git"},"license":"ISC","devDependencies":{"@types/node":"^20.11.25"},"_id":"@cababunga/lru@0.5.1","gitHead":"09effbae94befabaf829a451ae5f47c9ee7a0f78","bugs":{"url":"https://github.com/cababunga/base-lru/issues"},"homepage":"https://github.com/cababunga/base-lru#readme","_nodeVersion":"20.5.1","_npmVersion":"9.8.0","dist":{"integrity":"sha512-BJ5UQDj1mcyy647F/SZ55idGP45iTxSu7+ccoKosbLsNqvJJN5e4O3fU7D7qUQpxPnt+YChKp7FHV41Ho9SeGg==","shasum":"8ef3f48932b990f81a5062c5f1750e87cd268925","tarball":"https://registry.npmjs.org/@cababunga/lru/-/lru-0.5.1.tgz","fileCount":4,"unpackedSize":16282,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDjWxwNmFnSvb11W4abEG+wHdvp6m1qmaVMmAb4ZnO2xgIgUCoQ8kbaR+y+BmS/925NMG6vyZw2CLJm5O1cnGI18F0="}]},"_npmUser":{"name":"cababunga","email":"alexmv@gmail.com"},"directories":{},"maintainers":[{"name":"cababunga","email":"alexmv@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/lru_0.5.1_1711275495156_0.9035062035651571"},"_hasShrinkwrap":false}},"time":{"created":"2024-03-24T10:18:15.020Z","0.5.1":"2024-03-24T10:18:15.307Z","modified":"2024-03-24T10:18:15.839Z"},"maintainers":[{"name":"cababunga","email":"alexmv@gmail.com"}],"description":"In process cache with least-recently-used eviction logic, O(1) complexity for all operations and optional compression.","homepage":"https://github.com/cababunga/base-lru#readme","repository":{"type":"git","url":"git+https://github.com/cababunga/base-lru.git"},"bugs":{"url":"https://github.com/cababunga/base-lru/issues"},"license":"ISC","readme":"# In-process cache\n\nIn process cache with least-recently-used eviction logic, O(1) complexity for all operations and optional compression.\n\n## Install\n\n```sh\nnpm i @cababunga/lru\n```\n\n## Use\n\n```javascript\nconst lru = require(\"@cababunga/lru\");\nconst expensiveCall = async (a, b) => \n    new Promise(resolve => setTimeout(() => resolve(a * b), 1000));\nconst cache = new lru({log: console.log});\nconst cachedCall = cache.cachify(expensiveCall);\nawait cachedCall(2, 2);\nawait cachedCall(2, 2);\nawait cachedCall(2, 2);\n```\n\nConstructor Options:\n\n- exp - caching duration in seconds [30*60]\n- max - maximum memory to use for cache [1MiB]\n- log - function to use for debug logging [()=>{}]\n- codec - object with optional functions\n- codec.encode - takes an object and returns its serialized representation as a buffer\n- codec.decode - takes a buffer created by .encode and decodes it into an object\n- codec.serialize - data serializer [JSON.stringify()]\n- codec.parse - cache entry parser [JSON.parse()]\n- codec.compress - compress cache entry before storage [no compression]\n- codec.decompress - decompress cache entry after retrieval [no decompression]\n- codec.makeKey - takes an array (of function arguments) and produces sufficiently unique digest suitable to be used as a cache key [hex(md5(JSON.stringify()))]\n- codec.log - function that can be used for logging some cache serialization/compression timing [opt.log]\n\n### Compression\n\nTo avoid unnecessary dependencies, compression is not part of the package, but you can provide compressor and decompressor functions in the constructor options. Here is an example of how it could be done.\n\n```javascript\nconst lz4 = require(\"lz4\");\nconst lru = require(\"@cababunga/lru\");\nconst compress = buf => {\n    if (buf.length < 128)\n        return Buffer.concat([Buffer.from(\"\\x00\"), buf]);\n\n    return Buffer.concat([Buffer.from(\"\\x01\"), lz4.encode(buf)]);\n}\nconst decompress = buf => {\n    const compression = buf[0];\n    buf = buf.slice(1);\n    if (compression == 0)\n        return buf;\n\n    if (compression == 1)\n        return lz4.decode(buf);\n\n    throw new Error(\"Unknown compression type: \" + JSON.stringify(compression));\n}\nconst cache = new lru({codec: {compress, decompress}});\n```\n","readmeFilename":"README.md"}