-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcache.go
73 lines (61 loc) · 1.67 KB
/
cache.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package rediscache
import (
"net/url"
"time"
"github.com/go-redis/redis"
)
// Cache contains options to connect to a badger database
// a TTL of 0 does not expire keys
type Cache struct {
TTL time.Duration
}
// Conn is a connection to a redis database
type Conn struct {
TTL time.Duration
client *redis.Client
}
// Stats displays stats about redis
type Stats map[string]interface{}
// NewCache creates a new Cache
func NewCache(defaultTimeout time.Duration) (*Cache, error) {
return &Cache{TTL: defaultTimeout}, nil
}
// Open opens a new connection to redis
func (c Cache) Open(uri string) (*Conn, error) {
u, err := url.Parse(uri)
if err != nil {
return &Conn{}, err
}
pass, _ := u.User.Password()
client := redis.NewClient(&redis.Options{
Addr: u.Hostname() + ":" + u.Port(),
Password: pass,
DB: 0,
})
return &Conn{TTL: c.TTL, client: client}, nil
}
// Close closes the redis connection
func (c *Conn) Close() error {
return c.client.Close()
}
// Write writes data to the cache with the default cache TTL
func (c *Conn) Write(k, v []byte) error {
return c.WriteTTL(k, v, c.TTL)
}
// WriteTTL writes data to the cache with an explicit TTL
// a TTL of 0 does not expire keys
func (c *Conn) WriteTTL(k, v []byte, ttl time.Duration) error {
return c.client.Set(string(k), v, ttl).Err()
}
// Read retrieves data for a key from the cache
func (c *Conn) Read(k []byte) ([]byte, error) {
return c.client.Get(string(k)).Bytes()
}
// Stats provides stats about the redis database
func (c *Conn) Stats() (map[string]interface{}, error) {
info, err := c.client.Info().Result()
if err != nil {
return map[string]interface{}{}, err
}
return parseRedisInfo(info)
}