Ubiquity  2.0.2
php rapid development framework
ArrayCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\cache\system;
4 
9 
18  private $_fileMode;
19 
26  public function __construct($root, $postfix="", $cacheParams=[]) {
27  parent::__construct($root, $postfix);
28  $this->_fileMode=(isset($cacheParams["fileMode"])) ? $cacheParams["fileMode"] : 0777;
29  if (!is_dir($root))
30  \mkdir($root, $this->_fileMode, true);
31  }
32 
38  public function exists($key) {
39  return file_exists($this->_getPath($key));
40  }
41 
48  protected function storeContent($key, $content, $tag) {
49  $path=$this->_getPath($key);
50  if (@\file_put_contents($path, $content, LOCK_EX) === false) {
51  throw new CacheException("Unable to write cache file: {$path}");
52  }
53  if (@\chmod($path, $this->_fileMode) === false) {
54  throw new CacheException("Unable to set permissions of cache file: {$path}");
55  }
56  }
57 
63  public function fetch($key) {
64  return include ($this->_getPath($key));
65  }
66 
72  public function file_get_contents($key) {
73  return \file_get_contents($this->_getPath($key));
74  }
75 
82  public function getTimestamp($key) {
83  return \filemtime($this->_getPath($key));
84  }
85 
92  private function _getPath($key) {
93  return $this->_root . DIRECTORY_SEPARATOR . $key . $this->postfix . '.php';
94  }
95 
101  public function remove($key) {
102  $file=$this->_getPath($key);
103  if (\file_exists($file))
104  return \unlink($file);
105  return false;
106  }
107 
113  public function clear($matches="") {
114  $files=glob($this->_root . '/' . $matches . '*');
115  foreach ( $files as $file ) {
116  if (\is_file($file))
117  \unlink($file);
118  }
119  }
120 
126  public function getCacheFiles($type) {
127  return CacheFile::initFromFiles(ROOT . DS . CacheManager::getCacheDirectory() . $type, \ucfirst($type), function ($file) use ($type) {
128  $file=\basename($file);
129  return $type . "/" . substr($file, 0, strpos($file, $this->postfix . '.php'));
130  });
131  }
132 
138  public function clearCache($type) {
139  CacheFile::delete(ROOT . DS . CacheManager::getCacheDirectory() . \strtolower($type));
140  }
141 
147  public function getCacheInfo() {
148  $result=parent::getCacheInfo();
149  $result.="<br>Root cache directory is <b>" . UFileSystem::cleanPathname($this->_root) . "</b>.";
150  return $result;
151  }
152 
158  public function getEntryKey($key) {
159  return UFileSystem::cleanPathname($this->_getPath($key));
160  }
161 }
fetch($key)
Fetches data stored for the given key.
Definition: ArrayCache.php:63
getTimestamp($key)
Returns the timestamp of the last cache update for the given key.
Definition: ArrayCache.php:82
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:48
This class is responsible for storing Arrays in PHP files.
Definition: ArrayCache.php:13
This class is responsible for storing Arrays in PHP files.
__construct($root, $postfix="", $cacheParams=[])
Initializes the file cache-provider.
Definition: ArrayCache.php:26
exists($key)
Check if annotation-data for the key has been stored.
Definition: ArrayCache.php:38
file_get_contents($key)
return data stored for the given key.
Definition: ArrayCache.php:72
_getPath($key)
Maps a cache-key to the absolute path of a PHP file.
Definition: ArrayCache.php:92
static initFromFiles($folder, $type, $keyFunction=null)
Definition: CacheFile.php:22