Add namespace.

This commit is contained in:
2014-06-02 18:58:49 +04:00
parent aec1a60985
commit 1ba341b064
159 changed files with 265 additions and 264 deletions

83
Cache/Cache.php Normal file
View File

@ -0,0 +1,83 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage Cache
* @since 2010-03-04
*/
abstract class Cache
{
/**
* Add an item to the cache
*
* @param string $key
* @param mixed $value
* @param int $expire
* @return bool
*/
abstract public function add($key, $value, $expire = 0);
/**
* Decrement item's value
*
* @param string $key
* @param int $decrement
* @return bool
*/
abstract public function decrement($key, $decrement = 1);
/**
* Delete item from the cache
*
* @param string $key
* @return bool
*/
abstract public function del($key);
/**
* Flush all existing items
*
* @return bool
*/
abstract public function flush();
/**
* Retrieve item from the cache
*
* @param mixed $key
* @return mixed
*/
abstract public function get($key);
/**
* Increment item's value
*
* @param string $key
* @param int $increment
* @return bool
*/
abstract public function increment($key, $increment = 1);
/**
* Replace value of the existing item
*
* @param string $key
* @param mixed $value
* @param int $expire
* @return bool
*/
abstract public function replace($key, $value, $expire = 0);
/**
* Store data in the cache
*
* @param string $key
* @param mixed $value
* @param int $expire
* @return bool
*/
abstract public function set($key, $value, $expire = 0);
}

74
Cache/CacheKey.php Normal file
View File

@ -0,0 +1,74 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage Cache
* @since 2010-03-10
*/
class CacheKey
{
/**
* @var Cache
*/
protected $cacher;
/**
* @var CacheKey
*/
protected $key;
protected $expire = 0;
/**
* @param Cacher $cacher
* @param string $key
* @param mixed $params
* @param int $expire
* @return CacheKey
*/
public function __construct($cacher, $key, $params = array(), $expire = 0)
{
$this->cacher = $cacher;
$this->key = $key;
if ($params) {
$params = (is_array($params)) ? implode('|', $params) : $params;
$this->key = $key . '_' . $params;
}
$this->expire = $expire;
}
protected function getExpire()
{
return $this->expire;
}
/**
* @param int $expire
*/
public function setExpire($expire)
{
$this->expire = $expire;
}
public function get()
{
return $this->cacher->get($this->key);
}
/**
* @param mixed $value
* @return bool
*/
public function set($value)
{
return $this->cacher->set($this->key, $value, $this->expire);
}
public function del()
{
return $this->cacher->del($this->key);
}
}

40
Cache/Cacher.php Normal file
View File

@ -0,0 +1,40 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage Cache
* @since 2010-03-04
*/
class Cacher
{
/**
* Initialized cachers
*
* @var array
*/
static protected $caches = array();
/**
* @param $cacher
* @param null|string $config
* @return Cache
* @throws InitializationException
*/
static public function get($cacher, $config = null)
{
if (!isset(self::$caches[$cacher])) {
if (!$config) {
$config = Config::get($cacher);
}
$cache = new $cacher($config);
if (!$cache instanceof Cache) {
throw new InitializationException('Cache driver "' . $cacher . '" must extends Cache');
}
self::$caches[$cacher] = $cache;
}
return self::$caches[$cacher];
}
}

160
Cache/MemcacheCache.php Normal file
View File

@ -0,0 +1,160 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage Cache
* @since 2010-03-04
*/
class MemcacheCache extends Cache
{
/**
* @var Memcache
*/
protected $connection = null;
/**
* @var null|string
*/
protected $key_salt = null;
/**
* One hour to live default
*
* @var int
*/
protected $expire = 3600;
public function __construct($config)
{
$this->connection = new Memcache();
if (isset($config['key_salt'])) {
$this->key_salt = $config['key_salt'];
unset($config['key_salt']);
}
$required = array('hostname', 'port');
foreach ($config as $c) {
foreach ($required as $option) {
if (!isset($c[$option])) {
throw new InitializationException('Configuration must have a "' . $option . '".');
}
}
$this->connection->addServer($c['hostname'], $c['port']);
}
}
/**
* Add an item to the cache
*
* @param string $key
* @param mixed $value
* @param int $expire
* @return bool
*/
public function add($key, $value, $expire = 0)
{
return $this->connection->add($this->getKey($key), $value, null, $this->getExpire($expire));
}
/**
* Decrement item's value
*
* @param string $key
* @param int $decrement
* @return bool
*/
public function decrement($key, $decrement = 1)
{
return $this->connection->decrement($this->getKey($key), $decrement);
}
/**
* Delete item from the cache
*
* @param string $key
* @internal param int $value
* @return bool
*/
public function del($key)
{
return $this->connection->delete($this->getKey($key), 0);
}
/**
* Flush all existing items
*
* @return bool
*/
public function flush()
{
return $this->connection->flush();
}
/**
* Retrieve item from the cache
*
* @param string $key
* @return mixed
*/
public function get($key)
{
return $this->connection->get($this->getKey($key));
}
/**
* Increment item's value
*
* @param string $key
* @param int $increment
* @return bool
*/
public function increment($key, $increment = 1)
{
return $this->connection->increment($this->getKey($key), $increment);
}
/**
* Replace value of the existing item
*
* @param string $key
* @param mixed $value
* @param int $expire
* @internal param mixed $var
* @return bool
*/
public function replace($key, $value, $expire = 0)
{
return $this->connection->replace($this->getKey($key), $value, null, $this->getExpire($expire));
}
/**
* Store data in the cache
*
* @param string $key
* @param mixed $value
* @param int $expire
* @return bool
*/
public function set($key, $value, $expire = 0)
{
return $this->connection->set($this->getKey($key), $value, null, $this->getExpire($expire));
}
/**
* @param string $key
* @return string
*/
protected function getKey($key)
{
return md5($this->key_salt . $key);
}
public function getExpire($expire)
{
return ($expire > 0) ? $expire : $this->expire;
}
}