- Introduction
- Libraries
- Creating a Memcached Connection
- Single Client
- Multiple Clients
- Server Pools
- Type Mappers
- Booleans
- Timestamps
Memcached (pronounced "Mem-cash-dee") is a distributed memory cache with basic key-value store functionality. Although it doesn't come with all the bells and whistles of Redis, it does offer faster speed, which is suitable for simple key-value data. For more information, read the official PHP documentation.
Opulence lets you choose whichever Memcached library you'd like. The built-in library \Memcached
is the most popular.
Opulence\Memcached\Memcached
acts as a convenient wrapper around Memcached. It accepts either a single client or a list of clients. Opulence uses magic methods to pass on method calls to the underlying Memcached client(s).
use Memcached as Client;
use Opulence\Memcached\Memcached;
// Create our connection
$client = new Client();
$client->addServer('localhost', 11211);
$memcached = new Memcached($client);
// Try it out
$memcached->set('foo', 'bar');
echo $memcached->get('foo'); // "bar"
You can get the client instance:
$memcached->getClient();
If you pass in multiple clients, one of them MUST be named default
.
use Memcached as Client;
use Opulence\Memcached\Memcached;
$defaultClient = new Client();
$defaultClient->addServer('127.0.0.1', 11211);
$backupClient = new Client();
$backupClient->addServer('127.0.0.2', 11211);
$clients = [
'default' => $defaultClient,
'backup' => $backupClient
];
$memcached = new Memcached($clients);
You can get a particular client instance:
$memcached->getClient('backup');
Note: The
default
client will always be used unless you callgetClient($name)
and make calls to that client directly.
\Memcached
allows you to add multiple servers to a client:
use Memcached as Client;
use Opulence\Memcached\Memcached;
$client = new Client();
$client->addServer('127.0.0.1', 11211);
$client->addServer('127.0.0.2', 11211);
$memcached = new Memcached($client);
You can get the client instance:
$memcached->getClient();
Opulence\Memcached\Types\TypeMapper
helps you translate to and from Memcached data types. For example, you cannot store a DateTime
object in Memcached, so you need to convert to a Unix timestamp when storing it. Conversely, when you read from Memcached, you can use a type mapper to convert the Unix timestamp back into a DateTime
object.
You can also use a factory to create type mappers:
use Opulence\Memcached\Types\Factories\TypeMapperFactory;
$typeMapper = (new TypeMapperFactory)->createTypeMapper();
$phpBoolean = true;
echo $typeMapper->toMemcachedBoolean($phpBoolean); // 1
$memcachedBoolean = 1;
echo $typeMapper->fromMemcachedBoolean($memcachedBoolean) === true; // 1
$phpDate = new DateTime('1987-07-24 12:34:56');
echo $typeMapper->toMemcachedTimestamp($phpDate); // 554128496
Note: This method accepts any object implementing
DateTimeInterface
, includingDateTimeImmutable
.
$memcachedDate = 554128496;
$phpDate = $typeMapper->fromMemcachedTimestamp($memcachedDate);
echo $phpDate->format('Y-m-d'); // "1987-07-24"