7use Ubiquity\utils\base\UArray;
18class ArrayCache
extends AbstractDataCache {
24 const PHP_TAG =
"<?php\n";
42 public function __construct($root, $postfix =
'', $cacheParams = []) {
43 parent::__construct($root, $postfix);
44 $this->_fileMode = $cacheParams[
'fileMode'] ?? 0755;
47 public function init() {
48 if (! is_dir($this->_root)) {
49 \mkdir($this->_root, $this->_fileMode,
true);
60 public function exists($key) {
61 return \file_exists($this->_getPath($key));
64 public function store($key, $code, $tag =
null) {
66 if (\is_array($code)) {
67 $content = self::PHP_TAG .
'return ' . UArray::asPhpArray($code,
'array') .
";\n";
69 $path = $this->_getPath($key);
70 $dir = pathinfo($path, PATHINFO_DIRNAME);
71 if (UFileSystem::safeMkdir($dir)) {
72 if (@\file_put_contents($path, $content, LOCK_EX) ===
false) {
73 throw new CacheException(
"Unable to write cache file: {$path}");
75 if (@\chmod($path, $this->_fileMode) ===
false) {
76 throw new CacheException(
"Unable to set permissions of cache file: {$path}");
79 throw new CacheException(
"Unable to create folder : {$dir}");
90 public function fetch($key) {
91 return include ($this->_getPath($key));
101 public function file_get_contents($key) {
102 return \file_get_contents($this->_getPath($key));
112 public function getTimestamp($key) {
113 return \filemtime($this->_getPath($key));
123 private function _getPath($key) {
124 return $this->_root . $key . $this->postfix .
'.php';
132 public function remove($key) {
133 $file = $this->_getPath($key);
134 if (\file_exists($file))
135 return \unlink($file);
144 public function clear($matches =
"") {
145 $files = glob($this->_root . $matches .
'*');
146 foreach ($files as $file) {
157 public function getCacheFiles($type) {
158 return CacheFile::initFromFiles($this->_root . $type, \ucfirst($type),
function ($file) {
159 $path = UFileSystem::relativePath(dirname($file), $this->_root);
160 $file = \basename($file);
161 return $path . \DS . substr($file, 0, strpos($file, $this->postfix .
'.php'));
170 public function clearCache($type) {
171 CacheFile::delete($this->_root . \strtolower($type));
179 public function getCacheInfo() {
180 $result = parent::getCacheInfo();
181 $result .=
"\nRoot cache directory is <b>" . UFileSystem::cleanPathname($this->_root) .
"</b>.";
190 public function getEntryKey($key) {
191 return UFileSystem::cleanFilePathname($this->_getPath($key));
199 public function setRoot($root) {
200 $this->_root = rtrim($root, \DS) . \DS;
File system utilities Ubiquity\utils\base$UFileSystem This class is part of Ubiquity.