Files
web-site/Internal/package-cache/Drivers/MemcacheDriver.php
T
2026-07-01 16:44:17 +03:00

147 lines
2.8 KiB
PHP

<?php namespace ZN\Cache\Drivers;
/**
* ZN PHP Web Framework
*
* "Simplicity is the ultimate sophistication." ~ Da Vinci
*
* @package ZN
* @license MIT [http://opensource.org/licenses/MIT]
* @author Ozan UYKUN [ozan@znframework.com]
*/
use Memcached;
use ZN\Support;
use ZN\ErrorHandling\Errors;
use ZN\Cache\DriverMappingAbstract;
use ZN\Cache\Exception\ConnectionRefusedException;
/**
* @codeCoverageIgnore
*/
class MemcacheDriver extends DriverMappingAbstract
{
/**
* Keeps memcahce class
*
* @param object
*/
protected $memcache;
/**
* Magic constructor
*
* @param void
*
* @return void
*/
public function __construct(?array $settings = NULL)
{
parent::__construct();
Support::library('Memcached', 'Memcached');
$this->memcache = new Memcached;
$config = $this->config['driverSettings'];
$config = ! empty($settings)
? $settings
: $config['memcache'];
if( ! $this->memcache->addServer($config['host'], $config['port'], $config['weight']) )
{
throw new ConnectionRefusedException(NULL, 'Memcached connection error!'); // @codeCoverageIgnore
}
}
/**
* Select key
*
* @param string $key
* @param mixed $compressed
*
* @return mixed
*/
public function select($key, $compressed = NULL)
{
return $this->memcache->get($key);
}
/**
* Insert key
*
* @param string $key
* @param mixed $var
* @param int $time
* @param mixed $compressed
*
* @return bool
*/
public function insert($key, $var, $time, $compressed)
{
return $this->memcache->set($key, $var, $time);
}
/**
* Delete key
*
* @param string $key
*
* @return bool
*/
public function delete($key)
{
return $this->memcache->delete($key);
}
/**
* Increment key
*
* @param string $key
* @param int $increment = 1
*
* @return int
*/
public function increment($key, $increment)
{
return $this->memcache->increment($key, $increment);
}
/**
* Decrement key
*
* @param string $key
* @param int $decrement = 1
*
* @return int
*/
public function decrement($key, $decrement)
{
return $this->memcache->decrement($key, $decrement);
}
/**
* Clean all cache
*
* @param void
*
* @return bool
*/
public function clean()
{
return $this->memcache->flush();
}
/**
* Get info
*
* @param mixed $type
*
* @return array
*/
public function info($type = NULL)
{
return $this->memcache->getStats() ?: [];
}
}