-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Denis Zheleztsov
committed
Jun 22, 2017
1 parent
d14ad45
commit 1641bbf
Showing
5 changed files
with
150 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package rest | ||
|
||
import ( | ||
"crypto/md5" | ||
"github.com/bradfitz/gomemcache/memcache" | ||
"strings" | ||
) | ||
|
||
// Memcached connection settings | ||
type MC struct { | ||
Enabled bool `json:"enabled"` | ||
Hosts []string `json:"hosts"` | ||
Prefix string `json:"prefix"` | ||
Client *memcache.Client | ||
} | ||
|
||
// InitConnection to memcached cluster | ||
func (mc MC) InitConnection() *memcache.Client { | ||
client := memcache.New(mc.Hosts...) | ||
return client | ||
} | ||
|
||
// StoreToCache save data to memecache | ||
func (mc MC) StoreToCache(key string, data []byte) error { | ||
prefixed_key := strings.Join([]string{mc.Prefix, key}, "_") | ||
|
||
var item memcache.Item | ||
// item.Key = hashKey(prefixed_key) | ||
item.Key = prefixed_key | ||
item.Value = data | ||
|
||
// store | ||
err := mc.Client.Set(&item) | ||
|
||
return err | ||
} | ||
|
||
// (mc MC) GetFromCache ... | ||
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) | ||
if err != nil { | ||
return []byte(""), err | ||
} | ||
|
||
return data.Value, err | ||
} | ||
|
||
// hashKey return md5sum of prefixed_key | ||
func hashKey(key string) string { | ||
h := md5.New() | ||
sum := h.Sum([]byte(key)) | ||
|
||
return string(sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters