Memcache, refactoring, View helpers, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@124 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
72
cache/CacheKey.php
vendored
Normal file
72
cache/CacheKey.php
vendored
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-10
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class CacheKey
|
||||
{
|
||||
|
||||
protected $key;
|
||||
protected $params = '';
|
||||
protected $expire = 0;
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @param mixed $params
|
||||
* @param iCacheable $cacheable
|
||||
* @return CacheKey
|
||||
*/
|
||||
public function __construct($key, $params = array(), $cacheable)
|
||||
{
|
||||
$this->key = $key;
|
||||
if (!$cacheable instanceof iCacheable) {
|
||||
throw new GeneralException('CacheKey depends on iCacheable instance');
|
||||
}
|
||||
$this->cache = $cacheable->getCache();
|
||||
$this->expire = $cacheable->getKeyExpire($this->key);
|
||||
$this->params = (is_array($params)) ? implode('', $params) : $params;
|
||||
}
|
||||
|
||||
protected function getCacheKey()
|
||||
{
|
||||
$params = ($this->params) ? ('_' . $this->params) : '';
|
||||
return $this->key . $params;
|
||||
}
|
||||
|
||||
protected function getExpire()
|
||||
{
|
||||
return $this->expire;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $expire
|
||||
*/
|
||||
public function setExpire($expire)
|
||||
{
|
||||
$this->expire = $expire;
|
||||
}
|
||||
|
||||
public function get()
|
||||
{
|
||||
return $this->cache->get($this->getCacheKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set($value)
|
||||
{
|
||||
return $this->cache->set($this->getCacheKey(), $value, $this->expire);
|
||||
}
|
||||
|
||||
public function del()
|
||||
{
|
||||
return $this->cache->del($this->getCacheKey());
|
||||
}
|
||||
}
|
48
cache/CacheKeySet.php
vendored
Normal file
48
cache/CacheKeySet.php
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Cache
|
||||
* @since 2010-03-10
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class CacheKeySet extends CacheKey
|
||||
{
|
||||
public function get()
|
||||
{
|
||||
$set = $this->cache->get($this->key);
|
||||
$item_key = $this->getCacheKey();
|
||||
|
||||
if (!is_array($set) || !array_key_exists($item_key, $set)) {
|
||||
return false;
|
||||
}
|
||||
return $this->cache->get($item_key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set($value)
|
||||
{
|
||||
$set = $this->cache->get($this->key);
|
||||
if (!is_array($set)) {
|
||||
$set = array();
|
||||
}
|
||||
|
||||
$item_key = $this->getCacheKey();
|
||||
if (!$this->cache->set($item_key, $value, $this->expire)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$set[$item_key] = $this->cache->getExpire($this->expire);
|
||||
return $this->cache->set($this->key, $set, $this->expire);
|
||||
}
|
||||
|
||||
public function del()
|
||||
{
|
||||
return $this->cache->del($this->key);
|
||||
}
|
||||
}
|
54
cache/MemcacheCache.php
vendored
54
cache/MemcacheCache.php
vendored
@ -17,10 +17,26 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
protected $connection = null;
|
||||
|
||||
protected $key_salt = null;
|
||||
|
||||
/**
|
||||
* One hour to live default
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $expire = 3600;
|
||||
|
||||
protected $keys = array();
|
||||
|
||||
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) {
|
||||
@ -42,7 +58,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function add($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->add($key, $value, null, $expire);
|
||||
return $this->connection->add($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,7 +70,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function decrement($key, $decrement = 1)
|
||||
{
|
||||
return $this->connection->decrement($key, $decrement);
|
||||
return $this->connection->decrement($this->getKey($key), $decrement);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,7 +82,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function del($key)
|
||||
{
|
||||
return $this->connection->delete($key);
|
||||
return $this->connection->delete($this->getKey($key));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -82,12 +98,12 @@ class MemcacheCache extends Cache
|
||||
/**
|
||||
* Retrieve item from the cache
|
||||
*
|
||||
* @param mixed $key
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
public function get($key)
|
||||
{
|
||||
return $this->connection->get($key);
|
||||
return $this->connection->get($this->getKey($key));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,7 +115,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function increment($key, $increment = 1)
|
||||
{
|
||||
return $this->connection->increment($key, $increment);
|
||||
return $this->connection->increment($this->getKey($key), $increment);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,7 +128,7 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function replace($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->replace($key, $value, null, $expire);
|
||||
return $this->connection->replace($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -125,6 +141,28 @@ class MemcacheCache extends Cache
|
||||
*/
|
||||
public function set($key, $value, $expire = 0)
|
||||
{
|
||||
return $this->connection->set($key, $value, null, $expire);
|
||||
return $this->connection->set($this->getKey($key), $value, null, $this->getExpire($expire));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
protected function getKey($key)
|
||||
{
|
||||
if (!isset($this->keys[$key])) {
|
||||
$this->keys[$key] = md5($this->key_salt . $key);
|
||||
}
|
||||
return $this->keys[$key];
|
||||
}
|
||||
|
||||
public function getExpire($expire)
|
||||
{
|
||||
return ($expire > 0) ? $expire : $this->expire;
|
||||
}
|
||||
|
||||
public function cleanKeys()
|
||||
{
|
||||
$this->keys = array();
|
||||
}
|
||||
}
|
24
cache/iCacheable.php
vendored
Normal file
24
cache/iCacheable.php
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage cache
|
||||
* @since 2010-03-12
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
interface iCacheable
|
||||
{
|
||||
/**
|
||||
* @return Cache
|
||||
*/
|
||||
public function getCache();
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return Expiration time in seconds
|
||||
*/
|
||||
public function getKeyExpire($key);
|
||||
}
|
Reference in New Issue
Block a user