{"id":24367173,"url":"https://github.com/vajahath/feather-cache","last_synced_at":"2025-04-10T13:53:34.062Z","repository":{"id":57233956,"uuid":"145798840","full_name":"vajahath/feather-cache","owner":"vajahath","description":"A Simple Lightweight Pluggable Asynchronous Key-Value Cache For Multiple Storage Options","archived":false,"fork":false,"pushed_at":"2020-01-22T07:45:16.000Z","size":272,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-24T12:39:23.004Z","etag":null,"topics":[],"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/vajahath.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}},"created_at":"2018-08-23T04:11:11.000Z","updated_at":"2023-03-10T07:54:17.000Z","dependencies_parsed_at":"2022-09-04T21:40:37.165Z","dependency_job_id":null,"html_url":"https://github.com/vajahath/feather-cache","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Ffeather-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Ffeather-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Ffeather-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vajahath%2Ffeather-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vajahath","download_url":"https://codeload.github.com/vajahath/feather-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248229115,"owners_count":21068829,"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":"2025-01-19T01:42:07.088Z","updated_at":"2025-04-10T13:53:34.045Z","avatar_url":"https://github.com/vajahath.png","language":"TypeScript","readme":"# feather-cache\n\nA Simple Pluggable Key-Value Cache For Multiple Storage Options.\n\n[![npm](https://img.shields.io/npm/v/feather-cache.svg)](https://www.npmjs.com/package/feather-cache)\n[![Travis](https://img.shields.io/travis/vajahath/feather-cache.svg)](https://travis-ci.org/vajahath/feather-cache)\n[![styled with prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n[![TypeScript Ready](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/)\n\n\u003c!-- [![npm](https://img.shields.io/npm/dt/feather-cache.svg)]()\n[![Built with generator-ts-np](https://img.shields.io/badge/scaffolding-ts_np-2699ad.svg)](https://github.com/vajahath/generator-ts-np) --\u003e\n\n![](media/feather.png)\n\n## Install\n\n```bash\nnpm i --save feather-cache\n```\n\nYou don't have to install type definitions for typescript. It's built in.\n\n## What\n\nThis is a simple caching frame which can be used with available drivers or you can easily write one your own. If no drivers are provided (default) works like an in-memory cache. But that is not what this module intended for. The in-memory cache is provided for the sake of convenience. If that is what all you want, [lru-cache](https://www.npmjs.com/package/lru-cache) is recommended.\n\nActual use case for this module is to use with a driver (mongodb-driver or [indexed-db-driver](https://www.npmjs.com/package/feather-cache-indexeddb) etc.) which enables persistent caching.\n\n### Example\n\n#### in memory cache\n\n```ts\nconst { FeatherCache } = require('feather-cache');\n// or\nimport { FeatherCache } from 'feather-cache';\n\n// init\nconst fStore = new FeatherCache({\n  maxAgeInMs: 60 * 1000, // 1 min expiry\n});\n\n// ...async fn...\nawait fStore.set('hiya', 'mm!');\n\nawait fStore.get('hiya'); //-\u003e mm!\nawait fStore.fetch('hiya'); //-\u003e { key: 'hiya', val: 'mm!' }\n\n// ... after expiry time\nawait fStore.get('hiya'); //-\u003e null\n```\n\n#### with a driver\n\n```ts\n// ...\nconst fStore = new FeatherCache(driver);\n// ...\n```\n\n## APIs\n\n| function                          | description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |\n| --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| `FeatherCache.set(key, val, opt)` | returns `Promise`.\u003cbr\u003e**`key`**: string\u003cbr\u003e**`val`**: any data to be stored \u003cbr\u003e**`opt`**: (optional) `{ maxAgeInMs: 1000 }`, sets expiry time.                                                                                                                                                                                                                                                                                                                                                                                                                                            |\n| `FeatherCache.get(key)`           | returns `Promise` which resolves to the stored data. else, `null`. \u003cbr\u003e If data has expired returns `null` and then deletes the entry behind the scenes.                                                                                                                                                                                                                                                                                                                                                                                                                                   |\n| `FeatherCache.fetch(key)`         | returns `Promise` which resolves to stored data. The difference between `get` and `fetch` is that, `fetch` returns the stored data one more time after its expiry and then deletes it.\u003cbr\u003e**More info:**\u003cbr\u003e`get`: `finds data -\u003e if expired deletes it -\u003e returns null`.\u003cbr\u003e`fetch`: `finds data -\u003e if expired returns it -\u003e and then deletes it`.\u003cbr\u003e However, you can control this behavior by passing the `delete` option (default `delete: true`). Eg: `fStore.fetch('hiya', { delete: false })`. This sets the expiry flag but don't delete it. Sometimes you may find this helpful. |\n| `FeatherCache.del(key)`           | returns `Promise` deletes the entry                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |\n\n## Configuration / writing drivers\n\nA driver is nothing but a configuration object which exposes a persistent storage interface.\n\n```ts\nconst dbDriver = {\n  maxAgeInMs: 60 * 1000 * 5, // default expiry 5 mins\n  setFn: async function(key, val) {\n    // db interface to store data\n    await Db.create({ key, val });\n  },\n  getFn: async function(key) {\n    // db interface to get data\n    const data = await Db.find({ where: { key } });\n    return data;\n  },\n  delFn: async function(key) {\n    // db interface to delete data\n    await Db.remove({ where: { key } });\n  },\n};\n\n// pass in the driver\nconst featherStore = new FeatherCache(dbDriver);\n// ...\n```\n\nAn example: [feather-cache-indexeddb](https://www.npmjs.com/package/feather-cache-indexeddb).\n\nThose who publish drivers advised to follow the naming convention:\n\n```txt\nfeather-cache-\u003cstorage_option_name\u003e\n```\n\nAlso, attach the key word `feather-cache`.\n\n---\n\n[![used version of ts-np generator](https://img.shields.io/badge/ts--np-v2.0.1-a5a5a5.svg?style=flat-square)](https://github.com/vajahath/generator-ts-np)\n\n## Licence\n\nMIT \u0026copy; [Vajahath Ahmed](https://twitter.com/vajahath7)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvajahath%2Ffeather-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvajahath%2Ffeather-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvajahath%2Ffeather-cache/lists"}