Create a gist now

Instantly share code, notes, and snippets.

@yudppp /mem.go
Last active Oct 7, 2015

What would you like to do?
use memcached go
package main
import (
"encoding/json"
"sync"
"github.com/bradfitz/gomemcache/memcache"
)
var once = &sync.Once{}
var mc *memcache.Client
func InitiallizeCache() {
mc = memcache.New("localhost:11211")
// multi memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212")
}
func SetCache(key string, value interface{}) bool {
once.Do(func() {
InitiallizeCache()
})
v, err := json.Marshal(value)
if err != nil {
return false
}
mc.Set(&memcache.Item{Key: key, Value: []byte(v)})
return true
}
func GetCache(k string, value interface{}) bool {
once.Do(func() {
InitiallizeCache()
})
it, err := mc.Get(k)
if err != nil {
return false
}
err = json.Unmarshal(it.Value, value)
if err != nil {
return false
}
return true
}
func DelCache(key string) bool {
once.Do(func() {
InitiallizeCache()
})
err := mc.Delete(key)
if err != nil {
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment