Last active
January 19, 2016 20:58
-
-
Save i-void/2de07c66952428be9d29 to your computer and use it in GitHub Desktop.
PHP Kod Örneği
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 | |
namespace Netvent\AdminPanelBundle\Service; | |
use Aws\CloudFront\Exception\Exception; | |
use Doctrine\ORM\EntityManager; | |
use Horizon\TuketiyorumBundle\Service\Bundle\Cache\PriorityCacheInterface; | |
use Netvent\AdminPanelBundle\Components\StringHelper; | |
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | |
/** | |
* Class DynamicCache | |
* Cache listesi namespace altında cache edilmiş keyleri tutar | |
* Key kullanarak cache atama işleminde her key için bir unique prefix oluşturulur | |
* ve silme, güncelleme işlemlerinde bu prefix yeni bir unique prefix ile değiştirilir | |
* yeni prefix herhangi bir data içermeyeceği için suni bir silme işlemi gerçekleşmiş olur. | |
* Tüm namespace temizlenmek istenirse daha önce cache'lenmiş keylerin listesi alınarak | |
* her birinin prefixleri değiştirilir. | |
* | |
* @package Netvent\AdminPanelBundle\Service | |
*/ | |
abstract class DynamicCache implements CacheWarmerInterface { | |
protected $nameSpace; | |
protected $listKey; | |
protected $em; | |
protected $memcache; | |
protected $ttl; | |
public function __construct(PriorityCacheInterface $memcache, EntityManager $em, $ttl) | |
{ | |
/** @var PriorityCacheInterface memcache */ | |
$this->memcache = $memcache; | |
$this->em = $em; | |
$this->ttl = $ttl; | |
$this->setup(); | |
} | |
//Aşağıdaki iki alanı extend eden sınıfta set etmelisin! | |
//$this->nameSpace='dynamic_cache:'; | |
//$this->listKey='dynamic_cache_list'; | |
abstract function setup(); | |
/* | |
* Liste anahtarını kullanarak, ilgili cache listesini getirir | |
* @return Array | |
*/ | |
private function getCacheList() | |
{ | |
$value = $this->memcache->get($this->listKey); | |
return (is_array($value)) ? $value : []; | |
} | |
/* | |
* Cache listesine ilgili anahtarı ekler | |
* @param String $key Eklenecek anahtar | |
*/ | |
private function addKeyToCacheList($key) { | |
$key_arr = $this->getCacheList(); | |
if (!in_array($key,$key_arr)) { | |
$key_arr[] = $key; | |
$this->memcache->set($this->listKey, $key_arr, 0); | |
} | |
} | |
/* | |
* Cache listesinden ilgili anahtarı siler | |
* @param String $key Silinecek anahtar | |
*/ | |
private function removeKeyFromCacheList($key) { | |
$key_arr = $this->getCacheList(); | |
if(($index = array_search($key, $key_arr)) !== false) { | |
unset($key_arr[$index]); | |
$this->memcache->set($this->listKey, $key_arr, 0); | |
} | |
} | |
/* | |
* Listede bulunan anahtarlara bağlı tüm verileri siler | |
*/ | |
public function clearNameSpace() { | |
$key_arr = $this->getCacheList(); | |
foreach($key_arr as $index=>$key) { | |
$this->deleteData($key); | |
} | |
} | |
/* | |
* İlgili anahtarın prefixini değiştirerek eski verinin geçerliliğini kaldırır | |
* @param String $key Prefixi değiştirilecek anahtar | |
*/ | |
private function updatePrefix($key) { | |
$uid = StringHelper::getUniqueName(); | |
$this->memcache->set($this->nameSpace.$key, $uid, $this->ttl); | |
return $uid; | |
} | |
/* | |
* İlgili anahtarın prefixini getirir | |
* @param String $key Prefixi getirilecek anahtar | |
*/ | |
private function getPrefixOfKey($key) { | |
return $this->memcache->get($this->nameSpace.$key); | |
} | |
/* | |
* İlgili anahtara bağlı veriyi siler | |
* @param String $key Verisi silinecek anahtar | |
*/ | |
public function deleteData($key) { | |
$this->updatePrefix($key); | |
$this->removeKeyFromCacheList($key); | |
} | |
/* | |
* İlgili anahtarın verisini getirir | |
* @param String $key Verisi getirilecek anahtar | |
*/ | |
public function getData($key) { | |
$prefix = $this->getPrefixOfKey($key); | |
if (!empty($prefix)) { | |
return $this->memcache->get($prefix.':'.$key); | |
} else { | |
return false; | |
} | |
} | |
/* | |
* İlgili anahtara veri set eder | |
* @param String $key Veri eklenecek anahtar | |
*/ | |
public function setData($key, $data) { | |
$prefix = $this->getPrefixOfKey($key); | |
if (empty($prefix)) { | |
$prefix = $this->updatePrefix($key); | |
} | |
$this->memcache->set($prefix.':'.$key, $data, $this->ttl); | |
$this->addKeyToCacheList($key); | |
} | |
/* | |
* İlgili anahtarın verisini günceller | |
* @param String $key Verisi güncellenecek anahtar | |
*/ | |
public function updateData($key, $data) { | |
$this->deleteData($key); | |
$this->setData($key, $data); | |
} | |
/* CacheWarmerInterface zorunlu metodları */ | |
public function isOptional() | |
{ | |
return false; | |
} | |
public function warmUp($cacheDir) | |
{ | |
// Dinamik bir memcache yapısı olduğu için urller başta warmup edilemez. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment