Skip to content

Instantly share code, notes, and snippets.

@benr77
Last active September 17, 2017 08:23
Show Gist options
  • Save benr77/258e42642b4632d5a826 to your computer and use it in GitHub Desktop.
Save benr77/258e42642b4632d5a826 to your computer and use it in GitHub Desktop.
Memcached wrapper for persistent connections
<?php
namespace ChaletOps\BaseBundle\Utils;
/**
* Class MemcachedWrapper
*/
class MemcachedWrapper extends \Memcached
{
/**
* @param string $persistentId
*/
public function __construct($persistentId)
{
parent::__construct($persistentId);
}
/**
* Prevent adding of new servers as duplicates. We're persistent!
*
* @param array $servers
*
* @return bool
*/
public function addServers(array $servers)
{
if (0 == count($this->getServerList()))
{
return parent::addServers($servers);
}
return false;
}
/**
* Prevent adding of new server as duplicate. We're persistent!
*
* @param string $host
* @param int $port
* @param int $weight
*
* @return bool
*/
public function addServer($host, $port, $weight = 0)
{
foreach ($this->getServerList() as $server)
{
if ($server['host'] == $host && $server['port'] == $port)
{
return false;
}
}
return parent::addServer($host, $port, $weight);
}
}
@benr77
Copy link
Author

benr77 commented Jul 29, 2015

Wrapper for Memcached to permit persistent connections whilst preventing duplicate servers being added.

@ArlingtonHouse
Copy link

Thank you! Does the Memcached Cache Adapter in Symfony 3.3 change the approach on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment