Here we explain how you setup and use MemCachier in Go.
We recommend the mc
memcache client. It supports the binary protocol and SASL authentication and was specifically designed to work well with MemCachier. However, if you prefer to use gomemcache
we have a fork of gomemcache
that works with MemCachier.
mc
This client is supports the binary protocol and SASL authentication and is maintained by MemCachier. To install it run:
$ go get github.com/memcachier/mc
Next, configure your memcached client in the following way:
"MEMCACHIER_USERNAME")
username := os.Getenv("MEMCACHIER_PASSWORD")
password := os.Getenv("MEMCACHIER_SERVERS")
server := os.Getenv(
c := mc.NewMC(server, username, password)defer c.Quit()
This will create a memcache client with default parameters. If you prefer, you can also configure the memcache client like so:
"MEMCACHIER_USERNAME")
username := os.Getenv("MEMCACHIER_PASSWORD")
password := os.Getenv("MEMCACHIER_SERVERS")
server := os.Getenv(
config := mc.DefaultConfig()// default
config.Hasher = mc.NewModuloHasher() 2 // default
config.Retries = 200 * time.Millisecond // default
config.RetryDelay = true // default
config.Failover = 2 * time.Second // default
config.ConnectionTimeout = 60 * time.Second // default
config.DownRetryDelay = 1 // default
config.PoolSize = true // default
config.TcpKeepAlive = 60 * time.Second // default
config.TcpKeepAlivePeriod = true // default
config.TcpNoDelay =
c := mc.NewMCwithConfig(server, username, password, config)defer c.Quit()
The values for MEMCACHIER_SERVERS
, MEMCACHIER_USERNAME
, and MEMCACHIER_PASSWORD
are listed on your cache overview page. Make sure to add them to your environment.
After this, you can start writing cache code in your app:
0 // seconds if less than 30 days, unix timestamp if more
exp := 0
flags := 0
cas := "foo", "bar", flags, exp, cas)
_, err := c.Set(if err != nil {
"Failed to set value: %s\n", err)
fmt.Printf(
}
"foo")
val, _, _, err := c.Get(if err != nil {
"Failed to fetch value: %s\n", err)
fmt.Printf(
}"Got value: %s\n", val) fmt.Printf(
gomemcache
We highly recommed to use the mc
client since it was designed to work well with MemCachier but using gomemcache
is also possilbe. While gomemcache
is a popular memcache client it only supports the ASCII protocol. We have a fork that allows you to use this client with MemCachier anyway.
To install our version of gomemcache
:
$ go get github.com/memcachier/gomemcache
Next, configure your memcached client in the following way:
"MEMCACHIER_USERNAME")
username := os.Getenv("MEMCACHIER_PASSWORD")
password := os.Getenv("MEMCACHIER_SERVERS")
servers := os.Getenv(
",")...)
mc := memcache.New(strings.Split(servers, byte(password)) mc.SetAuth(username, []
The values for MEMCACHIER_SERVERS
, MEMCACHIER_USERNAME
, and MEMCACHIER_PASSWORD
are listed on your cache overview page. Make sure to add them to your environment.
After this, you can start writing cache code in your app:
"foo", Value: []byte("my value")})
err := mc.Set(&memcache.Item{Key: if err != nil {
"Failed to set value: %s\n", err)
fmt.Printf(
}
"foo")
val, err := mc.Get(if err != nil {
"Failed to fetch value: %s\n", err)
fmt.Printf(
}"Got value: %s\n", val) fmt.Printf(