Last active
October 7, 2015 13:16
-
-
Save yudppp/e3e3cbbe0bdd2143ec50 to your computer and use it in GitHub Desktop.
use memcached go
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
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