介绍
详细的介绍参考:
https://www.runoob.com/memcached/memcached-tutorial.html
由于memcached仅仅是一个高度内存的kv存储块,如果有更严谨的需求,需要如下选择:
- 集群,持久入磁盘,可恢复,多种业务模型(queue,set,list…),请选用redis.
- 服务一致性,服务发现,etcd
何时选用memcached最佳:
短时间内的业务量暴增,如秒杀
安装
拉取镜像,并跑3个实例,仅仅是均衡,并未关联集群
docker pull memcached
docker run --rm --name memchached1 -itd -p 11211:11211 memcached:latest
docker run --rm --name memchached2 -itd -p 11212:11211 memcached:latest
docker run --rm --name memchached3 -itd -p 11213:11211 memcached:latest
go客户端
package main
import (
"fmt"
"github.com/bradfitz/gomemcache/memcache"
//"time"
)
func main() {
mc := memcache.New("localhost:11211", "localhost:11212", "localhost:11213")
//mc := memcache.New("localhost:11211")
e := mc.Set(&memcache.Item{Key: "foo2", Value: []byte("3")})
// 10 second expired
//e := mc.Set(&memcache.Item{Key: "foo2", Value: []byte("3"), Expiration: 10})
if e != nil {
panic(e)
}
it, e := mc.Get("foo2")
if e != nil {
panic(e)
}
//newV,e :=mc.Increment("foo2", 15)
//if e!=nil {
// panic(e)
//}
fmt.Println(string(it.Value))
//fmt.Println(newV)
//time.Sleep(5 * time.Second)
//it2, e2 := mc.Get("foo2")
//fmt.Println(it2, e2)
//
//time.Sleep(6 * time.Second)
//it3, e3 := mc.Get("foo2")
//fmt.Println(it3, e3)
}