{"id":22256831,"url":"https://github.com/albinodrought/cachios","last_synced_at":"2025-04-09T17:26:26.596Z","repository":{"id":21457490,"uuid":"92792782","full_name":"AlbinoDrought/cachios","owner":"AlbinoDrought","description":"Simple axios cache wrapper using node-cache","archived":false,"fork":false,"pushed_at":"2023-01-09T02:28:08.000Z","size":1175,"stargazers_count":88,"open_issues_count":6,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-13T23:54:08.229Z","etag":null,"topics":["axios","cache","hacktoberfest","node-cache","nodejs"],"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/AlbinoDrought.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-30T02:51:21.000Z","updated_at":"2023-06-12T03:08:25.000Z","dependencies_parsed_at":"2023-01-11T21:13:26.865Z","dependency_job_id":null,"html_url":"https://github.com/AlbinoDrought/cachios","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbinoDrought%2Fcachios","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbinoDrought%2Fcachios/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbinoDrought%2Fcachios/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlbinoDrought%2Fcachios/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlbinoDrought","download_url":"https://codeload.github.com/AlbinoDrought/cachios/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248076249,"owners_count":21043735,"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":["axios","cache","hacktoberfest","node-cache","nodejs"],"created_at":"2024-12-03T08:08:42.017Z","updated_at":"2025-04-09T17:26:26.568Z","avatar_url":"https://github.com/AlbinoDrought.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cachios\n\n[![Dependency Status](https://david-dm.org/albinodrought/cachios.svg)](https://david-dm.org/albinodrought/cachios)\n[![npm version](https://badge.fury.io/js/cachios.svg)](https://badge.fury.io/js/cachios)\n![Build Status: Node.js CI](https://github.com/AlbinoDrought/cachios/workflows/Node.js%20CI/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/AlbinoDrought/cachios/badge.svg?branch=master)](https://coveralls.io/github/AlbinoDrought/cachios?branch=master)\n\n[![NPM](https://nodei.co/npm/cachios.png)](https://nodei.co/npm/cachios/)\n\nA simple `axios` cache wrapper using `node-cache`.\n\n---\n\nCachios is meant to be a replacement for the following pattern:\n\n```js\nconst axios = require('axios');\n\nconst resources = {};\n\nfunction getResource(id) {\n  if (!resources[id]) {\n    // actually retrieve the resource\n    return axios.get(`/api/thing/${id}`).then((resp) =\u003e {\n      // store the resource\n      resources[id] = resp.data;\n      return resp.data;\n    });\n  } else {\n    // return the resource we already have\n    return Promise.resolve(resources[id]);\n  }\n}\n```\n\nWith Cachios, this is replaced with:\n\n```js\nconst cachios = require('cachios');\n\nfunction getResource(id) {\n  return cachios.get(`/api/thing/${id}`).then((resp) =\u003e {\n    return resp.data;\n  });\n}\n```\n\nThe following `axios` methods are supported:\n\n* request\n* get\n* delete\n* head\n* options\n* post\n* put\n* patch\n\nThe entire response is not cached, and is instead trimmed down (by default) to `status` and `data`. To configure this, see [\"Custom Response Copier\"](#custom-response-copier).\n\n## Installation\n\n`npm install --save cachios`\n\n## Examples\n\nBasic:\n\n```js\nconst cachios = require('cachios');\n\ncachios.get('https://jsonplaceholder.typicode.com/posts/1', {\n  ttl: 300 /* seconds */,\n}).then(console.log);\n\n```\n\nCustom axios client:\n\n```js\n// your normal, non-cached axios instance that is already setup.\nimport axios from './configured-axios';\n\nconst cachios = require('cachios');\nconst cachiosInstance = cachios.create(axios);\n\nconst postData = {/* your postdata here */};\n\ncachiosInstance.post('/posts/1', postData, {\n  ttl: 30, // persist 30 seconds\n}).then((resp) =\u003e {\n  console.log(resp.status);\n\n  const data = resp.data;\n  console.log(data.title);\n  console.log(data.body);\n});\n\n```\n\nMultiple cached GET requests: [Runkit](https://runkit.com/albinodrought/cachios-get-example)\n\nMultiple cached GET requests with different query parameters: [Runkit](https://runkit.com/albinodrought/cachios-get-params-example)\n\n## Configuration\n\n### TTL\n\nTo set the cache TTL, pass it in with your request config:\n\n```js\nconst cachios = require('cachios');\n\ncachios.get('url', {\n  ttl: /* time to live in seconds */,\n});\n\nconst postData = {};\ncachios.post('url', postData, {\n  headers: /* your custom headers */\n  ...\n  ttl: 60, // persist this result for 60 seconds\n});\n```\n\n### Force Ignore Cache\n\nTo ignore existing cache items and force a fresh request to go through, use `force: true`:\n\n```js\nconst cachios = require('cachios');\n\ncachios.get('url'); // cache 'url'\n\n// ignore and update cache for 'url' by using `force: true`\ncachios.get('url', {\n  force: true,\n});\n```\n\n### Custom Axios Instance\n\nCachios also supports using a pre-configured `axios` instance:\n\n```js\nconst cachios = require('cachios');\nconst axios = require('axios');\n\nconst axiosInstance = axios.create({\n  baseURL: 'https://jsonplaceholder.typicode.com',\n});\n\n// all requests will now use this axios instance\nconst cachiosInstance = cachios.create(axiosInstance);\n```\n\n### Custom Cache Configuration\n\nInternally, Cachios uses `node-cache` with sane defaults. To configure it yourself, pass it during `cachios.create`:\n\n```js\nconst cachios = require('cachios');\nconst axios = require('axios');\n\n// configure `node-cache` to keep cache forever!\nconst cachiosInstance = cachios.create(axios, {\n  stdTTL: 0,\n  checkperiod: 0,\n});\n```\n\n### Alternative Cache Implementation\n\nDon't want to use `node-cache`? The `.cache` property can be overridden.\n\n`cachios` expects the cache implementation to work as follows:\n\n```js\ncachios.cache = {\n  /**\n  cacheKey: string\n\n  if a value has been set for this `cacheKey`, return it.\n  otherwise, return a falsey value (undefined, false, null).\n\n  synchronous, asynchronous, and promise-returning functions are supported.\n  */\n  get(cacheKey),\n\n  /**\n  cacheKey: string\n  cacheValue: mixed\n  ttl: number|undefined\n\n  store the value `cacheValue` under `cacheKey` for `ttl` seconds.\n  if `ttl` is not set, it is assumed the value is stored forever.\n\n  synchronous, asynchronous, and promise-returning functions are supported.\n  */\n  set(cacheKey, cacheValue, ttl),\n}\n```\n\nExample using `lru-cache`:\n\n```js\nconst cachios = require('cachios');\nconst LRU  = require('lru-cache');\n\ncachios.cache = LRU(500);\n\ncachios.get('http://example.com/') // not cached\n.then(() =\u003e cachios.get('http://example.com/')); // cached\n.then(() =\u003e {\n  console.log(cachios.cache.itemCount); // 1 item in cache - the first request\n});\n```\n\nExample of persistent cache with `keyv` and `@keyv/sqlite`:\n\n```js\nconst cachios = require('cachios');\nconst Keyv = require('keyv');\n\ncachios.cache = new Keyv('sqlite://cache.sqlite');\n\ncachios.get('http://example.com/') // not cached\n.then(() =\u003e cachios.get('http://example.com/')); // cached\n.then(() =\u003e cachios.cache.opts.store.query('SELECT COUNT(*) as count FROM keyv'))\n.then((cacheSize) =\u003e {\n  console.log(cacheSize[0].count); // 1 item in cache - the first request\n});\n```\n\n### Custom Response Copier\n\nBy default, Cachios uses the following function to trim responses:\n\n```js\nfunction defaultResponseCopier(response) {\n  return {\n    status: response.status,\n    data: response.data,\n  };\n}\n```\n\nThis was originally implemented because of errors during response storage.\n\nTo change what is saved, set the `getResponseCopy` property of your Cachios instance:\n\n```js\nconst cachios = require('cachios');\n\ncachios.getResponseCopy = function (response) {\n  return {\n    status: response.status,\n    statusText: response.statusText,\n    data: response.data,\n  };\n};\n```\n\n### Custom Cache Identifier\n\nBy default, Cachios uses the following function to create a unique cache identifier:\n\n```js\nfunction defaultCacheIdentifer(config) {\n  return {\n    method: config.method,\n    url: config.url,\n    params: config.params,\n    data: config.data,\n  };\n}\n```\n\nTo override this, set the `getCacheIdentifier` property of your Cachios instance:\n\n```js\nconst cachios = require('cachios');\n\ncachios.getCacheIdentifier = function (config) {\n  return {\n    method: config.method,\n    url: config.url,\n    params: config.params,\n    data: config.data,\n    headers: config.headers,\n  };\n};\n```\n\n### Custom object-hash Replacer\n\nBy default, Cachios uses an internal `defaultReplacer` function to add FormData support to [object-hash](https://github.com/puleos/object-hash).\n\nTo override this, set the `getReplaced` property of your Cachios instance:\n\n```js\nconst cachios = require('cachios');\n\ncachios.getReplaced = function (thing) {\n  if (thing === 'foo') {\n    return 'bar';\n  }\n\n  return thing;\n};\n```\n\n## License\n\n[MIT](LICENSE.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falbinodrought%2Fcachios","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falbinodrought%2Fcachios","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falbinodrought%2Fcachios/lists"}