-
Notifications
You must be signed in to change notification settings - Fork 9
/
persistence_memcache.go
56 lines (49 loc) · 1.03 KB
/
persistence_memcache.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 (
"encoding/json"
"github.com/bradfitz/gomemcache/memcache"
"strings"
"sync"
)
const (
MEMCACHE_PERSISTENCE_KEY = "haproxyConfigMemcachePersist"
)
func init() {
PersistenceLayerMap["memcache"] = func() PersistenceLayer {
return new(PersistenceLayerMemcache)
}
}
type PersistenceLayerMemcache struct {
Memcache *memcache.Client
}
func (self *PersistenceLayerMemcache) Configure(c string) error {
s := strings.Split(c, ",")
self.Memcache = memcache.New(s...)
return nil
}
func (self *PersistenceLayerMemcache) GetConfig() (*Config, error) {
var c *Config
s, err := self.Memcache.Get(MEMCACHE_PERSISTENCE_KEY)
if err != nil {
return c, err
}
err = json.Unmarshal(s.Value, c)
if err == nil {
c.Mutex = new(sync.RWMutex)
}
return c, err
}
func (self *PersistenceLayerMemcache) SetConfig(c *Config) error {
b, err := json.Marshal(c)
if err != nil {
return err
}
err = self.Memcache.Set(&memcache.Item{
Key: MEMCACHE_PERSISTENCE_KEY,
Value: b,
})
if err != nil {
return err
}
return nil
}