Fix memcache

This commit is contained in:
Denis Zheleztsov 2017-06-23 10:22:49 +03:00
parent ad9635920a
commit 38c820280e
2 changed files with 47 additions and 29 deletions

View File

@ -2,6 +2,7 @@ package rest
import ( import (
"crypto/md5" "crypto/md5"
"encoding/base64"
"github.com/bradfitz/gomemcache/memcache" "github.com/bradfitz/gomemcache/memcache"
"strings" "strings"
) )
@ -26,7 +27,7 @@ func (mc MC) StoreToCache(key string, data []byte) error {
var item memcache.Item var item memcache.Item
// item.Key = hashKey(prefixed_key) // item.Key = hashKey(prefixed_key)
item.Key = prefixed_key item.Key = hashKey(prefixed_key)
item.Value = data item.Value = data
// store // store
@ -35,13 +36,12 @@ func (mc MC) StoreToCache(key string, data []byte) error {
return err return err
} }
// (mc MC) GetFromCache ... // GetFromCache get node data from memcache
func (mc MC) GetFromCache(key string) ([]byte, error) { func (mc MC) GetFromCache(key string) ([]byte, error) {
var data *memcache.Item var data *memcache.Item
prefixed_key := strings.Join([]string{mc.Prefix, key}, "_") prefixed_key := strings.Join([]string{mc.Prefix, key}, "_")
// data, err := mc.Client.Get(hashKey(prefixed_key)) data, err := mc.Client.Get(hashKey(prefixed_key))
data, err := mc.Client.Get(prefixed_key)
if err != nil { if err != nil {
return []byte(""), err return []byte(""), err
} }
@ -49,10 +49,21 @@ func (mc MC) GetFromCache(key string) ([]byte, error) {
return data.Value, 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 { func hashKey(key string) string {
h := md5.New() h := md5.New()
sum := h.Sum([]byte(key)) sum := h.Sum([]byte(key))
return string(sum) b64 := base64.StdEncoding.EncodeToString(sum)
return b64
} }

View File

@ -50,17 +50,17 @@ func (z ZooNode) GetChildrens(path string) Ls {
l.State = "OK" l.State = "OK"
l.Path = path l.Path = path
if z.MC.Enabled { // if z.MC.Enabled {
data, err := z.MC.GetFromCache(lsPath) // data, err := z.MC.GetFromCache(lsPath)
if err != nil { // if err != nil {
log.Print("V1 LS ERROR: ", err.Error()) // log.Print("V1 LS ERROR: ", err.Error())
} else { // } else {
log.Print("We are get it from memecache!") // log.Print("We are get it from memecache!")
childrens := strings.Split(string(data), ",") // childrens := strings.Split(string(data), ",")
l.Childrens = childrens // l.Childrens = childrens
return l // return l
} // }
} // }
childrens, zkStat, err := z.Conn.Children(lsPath) childrens, zkStat, err := z.Conn.Children(lsPath)
if err != nil { if err != nil {
@ -69,13 +69,13 @@ func (z ZooNode) GetChildrens(path string) Ls {
return l return l
} }
// Store to cache // // Store to cache
if z.MC.Enabled { // if z.MC.Enabled {
err := z.MC.StoreToCache(lsPath, []byte(strings.Join(childrens, ","))) // err := z.MC.StoreToCache(lsPath, []byte(strings.Join(childrens, ",")))
if err != nil { // if err != nil {
log.Print("V1 LS: ", err.Error()) // log.Print("V1 LS: ", err.Error())
} // }
} // }
l.Error = "" l.Error = ""
l.Childrens = childrens l.Childrens = childrens
@ -105,7 +105,7 @@ func (z ZooNode) GetNode(path string) Get {
// Get data from memcached // Get data from memcached
if z.MC.Enabled { if z.MC.Enabled {
if data, err := z.MC.GetFromCache(getPath); err != nil { if data, err := z.MC.GetFromCache(getPath); err != nil {
log.Print("V1 GET: ", err.Error()) log.Print("[mc ERROR] ", err.Error())
} else { } else {
g.Data = data g.Data = data
return g return g
@ -123,7 +123,7 @@ func (z ZooNode) GetNode(path string) Get {
if z.MC.Enabled { if z.MC.Enabled {
err := z.MC.StoreToCache(getPath, data) err := z.MC.StoreToCache(getPath, data)
if err != nil { if err != nil {
log.Print("V1 LS: ", err.Error()) log.Print("[mc ERROR] ", err.Error())
} }
} }
@ -151,8 +151,15 @@ func (z ZooNode) RMR(path string) {
err = z.Conn.Delete(path, -1) err = z.Conn.Delete(path, -1)
if err != nil { if err != nil {
log.Print("[zk ERROR] ", err) 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 ... // CreateNode ...
@ -190,7 +197,7 @@ func (z ZooNode) UpdateNode(path string, content []byte) string {
if z.MC.Enabled { if z.MC.Enabled {
if err := z.MC.StoreToCache(upPath, content); err != nil { if err := z.MC.StoreToCache(upPath, content); err != nil {
log.Print("V1 update ERROR: ", err.Error()) log.Print("[mc ERROR] ", err.Error())
} }
} }
@ -214,7 +221,7 @@ func (z ZooNode) CreateChild(path string, content []byte) string {
if z.MC.Enabled { if z.MC.Enabled {
if err := z.MC.StoreToCache(crPath, content); err != nil { if err := z.MC.StoreToCache(crPath, content); err != nil {
log.Print("V1 create ERROR: ", err.Error()) log.Print("[mc ERROR] ", err.Error())
} }
} }