-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcache.go
124 lines (107 loc) · 2.72 KB
/
mcache.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package mcache
import (
"context"
"io"
"time"
// "github.com/bradfitz/gomemcache/memcache"
"google.golang.org/appengine/memcache"
)
type logger interface {
SetOutput(out io.Writer)
Print(args ...interface{})
Printf(format string, args ...interface{})
Println(args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Infoln(args ...interface{})
Warn(args ...interface{})
Warnf(format string, args ...interface{})
Warnln(args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Errorln(args ...interface{})
}
type Cache struct {
// Client *memmemcache
}
func New(logger logger) *Cache {
// mc := memcache.New()
// opts := &xredis.Options{
// Host: "localhost",
// Port: 6379,
// Password: "", // no password set
// // DB: 0, // use default DB
// }
// if redisURL != "" {
// opts = &xredis.Options{}
// u, err := url.Parse(redisURL)
// if err != nil {
// logger.Error(err)
// return nil
// }
// opts.Host = u.Host
// if strings.Contains(opts.Host, ":") {
// opts.Host = strings.Split(opts.Host, ":")[0]
// }
// p, _ := u.User.Password()
// opts.Password = p
// // opts.User = u.User.Username()
// port, err := strconv.Atoi(u.Port())
// if err != nil {
// logger.Error("cache couldn't parse port")
// return nil
// }
// opts.Port = port
// }
// client := xredis.SetupClient(opts)
// pong, err := client.Ping()
// if err != nil {
// logger.Error(err)
// return nil
// }
// logger.Info("cache running", pong)
return &Cache{
// Client: mc,
}
}
func (cache *Cache) Get(key string) (string, error) {
value, err := cache.GetBytes(key)
if err != nil {
return "", err
}
return string(value[:]), nil
}
func (cache *Cache) Del(key string) error {
ctx := context.Background()
err := memcache.Delete(ctx, key)
return err
}
func (cache *Cache) Expire(key string) error {
err := cache.Del(key)
return err
}
func (cache *Cache) GetBytes(key string) ([]byte, error) { // Encourages BLOATED interfaces
ctx := context.Background()
item, err := memcache.Get(ctx, key)
if err != nil {
return nil, err
}
return item.Value, err
}
func (cache *Cache) Set(key string, value string, duration time.Duration) error {
return cache.SetBytes(key, []byte(value), duration)
}
func (cache *Cache) SetBytes(key string, value []byte, duration time.Duration) error { // Encourages BLOATED interfaces
// result := string(value[:])
ctx := context.Background()
item := &memcache.Item{}
// secs := int32(duration / time.Second)
item.Expiration = duration
item.Key = key
item.Value = value
return memcache.Set(ctx, item)
}
func (cache *Cache) FlushDB() error {
ctx := context.Background()
return memcache.Flush(ctx)
}