Ubiquity  2.0.0
php rapid development framework
ArrayCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\cache\system;
4 
8 
17  private $_fileMode;
18 
25  public function __construct($root, $postfix="", $cacheParams=[]) {
26  parent::__construct($root, $postfix);
27  $this->_fileMode=(isset($cacheParams["fileMode"])) ? $cacheParams["fileMode"] : 0777;
28  if (!is_dir($root))
29  \mkdir($root, $this->_fileMode, true);
30  }
31 
37  public function exists($key) {
38  return file_exists($this->_getPath($key));
39  }
40 
47  protected function storeContent($key, $content, $tag) {
48  $path=$this->_getPath($key);
49  if (@\file_put_contents($path, $content, LOCK_EX) === false) {
50  throw new \Exception("Unable to write cache file: {$path}");
51  }
52  if (@\chmod($path, $this->_fileMode) === false) {
53  throw new \Exception("Unable to set permissions of cache file: {$path}");
54  }
55  }
56 
62  public function fetch($key) {
63  return include ($this->_getPath($key));
64  }
65 
71  public function file_get_contents($key) {
72  return \file_get_contents($this->_getPath($key));
73  }
74 
81  public function getTimestamp($key) {
82  return \filemtime($this->_getPath($key));
83  }
84 
91  private function _getPath($key) {
92  return $this->_root . DIRECTORY_SEPARATOR . $key . $this->postfix . '.php';
93  }
94 
100  public function remove($key) {
101  $file=$this->_getPath($key);
102  if (\file_exists($file))
103  return \unlink($file);
104  return false;
105  }
106 
112  public function clear($matches="") {
113  $files=glob($this->_root . '/' . $matches . '*');
114  foreach ( $files as $file ) {
115  if (\is_file($file))
116  \unlink($file);
117  }
118  }
119 
125  public function getCacheFiles($type) {
126  return CacheFile::initFromFiles(ROOT . DS . CacheManager::getCacheDirectory() . $type, \ucfirst($type), function ($file) use ($type) {
127  $file=\basename($file);
128  return $type . "/" . substr($file, 0, strpos($file, $this->postfix . '.php'));
129  });
130  }
131 
137  public function clearCache($type) {
138  CacheFile::delete(ROOT . DS . CacheManager::getCacheDirectory() . \strtolower($type));
139  }
140 
146  public function getCacheInfo() {
147  $result=parent::getCacheInfo();
148  $result.="<br>Root cache directory is <b>" . UFileSystem::cleanPathname($this->_root) . "</b>.";
149  return $result;
150  }
151 
157  public function getEntryKey($key) {
158  return UFileSystem::cleanPathname($this->_getPath($key));
159  }
160 }
fetch($key)
Fetches data stored for the given key.
Definition: ArrayCache.php:62
getTimestamp($key)
Returns the timestamp of the last cache update for the given key.
Definition: ArrayCache.php:81
Inspired by (c) Rasmus Schultz rasmus@mindplay.dk https://github.com/mindplay-dk/php-annotations ...
storeContent($key, $content, $tag)
Caches the given data with the given key.
Definition: ArrayCache.php:47
This class is responsible for storing Arrays in PHP files.
Definition: ArrayCache.php:12
This class is responsible for storing Arrays in PHP files.
__construct($root, $postfix="", $cacheParams=[])
Initializes the file cache-provider.
Definition: ArrayCache.php:25
exists($key)
Check if annotation-data for the key has been stored.
Definition: ArrayCache.php:37
file_get_contents($key)
return data stored for the given key.
Definition: ArrayCache.php:71
_getPath($key)
Maps a cache-key to the absolute path of a PHP file.
Definition: ArrayCache.php:91
static initFromFiles($folder, $type, $keyFunction=null)
Definition: CacheFile.php:22