darkweak / souin

An HTTP cache system, RFC compliant, compatible with @tyktechnologies, @traefik, @caddyserver, @go-chi, @bnkamalesh, @beego, @devfeel, @labstack, @gofiber, @go-goyave, @go-kratos, @gin-gonic, @roadrunner-server, @zalando, @zeromicro, @nginx and @apache
https://docs.souin.io
MIT License
705 stars 56 forks source link

Purging by regex not working on master #559

Open mac-chaffee opened 1 week ago

mac-chaffee commented 1 week ago

Hello, thanks for creating this amazing repo!

I noticed that even after #556 was merged into master, I still can't seem to purge keys by regex. I'm using Caddy and Otter on commit 2968d51eef04b31925352ac4aaf815ea49f4efbd.

Here is how to reproduce the issue: Dockerfile:

FROM --platform=$BUILDPLATFORM caddy:builder-alpine AS builder

ENV CGO_ENABLED=0

ARG TARGETOS
ARG TARGETARCH

ENV GOOS=${TARGETOS} GOARCH=${TARGETARCH}

RUN xcaddy build \
    --with github.com/caddy-dns/cloudflare \
    --with github.com/darkweak/souin/plugins/caddy@2968d51eef04b31925352ac4aaf815ea49f4efbd \
    --with github.com/darkweak/storages/otter/caddy

FROM caddy:alpine

COPY --from=builder /usr/bin/caddy /usr/bin/caddy

Caddyfile:

{
  cache {
    ttl 120s
    otter
    api {
      souin
    }
  }
}

localhost {
  cache
  respond "Hello World!"
}

Test purging by regex:

# Store something in the cache
$ curl -ik https://localhost/                                                   
HTTP/2 200 
alt-svc: h3=":443"; ma=2592000
cache-control: 
cache-status: Souin; fwd=uri-miss; stored; key=GET-https-localhost-/
content-type: text/plain; charset=utf-8
server: Caddy
content-length: 12
date: Sun, 20 Oct 2024 13:50:55 GMT

# Observe it's being cached
$ curl -ik https://localhost/souin-api/souin/surrogate_keys
HTTP/2 200 
alt-svc: h3=":443"; ma=2592000
content-type: application/json
server: Caddy
content-length: 592
date: Sun, 20 Oct 2024 13:51:33 GMT

{"GET-https-localhost-/":"\u0004\"M\u0018dp\ufffd\ufffd\u0000\u0000\u0000\ufffd\ufffdHTTP/0.0 200 OK\r\nAlt-Svc: h3=\":443\"; ma=2592000\r\nCache-Control: \r\nContent-Type: text/plain; charset=utf-8\r\nDate: Sun, 20 Oct 2024 13:50:55 GMT\r\nServer: Caddy\r\nX-Souin-Stored-Length: 12\u001b\u0000\ufffd\nTtl: 2m0s\r\n\r\nHello World!","IDX_GET-https-localhost-/":"\nV\n\u0015GET-https-localhost-/\u0012=\n\f\b\ufffd\ufffdԸ\u0006\u0010\ufffd\ufffdԃ\u0003\u0012\n\b\ufffd\ufffdԸ\u0006\u0010\ufffd\ufffd\u0001\u001a\n\b\ufffd\ufffdԸ\u0006\u0010\ufffd\ufffd\u00012\u0015GET-https-localhost-/"}%  

# Try to purge the cache with a regex
$ curl -ik -X PURGE 'https://localhost/souin-api/souin/.+'
HTTP/2 204 
alt-svc: h3=":443"; ma=2592000
server: Caddy
date: Sun, 20 Oct 2024 13:51:48 GMT

# Observe the cache was not purged
$ curl -ik https://localhost/souin-api/souin/surrogate_keys
HTTP/2 200 
alt-svc: h3=":443"; ma=2592000
content-type: application/json
server: Caddy
content-length: 592
date: Sun, 20 Oct 2024 13:51:50 GMT

{"GET-https-localhost-/":"\u0004\"M\u0018dp\ufffd\ufffd\u0000\u0000\u0000\ufffd\ufffdHTTP/0.0 200 OK\r\nAlt-Svc: h3=\":443\"; ma=2592000\r\nCache-Control: \r\nContent-Type: text/plain; charset=utf-8\r\nDate: Sun, 20 Oct 2024 13:50:55 GMT\r\nServer: Caddy\r\nX-Souin-Stored-Length: 12\u001b\u0000\ufffd\nTtl: 2m0s\r\n\r\nHello World!","IDX_GET-https-localhost-/":"\nV\n\u0015GET-https-localhost-/\u0012=\n\f\b\ufffd\ufffdԸ\u0006\u0010\ufffd\ufffdԃ\u0003\u0012\n\b\ufffd\ufffdԸ\u0006\u0010\ufffd\ufffd\u0001\u001a\n\b\ufffd\ufffdԸ\u0006\u0010\ufffd\ufffd\u00012\u0015GET-https-localhost-/"}

Note that curl -ik -X PURGE https://localhost/souin-api/souin/flush works. Maybe the fact that flush directly calls DeleteMany could explain the different behavior, but I'm not sure where the bug is coming from. Hope this info can help!

darkweak commented 6 days ago

Hey @mac-chaffee thank you for this feedback, I'll embark it in the PR #558.

darkweak commented 1 day ago

@mac-chaffee the commit 07d53bf5856586e391da1020e0a59f72dcb52ced should solve your issue:

xcaddy build --with github.com/darkweak/souin/plugins/caddy@07d53bf5856586e391da1020e0a59f72dcb52ced --with github.com/darkweak/souin@07d53bf5856586e391da1020e0a59f72dcb52ced