先下载包:
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("[email protected]"), 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("删除成功了") } }
完结
标签:username1,mc,fmt,Value,memcache,go,操作 From: https://www.cnblogs.com/ypeih/p/17299434.html