-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
54 lines (43 loc) · 1.06 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
package mxcache
import (
"fmt"
"log"
"net/url"
)
type MXCacheCreator func(u *url.URL) (MXCacher, error)
type expiredKeys []string
type MXCacher interface {
Get(key string) (interface{}, error)
Set(key string, data interface{}, ex int) error
Expire(pattern string) (expiredKeys, error)
}
var cacheBackends = map[string]MXCacheCreator{
"memory": newMemoryCache,
"redis": newRedisCache,
"mem+redis": newMemRedisCache,
}
func NewMXCache(uri string) (MXCacher, error) {
if uri == "" {
return nilCache{}, nil
}
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
backendCreator, ok := cacheBackends[u.Scheme]
if !ok {
return nil, fmt.Errorf("invalid cache backend %s", u.Scheme)
}
log.Println("Setting up", u.Scheme, "cache with", u.String())
return backendCreator(u)
}
type nilCache struct{}
func (c nilCache) Set(key string, data interface{}, ex int) error {
return nil
}
func (c nilCache) Get(key string) (interface{}, error) {
return nil, nil
}
func (c nilCache) Expire(pattern string) (expiredKeys, error) {
return []string{}, nil
}