go操作memcache
先下载包:
go get github.com/bradfitz/gomemcache/memcache
window下打开memcache服务:
cd C:\Program Files\memcached
memcached.exe start
再打开一个cmd窗口 连接memcached
telnet localhost 11211
测试一下
set key 0 0 5
hello
get key
go操作示例1:
import ( "fmt" "github.com/bradfitz/gomemcache/memcache" ) var err error func main() { //连接 mc := memcache.New("127.0.0.1:11211") //设置值 mc.Set(&memcache.Item{ Key: "email", Value: []byte("aaaaa@qq.com"), Expiration: 200, //设置过期时间 }) mc.Add(&memcache.Item{ Key: "username1", Value: []byte("lampol1"), Expiration: 200, //设置过期时间 }) //获取值 res, _ := mc.Get("email") fmt.Println(string(res.Value)) res1, _ := mc.Get("username1") fmt.Println(string(res1.Value)) //修改 mc.Replace(&memcache.Item{ Key: "username1", Value: []byte("lampol3"), Expiration: 200, //设置过期时间 }) res2, _ := mc.Get("username1") fmt.Println(string(res2.Value)) //删除 err := mc.Delete("username1") if err == nil { fmt.Println("删除成功了") } }
完结
但行好事,莫问前程!
本文来自博客园,作者:yangphp,转载请注明原文链接:https://www.cnblogs.com/ypeih/p/17299434.html