aboutsummaryrefslogtreecommitdiff
path: root/rediscache/main.go
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2021-10-24 15:57:32 -0400
committerMitchell Riedstra <mitch@riedstra.dev>2021-10-24 16:01:05 -0400
commit235b8f871fdfa35f9595268d194d28a3de655ec0 (patch)
tree46f562fddffd38ee10d5e3d858dd80c088879689 /rediscache/main.go
parente0d4a3e50921dc07e23ef9aa107bdc78b3adf176 (diff)
downloadgo-website-0.0.16.tar.gz
go-website-0.0.16.tar.xz
Unix sockets for redis. Page use FS interface. Clear redis func.v0.0.16
Additionally, Funcs can be passed in.
Diffstat (limited to 'rediscache/main.go')
-rw-r--r--rediscache/main.go23
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)