7use Ubiquity\utils\base\UArray;
18class ObjectCache
extends AbstractDataCache {
26 private $_cacheDirectory;
38 public function __construct($root, $postfix =
"", $cacheParams = []) {
39 parent::__construct($root, $postfix);
40 $this->_cacheDirectory = \basename($root);
41 $this->_fileMode = (isset($cacheParams[
"fileMode"])) ? $cacheParams[
"fileMode"] : 0755;
42 if (! is_dir($root)) {
43 \mkdir($root, $this->_fileMode,
true);
54 public function exists($key) {
55 return file_exists($this->_getPath($key));
58 public function store($key, $content, $tag =
null) {
59 $path = $this->_getPath($key);
60 $dir = pathinfo($path, PATHINFO_DIRNAME);
61 if (UFileSystem::safeMkdir($dir)) {
62 $content =
"<?php\n" . UArray::asPhpClass($content, $this->getClassname($key), $this->getNamespace($key),
true);
63 if (@\file_put_contents($path, $content, LOCK_EX) ===
false) {
64 throw new CacheException(
"Unable to write cache file: {$path}");
66 if (@\chmod($path, $this->_fileMode) ===
false) {
67 throw new CacheException(
"Unable to set permissions of cache file: {$path}");
70 throw new CacheException(
"Unable to create folder : {$dir}");
74 protected function getCompleteClassname($key) {
82 return "\\{$this->_cacheDirectory}\\{$key}_cache";
85 protected function getNamespace($key) {
93 return $this->_cacheDirectory .
'\\' . \substr($key, 0, \strrpos($key,
'\\'));
96 protected function getClassname($key) {
104 return \substr($key, \strrpos($key,
'\\') + 1) .
'_cache';
114 public function fetch($key) {
115 $class = $this->getCompleteClassname($key);
116 return $class::$value;
126 public function file_get_contents($key) {
127 return \file_get_contents($this->_getPath($key));
137 public function getTimestamp($key) {
138 return \filemtime($this->_getPath($key));
148 private function _getPath($key) {
149 $key = \str_replace(
'.',
'_', $key);
150 return $this->_root . $key .
'_cache.php';
158 public function remove($key) {
159 $file = $this->_getPath($key);
160 if (\file_exists($file))
161 return \unlink($file);
170 public function clear($matches =
"") {
171 $files = glob($this->_root . $matches .
'*');
172 foreach ($files as $file) {
183 public function getCacheFiles($type) {
184 return CacheFile::initFromFiles($this->_root . $type, \ucfirst($type),
function ($file) {
185 $path = UFileSystem::relativePath(dirname($file), $this->_root);
186 $file = \basename($file);
187 return $path . \DS . substr($file, 0, strpos($file, $this->postfix .
'.php'));
196 public function clearCache($type) {
197 CacheFile::delete($this->_root . \strtolower($type));
205 public function getCacheInfo() {
206 $result = parent::getCacheInfo();
207 $result .=
"\nRoot cache directory is <b>" . UFileSystem::cleanPathname($this->_root) .
"</b>.";
216 public function getEntryKey($key) {
217 return UFileSystem::cleanFilePathname($this->_getPath($key));
225 public function setRoot($root) {
226 $this->_root = \rtrim($root, \DS) . \DS;
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.