From 235b8f871fdfa35f9595268d194d28a3de655ec0 Mon Sep 17 00:00:00 2001 From: Mitchell Riedstra Date: Sun, 24 Oct 2021 15:57:32 -0400 Subject: Unix sockets for redis. Page use FS interface. Clear redis func. Additionally, Funcs can be passed in. --- rediscache/main.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'rediscache') diff --git a/rediscache/main.go b/rediscache/main.go index 7ae3d4d..04ca622 100644 --- a/rediscache/main.go +++ b/rediscache/main.go @@ -54,16 +54,31 @@ func (rw *redisHTTPResponseWriter) WriteData() { rw.Data = rw.buf.Bytes() } -// Simple function that will cache the response for given handler in redis -// and instead of responding with the result from the handler it will +// HandleWithParams is the same as Handle but caches for the GET params +// rather than discarding them. +func HandleWithParams(pool *redis.Pool, key string, next http.Handler) http.Handler { + return handle(pool, key, true, next) +} + +// Handle is a Simple function that will cache the response for given handler in +// redis and instead of responding with the result from the handler it will // simply dump the contents of the redis key if it exists. func Handle(pool *redis.Pool, key string, next http.Handler) http.Handler { + return handle(pool, key, false, next) +} + +func handle(pool *redis.Pool, key string, params bool, next http.Handler) http.Handler { //nolint:funlen return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { client := pool.Get() defer client.Close() + subkey := r.URL.Path + if params { + subkey = r.URL.Path + "?" + r.URL.RawQuery + } + content: - data, err := client.Do("HGET", key, r.URL.Path) + data, err := client.Do("HGET", key, subkey) if err != nil { // Assume something bad has happened with redis, we're // just going to log this and then pass through the @@ -83,7 +98,7 @@ func Handle(pool *redis.Pool, key string, next http.Handler) http.Handler { return } - _, err = client.Do("HSET", key, r.URL.Path, b) + _, err = client.Do("HSET", key, subkey, b) if err != nil { Logger.Println("ERROR: during set: ", err) -- cgit v1.2.3