|
| 1 | +<?php |
| 2 | +namespace micro\cache\system; |
| 3 | + |
| 4 | +use micro\controllers\admin\popo\CacheFile; |
| 5 | +use phpFastCache\CacheManager; |
| 6 | +use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
| 7 | + |
| 8 | +/** |
| 9 | + * This class is responsible for storing values with PhpFastCache. |
| 10 | + */ |
| 11 | +class PhpFastCacheDriver extends AbstractDataCache{ |
| 12 | + /** |
| 13 | + * @var ExtendedCacheItemPoolInterface |
| 14 | + */ |
| 15 | + private $cacheInstance; |
| 16 | + /** |
| 17 | + * Initializes the cache-provider |
| 18 | + */ |
| 19 | + public function __construct($root,$postfix="",$cacheType="Mongodb") { |
| 20 | + parent::__construct($root,$postfix); |
| 21 | + $this->cacheInstance = CacheManager::getInstance($cacheType,["itemDetailedDate"=>true,'host' => '127.0.0.1', |
| 22 | + 'port' => '27017', |
| 23 | + 'username' => '', |
| 24 | + 'password' => '', |
| 25 | + 'timeout' => '1']); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Check if annotation-data for the key has been stored. |
| 30 | + * @param string $key cache key |
| 31 | + * @return boolean true if data with the given key has been stored; otherwise false |
| 32 | + */ |
| 33 | + public function exists($key) { |
| 34 | + return $this->cacheInstance->hasItem($this->getRealKey($key)); |
| 35 | + } |
| 36 | + |
| 37 | + public function store($key, $code,$tag=null, $php=true) { |
| 38 | + $this->storeContent($key, $code,$tag); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Caches the given data with the given key. |
| 43 | + * @param string $key cache key |
| 44 | + * @param string $content the source-code to be cached |
| 45 | + * @param string $tag |
| 46 | + */ |
| 47 | + protected function storeContent($key,$content,$tag) { |
| 48 | + $key=$this->getRealKey($key); |
| 49 | + $item=$this->cacheInstance->getItem($key); |
| 50 | + $item->set($content); |
| 51 | + $item->addTag($tag); |
| 52 | + $this->cacheInstance->save($item); |
| 53 | + } |
| 54 | + |
| 55 | + protected function getRealKey($key){ |
| 56 | + $key=\str_replace("/", "-", $key); |
| 57 | + return \str_replace("\\", "-", $key); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Fetches data stored for the given key. |
| 62 | + * @param string $key cache key |
| 63 | + * @return mixed the cached data |
| 64 | + */ |
| 65 | + public function fetch($key) { |
| 66 | + $result=$this->cacheInstance->getItem($this->getRealKey($key))->get(); |
| 67 | + return eval($result); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * return data stored for the given key. |
| 72 | + * @param string $key cache key |
| 73 | + * @return mixed the cached data |
| 74 | + */ |
| 75 | + public function file_get_contents($key) { |
| 76 | + return $this->cacheInstance->getItem($this->getRealKey($key))->get(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the timestamp of the last cache update for the given key. |
| 81 | + * |
| 82 | + * @param string $key cache key |
| 83 | + * @return int unix timestamp |
| 84 | + */ |
| 85 | + public function getTimestamp($key) { |
| 86 | + $key=$this->getRealKey($key); |
| 87 | + return $this->cacheInstance->getItem($key)->getCreationDate()->getTimestamp(); |
| 88 | + } |
| 89 | + |
| 90 | + public function remove($key) { |
| 91 | + $key=$this->getRealKey($key); |
| 92 | + $this->cacheInstance->deleteItem($this->getRealKey($key)); |
| 93 | + } |
| 94 | + |
| 95 | + public function clear() { |
| 96 | + $this->cacheInstance->clear(); |
| 97 | + } |
| 98 | + |
| 99 | + protected function getCacheEntries($type){ |
| 100 | + return $this->cacheInstance->getItemsByTag($type); |
| 101 | + } |
| 102 | + |
| 103 | + public function getCacheFiles($type){ |
| 104 | + $result=[]; |
| 105 | + $entries=$this->getCacheEntries($type); |
| 106 | + |
| 107 | + foreach ($entries as $entry) { |
| 108 | + $key=$entry->getKey(); |
| 109 | + $result[]=new CacheFile(\ucfirst($type),$key,$entry->getCreationDate()->getTimestamp(),"",$key); |
| 110 | + } |
| 111 | + if(\sizeof($result)===0) |
| 112 | + $result[]=new CacheFile(\ucfirst($type),"","",""); |
| 113 | + return $result; |
| 114 | + } |
| 115 | + |
| 116 | + public function clearCache($type){ |
| 117 | + $this->cacheInstance->deleteItemsByTag($type); |
| 118 | + } |
| 119 | +} |
0 commit comments