-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
56 lines (51 loc) · 1.25 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/go-redis/redis/v7"
log "github.com/sirupsen/logrus"
"net/http"
"net/url"
)
type redisCache struct {
client *redis.Client
}
func newRedisCache(url *url.URL) (*redisCache, error) {
redisOptions, err := redis.ParseURL(url.String())
if err != nil {
return nil, err
}
client := redis.NewClient(redisOptions)
return &redisCache{client: client}, nil
}
func (c *redisCache) get(req *http.Request) (*http.Response, error) {
key := "cache-proxy:" + req.URL.String()
data, err := c.client.Get(key).Bytes()
if err != nil {
if err != redis.Nil {
return nil, nil
}
return nil, fmt.Errorf("unable to get %q: %w", key, err)
}
res, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(data)), req)
if err != nil {
return nil, fmt.Errorf("unable to read response: %w", err)
}
return res, nil
}
func (c *redisCache) set(req *http.Request, res *http.Response) error {
buf := bytes.Buffer{}
err := res.Write(&buf)
if err != nil {
return fmt.Errorf("unable to write response to buffer: %w", err)
}
go func() {
key := "cache-proxy:" + req.URL.String()
err = c.client.Set(key, buf.Bytes(), ttl).Err()
if err != nil {
log.Warnf("unable to set %q: %v", key, err)
}
}()
return nil
}