Skip to content

Commit

Permalink
Fix memcache
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Zheleztsov committed Jun 23, 2017
1 parent ad96359 commit 38c8202
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
23 changes: 17 additions & 6 deletions rest/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rest

import (
"crypto/md5"
"encoding/base64"
"github.com/bradfitz/gomemcache/memcache"
"strings"
)
Expand All @@ -26,7 +27,7 @@ func (mc MC) StoreToCache(key string, data []byte) error {

var item memcache.Item
// item.Key = hashKey(prefixed_key)
item.Key = prefixed_key
item.Key = hashKey(prefixed_key)
item.Value = data

// store
Expand All @@ -35,24 +36,34 @@ func (mc MC) StoreToCache(key string, data []byte) error {
return err
}

// (mc MC) GetFromCache ...
// GetFromCache get node data from memcache
func (mc MC) GetFromCache(key string) ([]byte, error) {
var data *memcache.Item
prefixed_key := strings.Join([]string{mc.Prefix, key}, "_")

// data, err := mc.Client.Get(hashKey(prefixed_key))
data, err := mc.Client.Get(prefixed_key)
data, err := mc.Client.Get(hashKey(prefixed_key))
if err != nil {
return []byte(""), err
}

return data.Value, err
}

// hashKey return md5sum of prefixed_key
// DeleteFromCache delete key from memcache
func (mc MC) DeleteFromCache(key string) error {
prefixed_key := strings.Join([]string{mc.Prefix, key}, "_")

err := mc.Client.Delete(hashKey(prefixed_key))

return err
}

// hashKey return base64 encoded md5sum of prefixed_key
func hashKey(key string) string {
h := md5.New()
sum := h.Sum([]byte(key))

return string(sum)
b64 := base64.StdEncoding.EncodeToString(sum)

return b64
}
53 changes: 30 additions & 23 deletions rest/zoo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ func (z ZooNode) GetChildrens(path string) Ls {
l.State = "OK"
l.Path = path

if z.MC.Enabled {
data, err := z.MC.GetFromCache(lsPath)
if err != nil {
log.Print("V1 LS ERROR: ", err.Error())
} else {
log.Print("We are get it from memecache!")
childrens := strings.Split(string(data), ",")
l.Childrens = childrens
return l
}
}
// if z.MC.Enabled {
// data, err := z.MC.GetFromCache(lsPath)
// if err != nil {
// log.Print("V1 LS ERROR: ", err.Error())
// } else {
// log.Print("We are get it from memecache!")
// childrens := strings.Split(string(data), ",")
// l.Childrens = childrens
// return l
// }
// }

childrens, zkStat, err := z.Conn.Children(lsPath)
if err != nil {
Expand All @@ -69,13 +69,13 @@ func (z ZooNode) GetChildrens(path string) Ls {
return l
}

// Store to cache
if z.MC.Enabled {
err := z.MC.StoreToCache(lsPath, []byte(strings.Join(childrens, ",")))
if err != nil {
log.Print("V1 LS: ", err.Error())
}
}
// // Store to cache
// if z.MC.Enabled {
// err := z.MC.StoreToCache(lsPath, []byte(strings.Join(childrens, ",")))
// if err != nil {
// log.Print("V1 LS: ", err.Error())
// }
// }

l.Error = ""
l.Childrens = childrens
Expand Down Expand Up @@ -105,7 +105,7 @@ func (z ZooNode) GetNode(path string) Get {
// Get data from memcached
if z.MC.Enabled {
if data, err := z.MC.GetFromCache(getPath); err != nil {
log.Print("V1 GET: ", err.Error())
log.Print("[mc ERROR] ", err.Error())
} else {
g.Data = data
return g
Expand All @@ -123,7 +123,7 @@ func (z ZooNode) GetNode(path string) Get {
if z.MC.Enabled {
err := z.MC.StoreToCache(getPath, data)
if err != nil {
log.Print("V1 LS: ", err.Error())
log.Print("[mc ERROR] ", err.Error())
}
}

Expand Down Expand Up @@ -151,8 +151,15 @@ func (z ZooNode) RMR(path string) {
err = z.Conn.Delete(path, -1)
if err != nil {
log.Print("[zk ERROR] ", err)
} else {
if z.MC.Enabled {
err := z.MC.DeleteFromCache(path)
if err != nil {
log.Print("[mc ERROR] ", err.Error())
}
}
log.Print("[WARNING] ", path, " deleted")
}
log.Print("[WARNING] ", path, " deleted")
}

// CreateNode ...
Expand Down Expand Up @@ -190,7 +197,7 @@ func (z ZooNode) UpdateNode(path string, content []byte) string {

if z.MC.Enabled {
if err := z.MC.StoreToCache(upPath, content); err != nil {
log.Print("V1 update ERROR: ", err.Error())
log.Print("[mc ERROR] ", err.Error())
}
}

Expand All @@ -214,7 +221,7 @@ func (z ZooNode) CreateChild(path string, content []byte) string {

if z.MC.Enabled {
if err := z.MC.StoreToCache(crPath, content); err != nil {
log.Print("V1 create ERROR: ", err.Error())
log.Print("[mc ERROR] ", err.Error())
}
}

Expand Down

0 comments on commit 38c8202

Please sign in to comment.