From 38c820280e155b07df0aa3a07b5157445ab0dac4 Mon Sep 17 00:00:00 2001 From: Denis Zheleztsov Date: Fri, 23 Jun 2017 10:22:49 +0300 Subject: [PATCH] Fix memcache --- rest/cache.go | 23 ++++++++++++++++------ rest/zoo.go | 53 +++++++++++++++++++++++++++++---------------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/rest/cache.go b/rest/cache.go index b5258ed..b0d3147 100644 --- a/rest/cache.go +++ b/rest/cache.go @@ -2,6 +2,7 @@ package rest import ( "crypto/md5" + "encoding/base64" "github.com/bradfitz/gomemcache/memcache" "strings" ) @@ -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 @@ -35,13 +36,12 @@ 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 } @@ -49,10 +49,21 @@ func (mc MC) GetFromCache(key string) ([]byte, error) { 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 } diff --git a/rest/zoo.go b/rest/zoo.go index 12cb503..32cadcb 100644 --- a/rest/zoo.go +++ b/rest/zoo.go @@ -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 { @@ -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 @@ -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 @@ -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()) } } @@ -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 ... @@ -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()) } } @@ -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()) } }