Skip to content

Instantly share code, notes, and snippets.

@yudppp
Last active October 7, 2015 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yudppp/e3e3cbbe0bdd2143ec50 to your computer and use it in GitHub Desktop.
Save yudppp/e3e3cbbe0bdd2143ec50 to your computer and use it in GitHub Desktop.
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