{"id":13671886,"url":"https://github.com/nswbmw/koa-router-cache","last_synced_at":"2025-04-27T18:31:51.996Z","repository":{"id":28990389,"uuid":"32517101","full_name":"nswbmw/koa-router-cache","owner":"nswbmw","description":"Router cache middleware for koa.","archived":false,"fork":false,"pushed_at":"2017-01-03T14:28:21.000Z","size":16,"stargazers_count":22,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-22T00:27:44.630Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/nswbmw.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":"2015-03-19T11:18:51.000Z","updated_at":"2024-09-18T09:11:09.000Z","dependencies_parsed_at":"2022-08-02T17:15:33.432Z","dependency_job_id":null,"html_url":"https://github.com/nswbmw/koa-router-cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nswbmw%2Fkoa-router-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nswbmw%2Fkoa-router-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nswbmw%2Fkoa-router-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nswbmw%2Fkoa-router-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nswbmw","download_url":"https://codeload.github.com/nswbmw/koa-router-cache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251187335,"owners_count":21549620,"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-08-02T09:01:20.999Z","updated_at":"2025-04-27T18:31:51.054Z","avatar_url":"https://github.com/nswbmw.png","language":"JavaScript","funding_links":[],"categories":["仓库","JavaScript"],"sub_categories":["中间件"],"readme":"## koa-router-cache\n\nRouter cache middleware for koa@1.\n\n### Install\n\n```\nnpm i koa-router-cache --save\n```\n\n### Usage\n\n```\ncache(app, options)\n```\n\nOptions:\n\n- key: `{String|GeneratorFunction}` cache key. (Required)\n- expire: `{Number}` expire time, in `ms`. (Required)\n- get: `{GeneratorFunction}` custom getter function for getting data from cache. (Required)\n- set: `{GeneratorFunction}` custom setter function for setting data to cache. (Required)\n- passthrough: `{GeneratorFunction}` whether pass request through, return an object. (Required)\n  - shouldCache: {Boolean} whether cache this result\n  - shouldPass: {Boolean} whether pass this request through\n- evtName: `{String}` event name for destroy cache. (Optional)\n- destroy: `{function}` destroy cache. (Optional)\n- pathToRegexp `{Object}` pathToRegexp options, see `https://github.com/pillarjs/path-to-regexp#usage`. (Optional)\n\n### Example\n\n`koa-router-cache` build-in simple `MemoryCache` and `RedisCache`, also you can write by yourself.\n\n**MemoryCache**\n\n```\n'use strict';\n \nvar app = require('koa')();\nvar cache = require('koa-router-cache');\nvar MemoryCache = cache.MemoryCache;\n \nvar count = 0;\n \napp.use(cache(app, {\n  'GET /': {\n    key: 'cache:index',\n    expire: 2 * 1000,\n    get: MemoryCache.get,\n    set: MemoryCache.set,\n    passthrough: MemoryCache.passthrough,\n    evtName: 'clearIndexCache',\n    destroy: MemoryCache.destroy\n  }\n}));\n \napp.use(function* () {\n  if (this.path === '/') {\n    this.body = count++;\n    if (count === 3) {\n      count = 0;\n      this.app.emit('clearIndexCache');\n    }  \n  }\n});\n \napp.listen(3000, function () {\n  console.log('listening on 3000.');\n});\n```\n\n**RedisCache**\n\n```\n'use strict';\n\nvar app = require('koa')();\nvar cache = require('koa-router-cache');\nvar RedisCache = cache.RedisCache();// or RedisCache(client)\n\nvar count = 0;\n\napp.use(cache(app, {\n  'GET /': {\n    key: 'cache:index',\n    expire: 2 * 1000,\n    get: RedisCache.get,\n    set: RedisCache.set,\n    passthrough: RedisCache.passthrough,\n    evtName: 'clearIndexCache',\n    destroy: RedisCache.destroy\n  }\n}));\n\napp.use(function* () {\n  if (this.path === '/') {\n    this.body = {\n      count: count++\n    };\n    if (count === 3) {\n      count = 0;\n      this.app.emit('clearIndexCache');\n    }\n  }\n});\n\napp.listen(3000, function () {\n  console.log('listening on 3000.');\n});\n```\n\nanother usage:\n\n```\n// Guests share one index page cache\nGET /': {\n  key: 'cache:index',\n  expire: 10 * 1000,\n  get: MemoryCache.get,\n  set: MemoryCache.set,\n  destroy: MemoryCache.destroy,\n  passthrough: function* passthrough(_cache) {\n    // Guests\n    if (!this.session || !this.session.user) {\n      if (_cache == null) {\n        return {\n          shouldCache: true,\n          shouldPass: true\n        };\n      }\n      this.type = 'text/html; charset=utf-8';\n      this.set('content-encoding', 'gzip');\n      this.body = _cache;\n      return {\n        shouldCache: true,\n        shouldPass: false\n      };\n    }\n    // Logged-in users\n    return {\n      shouldCache: false,\n      shouldPass: true\n    };\n  }\n}\n```\n\n### Test\n\n```\nnpm test\n```\n\n### License\n\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnswbmw%2Fkoa-router-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnswbmw%2Fkoa-router-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnswbmw%2Fkoa-router-cache/lists"}