{"name":"rache","version":"0.0.0","author":"","license":"Apache-2.0","_id":"rache@0.0.0","dist":{"shasum":"737a5f0e1752b59b9f07636be6bb7ee17e02f4f0","tarball":"http://registry-npm.ywwl.com/rache/download/rache-0.0.0.tgz","fileCount":1,"integrity":"sha512-rS6VfNU/LYK6XrRzEjvBmUm3qTerQJooERsz4h3DENJUpQ+GzfCD5kVDSqsOT+H/zsyWcxCiamHMVZ9k7aam3A==","signatures":[{"sig":"MEYCIQCAnFKWgiCfjzDsvcZRZzA1YfrCL35vsqxmCMtYGHJz3AIhALHGLF5WkXMHNysXQKqETRe2uy2DR/FKVRCO1nz6vg67","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":229,"size":249,"noattachment":false,"key":"/rache/-/rache-0.0.0.tgz"},"main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"_npmVersion":"10.7.0","description":"Random-eviction cache","directories":{},"_nodeVersion":"20.11.1","_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/rache_0.0.0_1719931117799_0.6964390999254779","host":"s3://npm-registry-packages"},"readme":"# Rache\n\nA random-eviction cache which imposes a global cache-size limit on all the caches derived from it.\n\nUseful when you have many caches and want to limit their memory usage as a whole, instead of per cache.\n\nThe `get` and `set` operations as well as `delete` (and cache eviction) take constant time.\n\n## Install\n\n```\nnpm i rache\n```\n\n## Usage\n\n```\nconst Rache = require('rache')\n\nconst cache = new Rache({ maxSize: 3 })\nconst cache2 = cache.sub()\nconst cache3 = cache.sub()\n\ncache.set('key', 'value')\ncache2.set('key', 'otherValue')\ncache3.set('some', 'thing')\n\n// cache 1 is a separate cache from cache2 and cache3\nconsole.log('cached:', cache.get('key')) // 'value'\n\n// But they share the same global size\nconsole.log(cache.globalSize, 'of', cache.maxSize) // 3 of 3\n\ncache.set('key2', 'another value')\n// The cache was full, so one of the existing 3 entries got evicted\n\nconsole.log(cache.globalSize, 'of', cache.maxSize) // 3 of 3\n```\n\n## API\n\n#### `const cache = new Rache({ maxSize=65536 })`\n\nCreate a new cache.\n\n`maxSize` is the maximum amount of entries globally, across this cache and all derived caches (derived with `cache.sub()` or `Rache.from(cache)`).\n\n#### `const subCache = cache.sub()`\n\nCreate a new cache which shares the global memory limit with the original cache.\n\n#### `const aCache = Rache.from(cache?)`\n\nCreate a new rache instance.\n\nIf an existing cache is passed in, it will create a sub-cache (equivalent to `aCache = cache.sub()`).\n\nOtherwise (if no `cache` or a falsy value is passed in), it will create a new cache (equivalent to `aCache = new Rache()`).\n\n\n#### `cache.globalSize`\n\nThe current amount of entries across all caches.\n\n#### `cache.maxSize`\n\nThe maximum amount of entries across all caches.\n\n#### `cache.size`\n\nThe amount of entries in this specific cache.\n\n#### `cache.set(key, value)`\n\nSet the key to the given value.\n\nIf the global cache size was at the limit, a random old entry is evicted from the cache.\n\n#### `cache.delete(key)`\n\nDelete the entry corresponding to `key`, if any.\n\nReturns `true` if an entry was deleted, `false` otherwise.\n\n#### `cache.get(key)`\n\nReturns the value corresponding to the given key, or `undefined` if there is none.\n\n#### `cache.keys()`\n\nReturns an iterator over the keys of this particular cache.\n\n#### `cache.values()`\n\nReturns an iterator over the values of this particular cache.\n\n#### `cache.clear()`\n\nClear all entries from this particular cache.\n\nAfter clearing, new entries can again be added to the cache.\n\n#### `cache.destroy()`\n\nDestroys the cache. The cache should no longer be used after it has been destroyed.\n","publish_time":1719931117965,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"hdegroote","email":"hans_npm@mailbox.org"}],"_cnpm_publish_time":1719931117965,"dist-tags":{"latest":"1.0.1"}}