Skip to content
Myr

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

4 files+91 added−6 removed

modified:deno_dist/middleware/cache/index.ts+10 added-3 removed
modified:src/middleware/cache/index.test.ts+67 added
9 unmodified lines
10 await promise11 }12}1314describe('Customizing Caching Keys', () => {15 const app = new Hono()1617 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 })2930 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 })4344 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 })5556 const ctx = new Context()5758 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 })6465 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 })7172 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})8081describe('Cache Middleware', () => {82 const app = new Hono()8384 let count = 1
202 unmodified lines
modified:src/middleware/cache/index.ts+10 added-3 removed
modified:src/test-utils/setup-vitest.ts+4 added
26 unmodified lines
27 async match(key: Request | string): Promise<Response | null> {28 return this.store.get(key) || null29 }3031 async keys() {32 return this.store.keys()33 }3435 async put(key: Request | string, response: Response): Promise<void> {36 this.store.set(key, response)37 }38}
10 unmodified lines