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

Feature request: customization of cache key #444

Open
p0358 opened this issue Jan 25, 2024 · 8 comments · May be fixed by #514
Open

Feature request: customization of cache key #444

p0358 opened this issue Jan 25, 2024 · 8 comments · May be fixed by #514

Comments

@p0358
Copy link
Contributor

p0358 commented Jan 25, 2024

Currently it's pretty much just hardcoded to method+scheme+host+key+query+body+headerValues, with the ability to drop some of these fields, but not to add new ones. Only the option to specify request headers gives us some flexibility with which I was allowed to do that:

    map {header.Cookie} {site_language} {
        ~.*language_id=2.* "en"
        default "pl"
    }
    request_header X-Site-Language {site_language}
    header X-Site-Language {site_language}

also gotta do this in main config for that:

{
    order cache before rewrite
    order request_header before cache
}

Now that is pretty hacky and cumbersome. Wouldn't it be nice if we could specify a custom pattern that could be added or even replace the key with something custom?

Such as:

# pseudocode
cache {
    key {
        custom KEY-{http.request.uri.query}-{site_language}
    }
}
@p0358
Copy link
Contributor Author

p0358 commented Jan 25, 2024

On an unrelated note, I seem to witness some worrisome behavior with cache output being randomly broken

curl -v http://localhost:4443/esi
*   Trying 127.0.0.1:4443...
* Connected to localhost (127.0.0.1) port 4443 (#0)
> GET /esi HTTP/1.1
> Host: localhost:4443
> User-Agent: curl/8.0.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Age: 27
< Cache-Control: public
< Cache-Status: Souin; hit; ttl=973; key=GET-http-localhost:4443-/esi
< Content-Length: 97
< Content-Type: text/html
< Date: Thu, 25 Jan 2024 00:44:39 GMT
< Server: Caddy
<
<h1>ESI INCLUDE</h1>* transfer closed with 77 bytes remaining to read
* Closing connection 0
curl: (18) transfer closed with 77 bytes remaining to read

This is with the example Caddyfile from this repo. Had similar behavior with browser on another OS (this one is ran on Windows), but managed to solve it by disabling gzip seemingly (or by explicitly providing Accept-Encoding as one of headers to be part of the cache key. But the weird thing is:

  1. curl doesn't use any content encoding by default
  2. browsers do seem to always send the same "Accept-Encoding" value when requesting pages
    ...so something doesn't add up here either

@p0358
Copy link
Contributor Author

p0358 commented Jan 25, 2024

Look at the mismatch of Content-Length header when it's cache miss or hit

curl -v http://localhost:4443/esi
*   Trying 127.0.0.1:4443...
* Connected to localhost (127.0.0.1) port 4443 (#0)
> GET /esi HTTP/1.1
> Host: localhost:4443
> User-Agent: curl/8.0.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Cache-Control: public
< Cache-Status: Souin; fwd=uri-miss; stored; key=GET-http-localhost:4443-/esi
< Content-Type: text/plain; charset=utf-8
< Server: Caddy
< Date: Thu, 25 Jan 2024 00:49:16 GMT
< Content-Length: 20
<
<h1>ESI INCLUDE</h1>* Connection #0 to host localhost left intact
curl -v http://localhost:4443/esi
*   Trying 127.0.0.1:4443...
* Connected to localhost (127.0.0.1) port 4443 (#0)
> GET /esi HTTP/1.1
> Host: localhost:4443
> User-Agent: curl/8.0.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Age: 3
< Cache-Control: public
< Cache-Status: Souin; hit; ttl=997; key=GET-http-localhost:4443-/esi
< Content-Length: 97
< Content-Type: text/plain; charset=utf-8
< Date: Thu, 25 Jan 2024 00:49:16 GMT
< Server: Caddy
<
<h1>ESI INCLUDE</h1>* transfer closed with 77 bytes remaining to read
* Closing connection 0
curl: (18) transfer closed with 77 bytes remaining to read

The above is on Windows with caddy downloaded freshly from their site with Souin selected as the only addon.

@darkweak
Copy link
Owner

darkweak commented Feb 3, 2024

Hello @p0358 does your PR #445 fix that?

@p0358
Copy link
Contributor Author

p0358 commented Feb 3, 2024

@darkweak I think that'd be it yeah, at very first I didn't realize it was related to ESI

@darkweak
Copy link
Owner

darkweak commented Feb 7, 2024

I have to check why it returns a bad Content-Length in the Caddy test suite

@darkweak
Copy link
Owner

darkweak commented Feb 8, 2024

@p0358 your PR is ready, thank you for your contribution! 🚀

@teodorescuserban
Copy link

This should probably be closed, I made a new feature request about the custom key #513

@darkweak
Copy link
Owner

In the linked PR above, you'll be able to set your own template.

cache {
    key {
        template KEY-{http.request.uri.query}-{host}
    }
}

@darkweak darkweak linked a pull request May 18, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants