{"id":13725982,"url":"https://github.com/lukeed/tmp-cache","last_synced_at":"2025-10-09T18:24:47.644Z","repository":{"id":57152509,"uuid":"129490631","full_name":"lukeed/tmp-cache","owner":"lukeed","description":"A least-recently-used cache in 35 lines of code~!","archived":false,"fork":false,"pushed_at":"2020-09-08T16:45:37.000Z","size":18,"stargazers_count":177,"open_issues_count":1,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-22T16:50:56.821Z","etag":null,"topics":["cache","lru","lru-cache","mru"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lukeed.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"lukeed"}},"created_at":"2018-04-14T06:57:10.000Z","updated_at":"2025-03-13T00:37:21.000Z","dependencies_parsed_at":"2022-08-27T16:23:17.862Z","dependency_job_id":null,"html_url":"https://github.com/lukeed/tmp-cache","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Ftmp-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Ftmp-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Ftmp-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Ftmp-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukeed","download_url":"https://codeload.github.com/lukeed/tmp-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252957089,"owners_count":21831434,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","lru","lru-cache","mru"],"created_at":"2024-08-03T01:02:45.565Z","updated_at":"2025-10-09T18:24:42.592Z","avatar_url":"https://github.com/lukeed.png","language":"JavaScript","funding_links":["https://github.com/sponsors/lukeed"],"categories":["JavaScript"],"sub_categories":[],"readme":"# tmp-cache ![CI](https://github.com/lukeed/tmp-cache/workflows/CI/badge.svg) [![codecov](https://badgen.net/codecov/c/github/lukeed/tmp-cache)](https://codecov.io/gh/lukeed/tmp-cache)\n\n\u003e A least-recently-used cache in 35 lines of code~!\n\nLRU caches operate on a first-in-first-out queue. This means that the first item is the oldest and will therefore be deleted once the `max` limit has been reached.\n\nWhen a `maxAge` value is set, items are given an expiration date. This allows existing items to become stale over time which, depending on your `stale` config, is equivalent to the item not existing at all!\n\nIn order to counteract this idle decay, all `set()` and `get()` operations on an item \"refresh\" its expiration date. By doing so, a new `expires` value is issued \u0026 the item is moved to the end of the list \u0026mdash; aka, it's the newest kid on the block!\n\n\n## Install\n\n```\n$ npm install --save tmp-cache\n```\n\n\n## Usage\n\n```js\nconst Cache = require('tmp-cache');\n\nlet cache = new Cache(3); // sets \"max\" size\n\ncache.set('a', 1); //~\u003e ['a']\ncache.set('b', 2); //~\u003e ['a', 'b']\ncache.set('c', 3); //~\u003e ['a', 'b', 'c']\ncache.get('a');    //~\u003e ['b', 'c', 'a']\ncache.set('d', 4); //~\u003e ['c', 'a', 'd']\ncache.peek('a');   //~\u003e ['c', 'a', 'd']\ncache.delete('d'); //~\u003e ['c', 'a']\ncache.has('d');    //=\u003e false\ncache.set('e', 5); //~\u003e ['c', 'a', 'e']\ncache.size;        //=\u003e 3\ncache.clear();     //~\u003e []\n\ncache = new Cache({ maxAge:10 });\n\ncache.set(123, 'hello'); //~\u003e valid for 10ms\ncache.get(123); //=\u003e 'hello'  --  resets 10ms counter\nsetTimeout(_ =\u003e cache.get(123), 25); //=\u003e undefined\n\ncache = new Cache({ maxAge:0, stale:true });\n\ncache.set('foo', [123]); //~\u003e already stale, 0ms lifespan\ncache.get('foo'); //=\u003e [123]  --  because options.stale\ncache.get('foo'); //=\u003e undefined  --  previous op flagged removal\n```\n\n## API\n\nAside from the items \u0026 changes mentioned below, `tmp-cache` extends the `Map` class, so all [properties and methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Map_instances) are inherited.\n\n### Cache(options)\n\nReturns: `Cache extends Map`\n\n#### options.max\n\nType: `Number`\u003cbr\u003e\nDefault: `Infinity`\n\nThe maximum number of items the cache will hold. Adding more entries will force the oldest, least-recently-used item to be purged.\n\nFailure to include any `max` restriction could potentially allow infinite unique entries! They will only be purged based on their `expires` value (if set).\n\n\u003e **Note:** If `options` is an integer, then it is used as the `options.max` value.\n\n#### options.maxAge\n\nType: `Number`\u003cbr\u003e\nDefault: `-1`\n\nThe maximum age (in ms) an item is considered valid; aka, its lifespan.\n\nItems are not pro-actively pruned out as they age, but if you try to access an item that has expired, it will be purged and, by default, result in an `undefined` response.\n\n#### options.stale\n\nType: `Boolean`\u003cbr\u003e\nDefault: `false`\n\nAllow an expired/stale item's value to be returned before deleting it.\n\n\n### Cache.set(key, value, maxAge?)\n\nPersists the item and its value into the Cache. If a `maxAge` value exists (via custom or cache-level options), an expiration date will also be stored.\n\nWhen setting or updating an item that already exists, the original is removed. This allows the new item to be unique \u0026 the most recently used!\n\n#### key\nType: `String`\n\nThe item's unique identifier.\n\n#### value\nType: `Mixed`\n\nThe item's value to cache.\n\n#### maxAge\nType: `Number`\u003cbr\u003e\nDefault: `options.maxAge`\n\nOptionally override the [`options.maxAge`](#optionsmaxage) for this (single) operation.\n\n\n### Cache.get(key, mutate?)\n\nRetrieve an item's value by its key name. By default, this operation will refresh/update the item's expiration date.\n\nMay also return `undefined` if the item does not exist, or if it has expired \u0026 [`stale`](#optionsstale) is not set.\n\n#### key\nType: `String`\n\nThe item's unique identifier.\n\n#### mutate\nType: `Boolean`\u003cbr\u003e\nDefault: `true`\n\nRefresh the item's expiration date, marking it as _more_ recently used.\n\n\n### Cache.peek(key)\n\nReturn an item's value without updating its position or refreshing its expiration date.\n\nMay also return `undefined` if the item does not exist, or if it has expired \u0026 [`stale`](#optionsstale) is not set.\n\n#### key\nType: `String`\n\nThe item's unique identifier.\n\n\n\n## License\n\nMIT © [Luke Edwards](https://lukeed.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Ftmp-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukeed%2Ftmp-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Ftmp-cache/lists"}