Maslosoft Cache API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?php
namespace Maslosoft\Cache\Adapters;
use Maslosoft\Cache\Interfaces\CacheAdapterInterface;
use Memcached as Mc;
/**
* Memcached adapter.
*
* NOTE: There is memcache and memcached php extensions.
* This adapter requires memcached extension, which offers more fatures.
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Memcached implements CacheAdapterInterface
{
/**
*
* @var Mc
*/
private $mc = null;
public $servers = [
'localhost:11211'
];
public function __construct()
{
if (!class_exists(Mc::class))
{
return;
}
$this->mc = new Mc();
foreach ($this->servers as $connectionString)
{
list($host, $port) = explode(':', $connectionString);
$this->mc->addServer($host, $port);
}
}
public function clear()
{
return $this->mc->flush();
}
public function get($key)
{
return $this->mc->get($key);
}
public function has($key)
{
$this->mc->get($key);
return Mc::RES_NOTFOUND !== $this->mc->getResultCode();
}
public function remove($key)
{
return $this->mc->delete($key);
}
public function set($key, $data, $timeout = null)
{
return $this->mc->set($key, $data, $timeout);
}
public function isAvailable()
{
return extension_loaded('memcached');
}
}
API documentation generated by ApiGen