{"id":13464922,"url":"https://github.com/arbazsiddiqui/lru-cache-node","last_synced_at":"2025-03-25T11:32:20.517Z","repository":{"id":31950843,"uuid":"130033638","full_name":"arbazsiddiqui/lru-cache-node","owner":"arbazsiddiqui","description":"A lighting fast cache manager for node with least-recently-used policy.","archived":false,"fork":false,"pushed_at":"2023-03-06T13:29:55.000Z","size":657,"stargazers_count":118,"open_issues_count":4,"forks_count":8,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-05T05:48:21.398Z","etag":null,"topics":["cache","hacktoberfest","lru","lru-cache","mru","node-cache"],"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/arbazsiddiqui.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-18T08:55:47.000Z","updated_at":"2023-05-23T04:36:27.000Z","dependencies_parsed_at":"2024-06-21T02:12:05.157Z","dependency_job_id":"53b5a80e-3339-4ac5-b2e9-17aa0f17a9c1","html_url":"https://github.com/arbazsiddiqui/lru-cache-node","commit_stats":{"total_commits":41,"total_committers":1,"mean_commits":41.0,"dds":0.0,"last_synced_commit":"f007399a27d17a7acbf06b594d10aea4fd716b3e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arbazsiddiqui%2Flru-cache-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arbazsiddiqui%2Flru-cache-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arbazsiddiqui%2Flru-cache-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arbazsiddiqui%2Flru-cache-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arbazsiddiqui","download_url":"https://codeload.github.com/arbazsiddiqui/lru-cache-node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245454333,"owners_count":20618012,"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","hacktoberfest","lru","lru-cache","mru","node-cache"],"created_at":"2024-07-31T14:00:52.849Z","updated_at":"2025-03-25T11:32:18.585Z","avatar_url":"https://github.com/arbazsiddiqui.png","language":"JavaScript","readme":"# lru-cache-node \n[![Build Status](https://travis-ci.org/arbazsiddiqui/lru-cache-node.svg?branch=master)](https://travis-ci.org/arbazsiddiqui/lru-cache-node)\n[![codecov](https://codecov.io/gh/arbazsiddiqui/lru-cache-node/branch/master/graph/badge.svg)](https://codecov.io/gh/arbazsiddiqui/lru-cache-node)\n[![npm](https://img.shields.io/npm/dt/lru-cache-node.svg)](https://npmjs.com/package/lru-cache-node)\n\n\u003e A lighting fast cache manager for node with least-recently-used policy.\n\nA super fast cache for node with LRU policy. Cache will keep on adding values until the `maxSize` is reached.\n\nAfter that it will start popping out the Least recently used/accessed value from the cache in order to set the new ones.\n\n\nSupports expiry and stale.\n\nImplemented using doubly-linked-list and hashmap with O(1) time complexity for gets and sets.\n\n\n## Install\n\n```\n$ npm install --save lru-cache-node\n```\n\n## Usage\n\n```js\nconst Cache = require('lru-cache-node');\n\nlet cache = new Cache(3); //set max size of cache as three\n\ncache.set('a', 7); //sets a value in cache with 'a' as key and 7 as value\ncache.set('b', 5);\ncache.set('c', 3); \n/*\n  [ { key: 'c', value: 3 },\n    { key: 'b', value: 5 },\n    { key: 'a', value: 7 } ]\n */\n \ncache.set('d', 10) // pops out a\n/*\n  [ { key: 'd', value: 10 },\n    { key: 'c', value: 3 },\n    { key: 'b', value: 5 } ]\n */\n \ncache.get(\"b\") //returns 5 and makes it most recently used\n/*\n  [ { key: 'b', value: 5 },\n    { key: 'd', value: 10 },\n    { key: 'c', value: 3 } ]\n */\n\ncache.peek(\"d\") //returns 10 but doesnt resets the order\n/*\n  [ { key: 'b', value: 5 },\n    { key: 'd', value: 10 },\n    { key: 'c', value: 3 } ]\n */\n\nlet cache = new Cache(3, 10); //Initialize Cache with size 3 and expiry for keys as 10ms\nconst sleep = ms =\u003e new Promise(r =\u003e setTimeout(r, ms));\n\ncache.set('a', 7); //valid for 10ms\ncache.get('a'); //returns 7 and resets 10ms counter\nawait sleep(15);\ncache.get('a'); //null\n\ncache.set('b', 5, 30); //overwrites cache's default expiry of 10ms and uses 30ms\nawait sleep(15);\ncache.get('b'); //returns 5 and resets the expiry of b back to 30ms\nawait sleep(35);\ncache.get('b'); //null\n```\n\n## API\n### cache(maxSize, maxAge, stale)\n\n#### `maxSize`\nType: `Number`\u003cbr\u003e\nDefault: `Infinity`\n\nMaximum size of the cache.\n\n#### `maxAge`\nType: `Number`\u003cbr\u003e\nDefault: `Infinity`\n\nDefault expiry for all keys for the cache. It does not proactively deletes expired keys, but will return null when an expired key is being accessed.\n\n#### `stale`\nType: `Boolean`\u003cbr\u003e\nDefault: `false`\n\nIf set to true, will return the value of expired key before deleting it from cache.\n\n### set(key, value, maxAge)\n\n#### `key`\n\nKey to be set.\n\n#### `value`\n\nValue for the key.\n\n#### `maxAge`\n\nExpiry of the key. Will override cache's `maxAge` if specified.\n\n### get(key)\n\nReturns the value for the key. If not key does not exist will return `null`.\n\n\u003eBoth set() and get() will update the \"recently used\"-ness and expiry of the key.\n\n### peek(key)\n\nReturns the value for the key, without making the key most recently used. If not key does not exist will return `null`.\n\n### delete(key)\nDeletes the key from the cache.\n\n### contains(key)\n\nReturns a boolean indication if the value exists in cache or not.\n\n### has(key)\n\nAlias for `contains` function.\n\n### getSize()\n\nReturns the current size of cache.\n\n### reset()\n\nClears the whole cache and reinitialize it.\n\n### toArray()\n\nReturns an array form of the catch.\n```js\nlet cache = new Cache();\ncache.set(\"a\", 5);\ncache.set(\"b\", 4);\ncache.set(\"c\", 0);\ncache.toArray()\n/*\n[ { key: 'c', value: 0 },\n  { key: 'b', value: 4 },\n  { key: 'a', value: 5 } ]\n*/\n```\n\n### forEach(callback)\n\nTakes a function and iterates over all the keys in the cache, in order of recentness. Callback takes `key`, `value` and `index` as params.\n```js\nlet cache = new Cache();\ncache.set(\"a\", 1);\ncache.set(\"b\", 2);\ncache.set(\"c\", 3);\ncache.forEach((key, value, index) =\u003e {\n\tconsole.log(key, value, index)\n})\n/*\nc 3 0\nb 2 1\na 1 2\n*/\n```\n","funding_links":[],"categories":["Javascript","JavaScript"],"sub_categories":["npm packages"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farbazsiddiqui%2Flru-cache-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farbazsiddiqui%2Flru-cache-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farbazsiddiqui%2Flru-cache-node/lists"}