{"id":18812854,"url":"https://github.com/facundoolano/redis-lru","last_synced_at":"2025-04-05T18:10:15.989Z","repository":{"id":52029254,"uuid":"75512147","full_name":"facundoolano/redis-lru","owner":"facundoolano","description":"Redis-backed LRU cache for Node.js","archived":false,"fork":false,"pushed_at":"2022-11-01T23:13:43.000Z","size":115,"stargazers_count":96,"open_issues_count":9,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-29T16:08:00.149Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/facundoolano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-04T02:06:55.000Z","updated_at":"2024-06-26T19:28:24.000Z","dependencies_parsed_at":"2023-01-21T04:02:33.502Z","dependency_job_id":null,"html_url":"https://github.com/facundoolano/redis-lru","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facundoolano%2Fredis-lru","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facundoolano%2Fredis-lru/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facundoolano%2Fredis-lru/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/facundoolano%2Fredis-lru/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/facundoolano","download_url":"https://codeload.github.com/facundoolano/redis-lru/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247378149,"owners_count":20929297,"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":[],"created_at":"2024-11-07T23:35:22.627Z","updated_at":"2025-04-05T18:10:15.960Z","avatar_url":"https://github.com/facundoolano.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# redis lru cache [![Build Status](https://secure.travis-ci.org/facundoolano/redis-lru.png)](http://travis-ci.org/facundoolano/redis-lru)\n\nA least recently used (LRU) cache backed by Redis, allowing data to be shared by\nmultiple Node.JS processes. API inspired by [node-lru-cache](https://github.com/isaacs/node-lru-cache).\n\n```js\nvar redis = require('redis').createClient(port, host, opts);\nvar lru = require('redis-lru');\n\nvar personCache = lru(redis, 5); // up to 5 items\n\npersonCache.set('john', {name: 'John Doe', age: 27})\n  .then(() =\u003e personCache.set('jane', {name: 'Jane Doe', age: 30}))\n  .then(() =\u003e personCache.get('john'))\n  .then(console.log) // prints {name: 'John Doe', age: 27}\n  .then(() =\u003e personCache.reset()) //clear the cache\n\nvar bandCache = lru(redis, {max: 2, namespace: 'bands', maxAge: 15000}); // use a different namespace and set expiration\n\nbandCache.set('beatles', 'john, paul, george, ringo')\n  .then(() =\u003e bandCache.set('zeppelin', 'jimmy, robert, john, bonzo'))\n  .then(() =\u003e bandCache.get('beatles')) // now beatles are the most recently accessed\n  .then(console.log) // 'john, paul, george, ringo'\n  .then(() =\u003e bandCache.set('floyd', 'david, roger, syd, richard, nick')) // cache full, remove least recently accessed\n  .then(() =\u003e bandCache.get('zeppelin'))\n  .then(console.log) // null, was evicted from cache\n```\n\nWorks both with [node_redis](https://github.com/NodeRedis/node_redis) and [ioredis](https://github.com/luin/ioredis) clients.\n\n## Installation\n\n```\nnpm install redis-lru\n```\n\n## Options\n\n* `max`: Maximum amount of items the cache can hold. This option is required; if no\nother option is needed, it can be passed directly as the second parameter when creating\nthe cache.\n* `namespace`: Prefix appended to all keys saved in Redis, to avoid clashes with other applications\nand to allow multiple instances of the cache.\n* `maxAge`: Maximum amount of milliseconds the key will be kept in the cache; after that getting/peeking will\nresolve to `null`. Note that the value will be removed from Redis after `maxAge`, but the key will\nbe kept in the cache until next time it's accessed (i.e. it will be included in `count`, `keys`, etc., although not in `has`.).\n* `score`: function to customize the score used to order the elements in the cache. Defaults to `() =\u003e new Date().getTime()`\n* `increment`: if `true`, on each access the result of the `score` function is added to the previous one,\nrather than replacing it.\n\n## API\n\nAll methods return a Promise.\n\n* `set(key, value, maxAge)`: set value for the given key, marking it as the most recently accessed one.\nKeys should be strings, values will be JSON.stringified. The optional `maxAge` overrides for this specific key\nthe global expiration of the cache.\n* `get(key)`: resolve to the value stored in the cache for the given key or `null` if not present.\nIf present, the key will be marked as the most recently accessed one.\n* `getOrSet(key, fn, maxAge)`: resolve to the value stored in the cache for the given key. If not present,\nexecute `fn`, save the result in the cache and return it. `fn` should be a no args function that\nreturns a value or a promise. If `maxAge` is passed, it will be used only if the key is not already in the cache.\n* `peek(key)`: resolve to the value stored in the cache for the given key, without changing its\nlast accessed time.\n* `del(key)`: removes the item from the cache.\n* `reset()`: empties the cache.\n* `has(key)`: resolves to true if the given key is present in the cache.\n* `keys()`: resolves to an array of keys in the cache, sorted from most to least recently accessed.\n* `values()`: resolves to an array of values in the cache, sorted from most to least recently accessed.\n* `count()`: resolves to the number of items currently in the cache.\n\n### Using as a LFU cache\n\nBy using a custom `score` function and the `increment` option, one can turn the cache\ninto a least frequently used (LFU), where the items that have been accessed more times\n(rather than most recently) are preserved:\n\n```js\nvar redis = require('redis').createClient(port, host, opts);\nvar lru = require('redis-lru');\n\nvar bandLfu = lru(redis, {max: 2, score: () =\u003e 1, increment: true});\n\nbandLfu.set('beatles', 'john, paul, george, ringo')\n  .then(() =\u003e bandLfu.get('beatles')) // accessed twice\n  .then(() =\u003e bandLfu.set('zeppelin', 'jimmy, robert, john, bonzo'))\n  .then(() =\u003e bandLfu.set('floyd', 'david, roger, syd, richard, nick')) // cache full, remove least frequently accessed\n  .then(() =\u003e bandLfu.get('zeppelin'))\n  .then(console.log) // null, was evicted from cache\n```\n\n## Implementation\n\nEach item in the cache is stored as a regular key/value in Redis. Additionally,\na [ZSET](https://redis.io/topics/data-types#sorted-sets) is used to keep an\nindex of the keys sorted by last-accessed timestamp.\n\nRequires Redis 3.0.2 or greater, since it uses the\n[XX option of ZADD](https://redis.io/commands/zadd#zadd-options-redis-302-or-greater).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffacundoolano%2Fredis-lru","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffacundoolano%2Fredis-lru","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffacundoolano%2Fredis-lru/lists"}