{"name":"rache","version":"0.0.3","author":{"name":"Holepunch"},"license":"Apache-2.0","_id":"rache@0.0.3","homepage":"https://github.com/holepunchto/rache#readme","bugs":{"url":"https://github.com/holepunchto/rache/issues"},"dist":{"shasum":"301f646202595c1f8440fbfd95b484776f97fdda","tarball":"http://registry-npm.ywwl.com/rache/download/rache-0.0.3.tgz","fileCount":4,"integrity":"sha512-QxLfsUZK4e1BoMZ6GfxBxLrpeTBcZHPK5oUb1aPPozAJyJxx5BNT3p/ANgPcHnZZZebX+hnPLTgEEMmQOwgP9w==","signatures":[{"sig":"MEUCIQDpP94mbqkGO/1lpegXJa9KMYNOVc4ocLsJT0nrMJqMEgIgUtoCxKkbkRD55ypQ9b5a9ZUnujWhasFusiky16gV6t0=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":16935,"size":6113,"noattachment":false,"key":"/rache/-/rache-0.0.3.tgz"},"main":"index.js","gitHead":"62d4ccbdd9d83a39b0e7cc300edd17c37c9dd49c","scripts":{"test":"standard && brittle test.js --coverage"},"_npmUser":{"name":"hdegroote","email":"hans_npm@mailbox.org"},"repository":{"url":"git+https://github.com/holepunchto/rache.git","type":"git"},"_npmVersion":"9.3.0","description":"Random-eviction cache","directories":{},"_nodeVersion":"18.12.1","dependencies":{},"_hasShrinkwrap":false,"devDependencies":{"brittle":"^3.5.2","standard":"^17.1.0"},"_npmOperationalInternal":{"tmp":"tmp/rache_0.0.3_1720023592274_0.2483442570229326","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":1720023592404,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"hdegroote","email":"hans_npm@mailbox.org"}],"_cnpm_publish_time":1720023592404,"dist-tags":{"latest":"1.0.1"}}