{"name":"rache","version":"1.0.0","description":"Random-eviction cache","main":"index.js","scripts":{"test":"standard && brittle test.js --coverage"},"repository":{"type":"git","url":"git+https://github.com/holepunchto/rache.git"},"author":{"name":"Holepunch"},"license":"Apache-2.0","bugs":{"url":"https://github.com/holepunchto/rache/issues"},"homepage":"https://github.com/holepunchto/rache#readme","dependencies":{},"devDependencies":{"brittle":"^3.5.2","standard":"^17.1.0"},"gitHead":"f657cbeb55599f3f197ec2441874e67bed94b2b6","_id":"rache@1.0.0","_nodeVersion":"18.12.1","_npmVersion":"9.3.0","dist":{"integrity":"sha512-e0k0g0w/8jOCB+7YqCIlOa+OJ38k0wrYS4x18pMSmqOvLKoyhmMhmQyCcvfY6VaP8D75cqkEnlakXs+RYYLqNg==","shasum":"e56f95abc0c056d3b86c9fdad6a004ce87921e30","tarball":"http://registry-npm.ywwl.com/rache/download/rache-1.0.0.tgz","fileCount":4,"unpackedSize":17032,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCvt0wAhe3JwIJEslYbCOi1AEiVT4E3/665kW6eDgwvKAIhAO7ob6Mge2MVsPgYuDqp++pnhn6pBVDsIDCI33LTgOCf"}],"size":6139,"noattachment":false,"key":"/rache/-/rache-1.0.0.tgz"},"_npmUser":{"name":"hdegroote","email":"hans_npm@mailbox.org"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/rache_1.0.0_1720117883639_0.2522593063309564"},"_hasShrinkwrap":false,"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":1720117883783,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"hdegroote","email":"hans_npm@mailbox.org"}],"_cnpm_publish_time":1720117883783,"dist-tags":{"latest":"1.0.1"}}