{"id":19213531,"url":"https://github.com/sammaji/hono-server-cache","last_synced_at":"2026-02-06T19:31:08.180Z","repository":{"id":261914781,"uuid":"885710682","full_name":"sammaji/hono-server-cache","owner":"sammaji","description":"A flexible server-side caching middleware for Hono applications.","archived":false,"fork":false,"pushed_at":"2024-11-09T09:21:20.000Z","size":41,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-04T01:05:08.934Z","etag":null,"topics":["caching","hono","middleware","server"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/hono-server-cache","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/sammaji.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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,"zenodo":null}},"created_at":"2024-11-09T07:20:55.000Z","updated_at":"2025-06-03T02:55:05.000Z","dependencies_parsed_at":"2025-04-15T09:14:24.186Z","dependency_job_id":"82d45d76-17cc-45e9-aa2e-01bb12f18e31","html_url":"https://github.com/sammaji/hono-server-cache","commit_stats":null,"previous_names":["sammaji/hono-server-cache"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sammaji/hono-server-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammaji%2Fhono-server-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammaji%2Fhono-server-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammaji%2Fhono-server-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammaji%2Fhono-server-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sammaji","download_url":"https://codeload.github.com/sammaji/hono-server-cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sammaji%2Fhono-server-cache/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29174068,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-06T19:28:14.811Z","status":"ssl_error","status_checked_at":"2026-02-06T19:28:13.420Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["caching","hono","middleware","server"],"created_at":"2024-11-09T14:06:08.771Z","updated_at":"2026-02-06T19:31:08.164Z","avatar_url":"https://github.com/sammaji.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hono-server-cache\n\n`hono-server-cache-middleware` makes it easier to implement server side caching and invalidation in your [hono](https://hono.dev/) application. It is different from the [cache middleware](https://hono.dev/docs/middleware/builtin/cache) provided by hono.\n\n## Features\n\n- 🚀 Simple integration with Hono applications\n- 🎯 Flexible cache key generation\n- 🔄 Automatic cache invalidation on non-GET requests\n- ⚙️ Customizable cache storage backends\n- 🎨 Route-based cache matching\n- 📊 Cache hit/miss headers\n\n## Installation\n\n```bash\nnpm install hono-server-cache\n```\n\n## Quick Start\n\n```typescript\nimport { Hono } from 'hono'\nimport { serverCache } from 'hono-server-cache'\n\nconst app = new Hono()\n\n// Example using an in-memory cache\nconst cache = new Map()\n\napp.use(serverCache({\n  // Cache all routes under /api\n  matcher: ['/api/*'],\n  \n  // Cache operations\n  readCacheFn: (key) =\u003e cache.get(key),\n  writeCacheFn: (key, value) =\u003e cache.set(key, value),\n  invalidateCacheFn: (key) =\u003e cache.delete(key)\n}))\n```\n\n## API Reference\n\n### `serverCache(options: HonoCachingOptions)`\n\nCreates a middleware function for handling server-side caching.\n\n#### Options\n\n```typescript\ntype HonoCachingOptions\u003cT\u003e = {\n  keyFn?: (ctx: Hono.Context) =\u003e string\n  readCacheFn: (key: string) =\u003e T | undefined | null | Promise\u003cT | undefined | null\u003e\n  writeCacheFn: (key: string, value: T) =\u003e void | Promise\u003cvoid\u003e\n  invalidateCacheFn: (key: string) =\u003e void | Promise\u003cvoid\u003e\n  matcher: string[] | ((ctx: Hono.Context) =\u003e MatcherFnResponse)\n}\n```\n\n- `keyFn`: Function to generate cache keys. By default, it uses request path to generate key\n- `readCacheFn`: Function to read from cache\n- `writeCacheFn`: Function to write to cache\n- `invalidateCacheFn`: Function to invalidate cache entries\n- `matcher`: Array of route patterns or function to determine cache behavior\n\n### Matcher Response\n\n```typescript\ntype MatcherFnResponse = {\n  read: boolean    // Whether to attempt reading from cache\n  write: boolean   // Whether to cache the response\n  invalidate: boolean  // Whether to invalidate the cache\n}\n```\n\n## Examples\n\n### Custom Cache Key Generation\n\n```typescript\napp.use(serverCache({\n  matcher: ['/api/*'],\n  keyFn: (ctx) =\u003e `_key:${ctx.req.path}:${ctx.req.query('version')}`,\n  // ... cache functions\n}))\n```\n\n### Custom Matcher Function\n\n```typescript\napp.use(serverCache({\n  matcher: (ctx) =\u003e ({\n    read: ctx.req.method === 'GET',\n    write: ctx.req.method === 'GET',\n    invalidate: ctx.req.method === 'POST'\n  }),\n  // ... cache functions\n}))\n```\n\n### Redis Backend Example\n\n```typescript\nimport { Redis } from 'ioredis'\n\nconst redis = new Redis()\n\napp.use(serverCache({\n  matcher: ['/api/*'],\n  readCacheFn: async (key) =\u003e {\n    const data = await redis.get(key)\n    return data ? JSON.parse(data) : null\n  },\n  writeCacheFn: async (key, value) =\u003e {\n    await redis.set(key, JSON.stringify(value))\n  },\n  invalidateCacheFn: async (key) =\u003e {\n    await redis.del(key)\n  }\n}))\n```\n\n## Cache Headers\n\nThe middleware automatically adds an `X-Cache` header to responses:\n- `X-Cache: HIT` - Response was served from cache\n- `X-Cache: MISS` - Response was generated fresh\n\n## Behavior\n\n1. For GET requests matching the cache patterns:\n   - Attempts to read from cache first\n   - If cache miss, executes the handler and caches the response\n2. For non-GET requests:\n   - Invalidates the cache for the matching key\n   - Processes the request normally\n\n## License\n\nMIT\n\n## Contributing\nContributions are welcome! Please feel free to submit a Pull Request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsammaji%2Fhono-server-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsammaji%2Fhono-server-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsammaji%2Fhono-server-cache/lists"}