feat(cache): improved customizability (#2652)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* feat(cache): cacheName can accept functions too * feat(cache): custom key generator option added * chore(cache): updated deno files * fix(cache): formatted and linted * test(cache): added test case for customizability
authored by Aditya Mathur · committed by GitHub · parent f3f6f02 · Browse files · 4 files·+91 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+91 added−6 removed
- tests:+67 added−0 removed
- other:+24 added−6 removed
Provenance
- Commit
e1989ff- Tree
69c1cf4- Author
Aditya Mathur <57684218+MathurAditya724@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
f3f6f02
4 files changed+91 added−6 removed
modified:deno_dist/middleware/cache/index.ts+10 added-3 removed
12345567891011121354 unmodified lines68697071717272737475767778798081828314 unmodified linesimport type { Context } from '../../context.ts'import type { MiddlewareHandler } from '../../types.ts'
export const cache = (options: { cacheName: string cacheName: string | ((c: Context) => Promise<string> | string) wait?: boolean cacheControl?: string vary?: string | string[] keyGenerator?: (c: Context) => Promise<string> | string}): MiddlewareHandler => { if (!globalThis.caches) { console.log('Cache Middleware is not enabled because caches is not defined.') return async (_c, next) => await next()54 unmodified lines } }
return async function cache(c, next) { const key = c.req.url const cache = await caches.open(options.cacheName) let key = c.req.url if (options.keyGenerator) { key = await options.keyGenerator(c) }
const cacheName = typeof options.cacheName === 'function' ? await options.cacheName(c) : options.cacheName const cache = await caches.open(cacheName) const response = await cache.match(key) if (response) { return new Response(response.body, response) }14 unmodified linesmodified:src/middleware/cache/index.test.ts+67 added
9 unmodified lines10 await promise11 }12 }13 14+describe('Customizing Caching Keys', () => {15+ const app = new Hono()16+17+ const dynamicCacheName = 'dynamic-cache-name'18+ app.use(19+ '/dynamic-cache-name/*',20+ cache({21+ cacheName: async () => dynamicCacheName,22+ wait: true,23+ cacheControl: 'max-age=10',24+ })25+ )26+ app.get('/dynamic-cache-name/', (c) => {27+ return c.text('cached')28+ })29+30+ const dynamicCacheKey = 'dynamic-cache-key'31+ app.use(32+ '/dynamic-cache-key/*',33+ cache({34+ cacheName: 'my-app-v1',35+ wait: true,36+ cacheControl: 'max-age=10',37+ keyGenerator: async () => dynamicCacheKey,38+ })39+ )40+ app.get('/dynamic-cache-key/', (c) => {41+ return c.text('cached')42+ })43+44+ app.use(45+ '/dynamic-cache/*',46+ cache({47+ cacheName: async () => dynamicCacheName,48+ cacheControl: 'max-age=10',49+ keyGenerator: async () => dynamicCacheKey,50+ })51+ )52+ app.get('/dynamic-cache/', (c) => {53+ return c.text('cached')54+ })55+56+ const ctx = new Context()57+58+ it('Should use dynamically generated cache name', async () => {59+ await app.request('http://localhost/dynamic-cache-name/', undefined, ctx)60+ const cache = await caches.open(dynamicCacheName)61+ const keys = Array.from(await cache.keys())62+ expect(keys.length).toBe(1)63+ })64+65+ it('Should use dynamically generated cache key', async () => {66+ await app.request('http://localhost/dynamic-cache-key/')67+ const cache = await caches.open('my-app-v1')68+ const response = await cache.match(dynamicCacheKey)69+ expect(response).not.toBeNull()70+ })71+72+ it('Should retrieve cached response with dynamic cache name and key', async () => {73+ await app.request('http://localhost/dynamic-cache/', undefined, undefined, ctx)74+ const res = await app.request('http://localhost/dynamic-cache/', undefined, undefined, ctx)75+ expect(res).not.toBeNull()76+ expect(res.status).toBe(200)77+ expect(res.headers.get('cache-control')).toBe('max-age=10')78+ })79+})80+81 describe('Cache Middleware', () => {82 const app = new Hono()83 84 let count = 1202 unmodified linesmodified:src/middleware/cache/index.ts+10 added-3 removed
12345567891011121354 unmodified lines68697071717272737475767778798081828314 unmodified linesimport type { Context } from '../../context'import type { MiddlewareHandler } from '../../types'
export const cache = (options: { cacheName: string cacheName: string | ((c: Context) => Promise<string> | string) wait?: boolean cacheControl?: string vary?: string | string[] keyGenerator?: (c: Context) => Promise<string> | string}): MiddlewareHandler => { if (!globalThis.caches) { console.log('Cache Middleware is not enabled because caches is not defined.') return async (_c, next) => await next()54 unmodified lines } }
return async function cache(c, next) { const key = c.req.url const cache = await caches.open(options.cacheName) let key = c.req.url if (options.keyGenerator) { key = await options.keyGenerator(c) }
const cacheName = typeof options.cacheName === 'function' ? await options.cacheName(c) : options.cacheName const cache = await caches.open(cacheName) const response = await cache.match(key) if (response) { return new Response(response.body, response) }14 unmodified linesmodified:src/test-utils/setup-vitest.ts+4 added
26 unmodified lines27 async match(key: Request | string): Promise<Response | null> {28 return this.store.get(key) || null29 }30 31+ async keys() {32+ return this.store.keys()33+ }34+35 async put(key: Request | string, response: Response): Promise<void> {36 this.store.set(key, response)37 }38 }10 unmodified lines