{"id":13465112,"url":"https://github.com/tinovyatkin/tlru","last_synced_at":"2026-01-18T12:53:26.285Z","repository":{"id":35076583,"uuid":"204020478","full_name":"tinovyatkin/tlru","owner":"tinovyatkin","description":"Time aware least recently used (TLRU) cache for Node","archived":false,"fork":false,"pushed_at":"2023-11-09T10:46:21.000Z","size":2135,"stargazers_count":13,"open_issues_count":8,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T13:51:25.114Z","etag":null,"topics":["cache","lru","lru-cache","nodejs","settimeout","ttl","ttl-cache","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/tinovyatkin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-08-23T15:02:52.000Z","updated_at":"2024-07-11T14:27:45.000Z","dependencies_parsed_at":"2024-01-16T05:42:50.535Z","dependency_job_id":"61816584-a9a4-4f74-9288-1c04a811af90","html_url":"https://github.com/tinovyatkin/tlru","commit_stats":{"total_commits":596,"total_committers":5,"mean_commits":119.2,"dds":"0.13422818791946312","last_synced_commit":"5a0663795fc694af156cfbd2b545e978df4268c4"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinovyatkin%2Ftlru","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinovyatkin%2Ftlru/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinovyatkin%2Ftlru/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tinovyatkin%2Ftlru/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tinovyatkin","download_url":"https://codeload.github.com/tinovyatkin/tlru/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251119742,"owners_count":21539215,"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","nodejs","settimeout","ttl","ttl-cache","typescript"],"created_at":"2024-07-31T14:00:59.903Z","updated_at":"2026-01-18T12:53:26.275Z","avatar_url":"https://github.com/tinovyatkin.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"[![codecov](https://codecov.io/gh/tinovyatkin/tlru/branch/master/graph/badge.svg)](https://codecov.io/gh/tinovyatkin/tlru)\n\n# tlru\n\nTime aware least recently used [TLRU](\u003chttps://en.wikipedia.org/wiki/Cache_replacement_policies#Time_aware_least_recently_used_(TLRU)\u003e) cache for Node.JS\n\nSmall (~ 70 lines of TypeScript), fast (_extends_ native `Map` class, maintaining the same speed for `set`, `get` and `has` operations), memory efficient (don't uses an internal linked-list for LRU, but instead uses just one timer per class instance via [fun-dispatcher](https://github.com/tinovyatkin/fun-dispatcher) and proactively performs cache eviction - so great for caching large objects)\n\n## Usage as LRU cache\n\nTLRU may be seen as regular LRU cache where all cached items have the same `maxAge` as whole cache, and, effectively, this library can be used as efficient LRU cache:\n\n```js\nconst { TLRU } = require(\"tlru\");\n\nconst lru = new TLRU({ defaultLRU: true, maxStoreSize: 2 });\n\nlru.set(\"a\", 1);\nlru.set(\"b\", 2);\nexpect(lru.get(\"a\")).toBe(1);\nlru.delete(\"a\");\nlru.set(\"c\", 1);\nlru.set(\"d\", 1);\n\n// TLRU prunes cache lazily, so, need to give some time\nsetImmediate(() =\u003e {\n  expect([...lru.keys()]).toEqual([\"c\", \"d\"]);\n});\n\n// Peek item without affecting it's LRU rating\nlru.get(\"a\", false /* this is the flag */);\n\n// all native `Map` methods are here\nlru.delete(\"a\");\nconsole.log([...lru.entries()]); // [[key, value], ...]\n\n// Serializes to JSON, sorted by LRU rating\nconsole.log(JSON.stringify(lru)); // [[key1, value1], [key2, value2], ...]\n```\n\nSee more examples at `__tests__/lru.test.ts`\n\n## Usage as TLRU\n\nEach item in cache can have its own TTU (time to usage or time to live).\n\n```js\nconst { TLRU } = require(\"tlru\");\n\nconst lru = new TLRU({ maxStoreSize: 4, maxAgeMs: 1000 });\n\nlru.set(\"a\", 1, 500);\nlru.set(\"b\", 2, 700);\nlru.set(\"c\", 3); // default TTU = maxAgeMs\n\nsetTimeout(() =\u003e {\n  expect(lru.has(\"a\")).toBeFalsy();\n  expect(lru.get(\"b\")).toBe(2);\n  expect(lru.has(\"c\")).toBeTruthy();\n  expect(lru.size).toBe(2);\n}, 600);\n\nsetTimeout(() =\u003e {\n  expect(lru.has(\"b\")).toBeFalsy();\n  expect(lru.has(\"c\")).toBeTruthy();\n  expect(lru.size).toBe(1);\n}, 800);\n```\n\nItems can be revived to original TTU on getting, so, you will have LRU/TRLU hybrid:\n\n```js\nconst { TLRU } = require(\"tlru\");\n\nconst lru = new TLRU({ maxStoreSize: 4, maxAgeMs: 1000 });\nlru.set(\"a\", 1, 500);\nlru.set(\"b\", 2, 700);\nlru.set(\"c\", 3); // default TTU = maxAgeMs\n\nsetTimeout(() =\u003e {\n  expect(lru.has(\"a\")).toBeFalsy();\n  expect(lru.get(\"b\", true /* revive flag - reset TTU */)).toBe(2); // 'b' got new 700 ms of life\n  expect(lru.size).toBe(2);\n}, 600);\n\nsetTimeout(() =\u003e {\n  expect(lru.has(\"b\")).toBeTruthy();\n  expect(lru.has(\"c\")).toBeFalsy(); // should be evicted by cache maxAgeMs default\n  expect(lru.size).toBe(1);\n}, 1200);\n\nsetTimeout(() =\u003e {\n  expect(lru.size).toBe(0);\n}, 1500);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinovyatkin%2Ftlru","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftinovyatkin%2Ftlru","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftinovyatkin%2Ftlru/lists"}