Connecting

Go

On each connection, the first command sent to the server must authenticate by issuing a set with username password as the value. The proxy responds STORED on success or CLIENT_ERROR invalid credentials on failure. The key name (shown here as _auth) is ignored by the proxy — any value works. This is the default Memcached authentication mechanism and does not require SASL support, which not every client or hosting environment has properly compiled in.

Use mc1.public.gra1.ovh.velstash.io to connect to your instance. We recommend using TLS (port 11212) for connections over the public Internet, since traffic between you and this region (cached values as well as credentials) may be easier to intercept if travelling unencrypted. We reserve the right to introduce usage-based pricing for disproportionate external traffic in the future, with advance notice.

Placeholders — replace with your instance's actual values: host mc1.public.gra1.ovh.velstash.io, username your-username, password your-password.

Installing a client

gomemcache is used in the examples below.

go get github.com/bradfitz/gomemcache/memcache

Plain TCP

package main

import (
    "fmt"

    "github.com/bradfitz/gomemcache/memcache"
)

func main() {
    client := memcache.New("mc1.public.gra1.ovh.velstash.io:11211")

    auth := []byte("your-username your-password")

    if err := client.Set(&memcache.Item{Key: "_auth", Value: auth}); err != nil {
        panic(fmt.Sprintf("auth error: %v", err))
    }

    if err := client.Set(&memcache.Item{Key: "mykey", Value: []byte("bar")}); err != nil {
        panic(err)
    }

    item, err := client.Get("mykey")
    if err != nil {
        panic(err)
    }
    fmt.Println(string(item.Value))
}

TLS

package main

import (
    "context"
    "crypto/tls"
    "fmt"
    "net"

    "github.com/bradfitz/gomemcache/memcache"
)

func main() {
        host := "mc1.public.gra1.ovh.velstash.io:11212"

    client := memcache.New(host)
    client.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
                dialer := &tls.Dialer{
            Config: &tls.Config{ServerName: "mc1.public.gra1.ovh.velstash.io"}
        }
        return dialer.DialContext(ctx, network, address)
    }

    auth := []byte("your-username your-password")

    if err := client.Set(&memcache.Item{Key: "_auth", Value: auth}); err != nil {
        panic(fmt.Sprintf("auth error: %v", err))
    }

    if err := client.Set(&memcache.Item{Key: "mykey", Value: []byte("bar")}); err != nil {
        panic(err)
    }

    item, err := client.Get("mykey")
    if err != nil {
        panic(err)
    }
    fmt.Println(string(item.Value))
}

Reading and writing

err := client.Set(&memcache.Item{
    Key:        "user:123:profile",
    Value:      data,
    Expiration: 3600, // seconds
})

item, err := client.Get("user:123:profile")
if err == memcache.ErrCacheMiss {
    // not found
} else if err != nil {
    // other error
}

Common pitfalls

  • Items over 1MB (the default max item size) fail silently on set — check the result code or error your client returns if a value doesn't seem to be sticking.
  • Prefixing keys with : (e.g. user:123:profile) is what makes the dashboard's top-keys/prefixes view group things meaningfully — worth doing from day one.
  • The auth set authenticates the underlying TCP connection, not the client object — it only needs to run once per connection (that's why each example above issues it right after connecting, not before every request). If your client library pools connections or silently reopens them, make sure new connections authenticate too before you rely on them.