diff options
Diffstat (limited to 'rediscache/main.go')
| -rw-r--r-- | rediscache/main.go | 23 |
1 files changed, 19 insertions, 4 deletions
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) |
