Connecting
PHP
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.
mc1.public.gra1.ovh.velstash.io,
username your-username, password your-password.
Installing the extension
You need ext-memcached — not ext-memcache (no "d").
The two are different, unmaintained-vs-maintained PHP extensions, and only
ext-memcached is used in the examples below.
# Debian/Ubuntu
sudo apt install php-memcached
# via PECL
pecl install memcached
# Docker (Debian-based images)
RUN apt-get update && apt-get install -y php-memcached
Plain TCP
PHP's ext-memcached is used here — no TLS variant is shown below, see the note underneath.
// The callback runs once when the persistent connection is first established.
// Subsequent requests reuse the connection without re-authenticating.
$mc = new Memcached('myapp', function (Memcached $mc) {
$mc->setOption(Memcached::OPT_BINARY_PROTOCOL, false);
$mc->addServer('mc1.public.gra1.ovh.velstash.io', 11211);
$mc->set('_auth', 'your-username your-password')
or throw new Exception('auth error');
});
TLS
PHP's ext-memcached has no native TLS support. If you need to
connect over the public Internet and want traffic encrypted, run
stunnel alongside your application as a
local TLS-terminating proxy, and point ext-memcached at
127.0.0.1 on the local plaintext port stunnel exposes.
# stunnel.conf
[velstash]
client = yes
accept = 127.0.0.1:11212
connect = mc1.public.gra1.ovh.velstash.io:11212
Then point ext-memcached at 127.0.0.1:11212 instead
of the public host directly.
Reading and writing
<?php
$mc->set('user:123:profile', $data, 3600); // 3600s TTL
$value = $mc->get('user:123:profile');
if ($mc->getResultCode() === Memcached::RES_NOTFOUND) {
// not a cache hit — false alone isn't enough, since false
// can also be a legitimately stored value
}
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
setauthenticates 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.