Ubiquity  2.0.0
php rapid development framework
ApcuCache.php
Go to the documentation of this file.
1 <?php
2 namespace Ubiquity\cache\system;
3 
6 
14  public function __construct($root,$postfix="") {
15  parent::__construct($root,$postfix);
16  }
17 
23  public function exists($key) {
24  return \apcu_exists($this->getRealKey($key));
25  }
26 
27  public function store($key, $code, $tag,$php=true) {
28  $this->storeContent($key, $code,$tag);
29  }
30 
37  protected function storeContent($key,$content,$tag) {
38  \apcu_store($this->getRealKey($key), $content);
39  }
40 
41  protected function getRealKey($key){
42  return $key;
43  }
44 
50  public function fetch($key) {
51  $result=\apcu_fetch($this->getRealKey($key));
52  return eval($result);
53  }
54 
60  public function file_get_contents($key) {
61  return \apcu_fetch($this->getRealKey($key));
62  }
63 
70  public function getTimestamp($key) {
71  $key=$this->getRealKey($key);
72  $cache = \apcu_cache_info();
73  if (empty($cache['cache_list'])) {
74  return false;
75  }
76  foreach ($cache['cache_list'] as $entry) {
77  if ($entry['info'] != $key) {
78  continue;
79  }
80  $creationTime = $entry['creation_time'];
81  return $creationTime;
82  }
83  return \time();
84  }
85 
86  public function remove($key) {
87  \apcu_delete($this->getRealKey($key));
88  }
89 
90  public function clear() {
91  \apcu_clear_cache();
92  }
93 
94  protected function getCacheEntries($type){
95  $entries=$this->getAllEntries();
96  return \array_filter($entries,function($v) use ($type){return UString::startswith($v['info'], $type);});
97  }
98 
99  protected function getAllEntries(){
100  $entries=[];
101  $cache = \apcu_cache_info();
102  if (!empty($cache['cache_list'])) {
103  $entries=$cache['cache_list'];
104  }
105  return $entries;
106  }
107 
108  public function getCacheFiles($type){
109  $result=[];
110  $entries=$this->getCacheEntries($type);
111  foreach ($entries as $entry) {
112  $key=$entry['info'];
113  if(UString::startswith($key, $type)){
114  $result[]=new CacheFile(\ucfirst($type),$key,$entry['creation_time'],$entry['mem_size'],$key);
115  }
116  }
117  if(\sizeof($result)===0)
118  $result[]=new CacheFile(\ucfirst($type),"","","");
119  return $result;
120  }
121 
122  public function clearCache($type){
123  $entries=$this->getCacheEntries($type);
124  foreach ($entries as $entry){
125  $this->remove($entry['info']);
126  }
127  }
128 
129  public function getEntryKey($key){
130  return $this->getRealKey($key);
131  }
132 }
fetch($key)
Fetches data stored for the given key.
Definition: ApcuCache.php:50
store($key, $code, $tag, $php=true)
Definition: ApcuCache.php:27
storeContent($key, $content, $tag)
Caches the given data with the given key.
Definition: ApcuCache.php:37
This class is responsible for storing values in apcu cache.
Definition: ApcuCache.php:10
file_get_contents($key)
return data stored for the given key.
Definition: ApcuCache.php:60
Inspired by (c) Rasmus Schultz rasmus@mindplay.dk https://github.com/mindplay-dk/php-annotations ...
getTimestamp($key)
Returns the timestamp of the last cache update for the given key.
Definition: ApcuCache.php:70
exists($key)
Check if annotation-data for the key has been stored.
Definition: ApcuCache.php:23
This class is responsible for storing Arrays in PHP files.
__construct($root, $postfix="")
Initializes the apcu cache-provider.
Definition: ApcuCache.php:14
static startswith($hay, $needle)
Definition: UString.php:12