Caddy Redis Cache plugin

Hi! I’m new to Caddy and I love it so much already.

I’m playing around with writing some plugins to help me better understand the architecture.

What would be a good way to handle caching HTTP responses? I would like to put Caddy in front of several websites to handle caching, TLS, and maybe some security. I’ll make it so it doesn’t cache certain pages and HTTP methods.

At first I thought maybe I could use Middleware, but then I thought I would need to write two handlers; one handler to be called at the beginning to check if the cache exists, and another at the end to store the response. So, building it with Middlware didn’t seem like a good approach.

Then I thought about building something like the reverse_proxy directive. I think this would be the best route, but I haven’t started on it yet.

I hope it’s okay to ask for some advice like this.

Thank you :slight_smile:

What determines when a middleware runs is when it calls ServeHTTP on the next middleware. This means that you can run code before and after that call, so before calling the next middleware you can do your cache hit attempts, and after you can do your cache writes. Just one HTTP handler. The header directive works this way - if the “defer” option is set, then it’ll do its header operations on the way back down the middleware chain rather than on the way up.

We have a WIP cache handler that you could read through to get an idea how it would look. I definitely think there’s room for a Redis cache handler to exist as well.

1 Like

Thank you, @francislavoie that was very helpful :slight_smile:

This still needs lots of work, but I think I’m on the right track.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.