Skip to content

Instantly share code, notes, and snippets.

@sperelson
Created September 11, 2013 19:56
Show Gist options
  • Save sperelson/6528932 to your computer and use it in GitHub Desktop.
Save sperelson/6528932 to your computer and use it in GitHub Desktop.
A custom Laravel 4.x cache connector for extending the default memcached driver to support AWS ElastiCache. It is missing the important part: the Cache::extend() instruction. I've blogged the whole thing at blog.hapnic.com/2013/09/11/Laravel-4-and-ElastiCache/
<?php namespace Illuminate\Cache;
use Memcached;
class ElasticacheConnector {
/**
* Create a new Memcached connection.
*
* @param array $servers
* @return \Memcached
*/
public function connect(array $servers)
{
$memcached = $this->getMemcached();
// Set Elasticache options here
if (defined('\Memcached::OPT_CLIENT_MODE') && defined('\Memcached::DYNAMIC_CLIENT_MODE')) {
$memcache->setOption(\Memcached::OPT_CLIENT_MODE, \Memcached::DYNAMIC_CLIENT_MODE);
}
// For each server in the array, we'll just extract the configuration and add
// the server to the Memcached connection. Once we have added all of these
// servers we'll verify the connection is successful and return it back.
foreach ($servers as $server)
{
$memcached->addServer($server['host'], $server['port'], $server['weight']);
}
if ($memcached->getVersion() === false)
{
throw new \RuntimeException("Could not establish Memcached connection.");
}
return $memcached;
}
/**
* Get a new Memcached instance.
*
* @return \Memcached
*/
protected function getMemcached()
{
return new Memcached;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment