Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cache): add duration option #3367

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions runtime_tests/deno/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cache } from '../../src/middleware/cache/index.ts'
import { expect } from '@std/expect'
import { FakeTime } from '@std/testing/time'

Deno.test('Get cached text', async () => {
Deno.test('Should return cached response', async () => {
const c = await caches.open('my-app')
await c.delete('http://localhost')

Expand All @@ -26,13 +26,14 @@ Deno.test('Get cached text', async () => {
await app.request('http://localhost')
const res = await app.request('http://localhost')
expect(await res.text()).toBe('Hello Hono')
expect(res.headers.get('Hono-Cached-Time')).toBeNull()
expect(res).not.toBeNull()
expect(res.status).toBe(200)
})

Deno.test(
{
name: 'Do not get cached text over duration',
name: 'Should not return cached response over duration',
sanitizeResources: false,
},
async () => {
Expand All @@ -58,9 +59,10 @@ Deno.test(
})

await app.request('http://localhost')
await time.tickAsync(600_100) // 10min
await time.tickAsync(60000)
const res = await app.request('http://localhost')
expect(await res.text()).toBe('Not Found')
expect(res.headers.get('Hono-Cached-Time')).toBeNull()
expect(res).not.toBeNull()
expect(res.status).toBe(200)
}
Expand Down
11 changes: 4 additions & 7 deletions src/middleware/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
}
}

const durationHeader = 'Hono-Cache-Duration'
const cachedTime = 'Hono-Cached-Time'

return async function cache(c, next) {
let key = c.req.url
Expand All @@ -116,12 +116,10 @@
const response = await cache.match(key)
if (response) {
if (options.duration) {
const duration = Number(response.headers.get(durationHeader))
const duration = Number(response.headers.get(cachedTime)) + options.duration * 1000
if (duration && duration > Date.now()) {
const newResponse = new Response(response.body, response)
newResponse.headers.delete(durationHeader)
return newResponse
return new Response(response.body, response).headers.delete(cachedTime)
ryuapp marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 122 in src/middleware/cache/index.ts

View check run for this annotation

Codecov / codecov/patch

src/middleware/cache/index.ts#L119-L122

Added lines #L119 - L122 were not covered by tests
} else {
return new Response(response.body, response)
}
Expand All @@ -134,8 +132,7 @@
addHeader(c)
const res = c.res.clone()
if (options.duration) {
const duration = String(Date.now() + options.duration * 1000)
res.headers.set(durationHeader, duration)
res.headers.set(cachedTime, String(Date.now()))

Check warning on line 135 in src/middleware/cache/index.ts

View check run for this annotation

Codecov / codecov/patch

src/middleware/cache/index.ts#L135

Added line #L135 was not covered by tests
}
if (options.wait) {
await cache.put(key, res)
Expand Down
Loading