Created
September 20, 2013 08:30
-
-
Save kolyadin/6634742 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class KaCache | |
{ | |
private $memObject,$name,$lifetime; | |
private $tags = array(); | |
private $app; | |
public function __construct(Ka $app) | |
{ | |
$this->memObject = new Memcached(); | |
$this->memObject->addServer('127.0.0.1', 11211) or die('Не могу подключиться к memcached'); | |
$this->app = $app; | |
} | |
public function set($name,$lifetime,$tags = array()) | |
{ | |
$this->name = $name; | |
$ttl = strtr($lifetime,array( | |
's' => '*1' | |
,'month' => '*3600*24*30' | |
,'m' => '*60' | |
,'h' => '*3600' | |
,'d' => '*3600*24' | |
,'y' => '*3600*24*365' | |
)); | |
$addTime = eval("return $ttl;"); | |
$this->lifetime = $addTime; | |
#echo date('d.m.Y H:i:s'), ' - ',date('d.m.Y H:i:s',$this->lifetime),'<br/>'; | |
if (count($tags)) $this->tags = $tags; | |
return $this; | |
} | |
private function parseTime() | |
{ | |
} | |
public function get($name) | |
{ | |
$get = $this->memObject->get($name); | |
if ($this->memObject->getResultCode() == 0) return $get; | |
return false; | |
} | |
public function saveWithTags($callbackData) | |
{ | |
$cacheElement = array('entry' => array('key' => $this->name, 'val' => $callbackData)); | |
if (count($this->tags)) | |
{ | |
$tags = array(); | |
foreach ($this->tags as $tag) | |
{ | |
$tags[$tag] = microtime(1); | |
if (!$this->get('tag_'.$tag)) | |
{ | |
$this->memObject->set('tag_'.$tag,$tags[$tag],0); | |
} | |
} | |
$cacheElement['tags'] = $tags; | |
} | |
if ($this->memObject->set($this->name,$cacheElement,$this->lifetime)) | |
{ | |
return $callbackData; | |
} | |
} | |
public function call($callback) | |
{ | |
if (!is_callable($callback)) throw new KaException('Неверный аргумент',401); | |
if (empty($this->name) || empty($this->lifetime)) throw new KaException('Не установлено название кэша и/или время жизни',402); | |
//print '<pre>'.print_r($this->memObject->getAllKeys(),true).'</pre>'; | |
#die; | |
if ($storeData = $this->get($this->name)) | |
{ | |
$tagsValid = 1; | |
if (isset($storeData['tags']) && count($storeData['tags'])) | |
{ | |
foreach ($storeData['tags'] as $tag => $tagVersion) | |
{ | |
if ($this->get('tag_'.$tag) > $tagVersion) | |
{ | |
$tagsValid = 0; | |
break; | |
} | |
} | |
} | |
if ($tagsValid) | |
{ | |
return $storeData['entry']['val']; | |
} | |
else | |
{ | |
return $this->saveWithTags($callback()); | |
} | |
} | |
else | |
{ | |
return $this->saveWithTags($callback()); | |
} | |
} | |
public function flushTag(array $tags) | |
{ | |
foreach ($tags as $tag) | |
{ | |
if ($this->get('tag_'.$tag)) | |
{ | |
$this->memObject->replace('tag_'.$tag,microtime(1),0); | |
} | |
} | |
} | |
public function delete($name) | |
{ | |
$this->memObject->delete($name); | |
} | |
public function flush() | |
{ | |
$this->memObject->flush(); | |
} | |
public function dump() | |
{ | |
foreach ($this->memObject->getAllKeys() as $key) | |
{ | |
if (strpos($key,'tag_') === false) continue; | |
echo "$key | ".$this->get($key)."<br/>"; | |
} | |
} | |
/* | |
$data = $this->cache->set('test1','24s',['a','b'])->call(function(){ | |
}); | |
$this->cache->get('test1'); | |
$this->cache->flush('test1'); | |
$this->cache->flushTag('alfa'); | |
$this->cache->flushTag(array('alfa','beta')); | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment