16 use Symfony\Component\Cache\Adapter\FilesystemAdapter;
28 private $cacheDir =
null;
29 private $cache =
null;
30 private $fileUtil =
null;
39 $this->cacheDir = WCMF_BASE.$cacheDir;
48 $this->cacheDir = WCMF_BASE.$cacheDir;
54 public function exists($section, $key) {
55 $cache = $this->getCache($section);
56 return $cache->hasItem($this->sanitizeKey($key));
63 $cache = $this->getCache($section);
64 $item = $cache->getItem($this->sanitizeKey($key));
66 return (
new \DateTime())->setTimeStamp($item->get()[0]);
74 public function get($section, $key) {
75 $cache = $this->getCache($section);
76 $item = $cache->getItem($this->sanitizeKey($key));
77 return $item->isHit() ? $item->get()[1] :
null;
83 public function put($section, $key, $value, $lifetime=
null) {
84 $cache = $this->getCache($section);
85 $item = $cache->getItem($this->sanitizeKey($key));
86 $item->set([time(), $value]);
87 if ($lifetime !==
null) {
88 $item->expiresAfter($lifetime);
96 public function clear($section) {
97 if (preg_match(
'/\*$/', $section)) {
99 $cacheBaseDir = $this->getCacheDir();
100 if (is_dir($cacheBaseDir)) {
101 $dirStart = $this->sanitizeSection(preg_replace(
'/\*$/',
'', $section));
102 $directories = $this->fileUtil->getDirectories($cacheBaseDir);
103 foreach ($directories as $directory) {
104 if (strpos($directory, $dirStart) === 0) {
105 $directory = $cacheBaseDir.$directory;
106 $this->fileUtil->emptyDir($directory);
113 $directory = $this->getCacheDir().$this->sanitizeSection($section);
114 $this->fileUtil->emptyDir($directory);
123 $cacheDir = $this->getCacheDir();
124 if (is_dir($cacheDir)) {
125 $this->fileUtil->emptyDir($cacheDir);
133 private function getCacheDir() {
134 if ($this->cacheDir ==
null) {
135 $this->cacheDir = WCMF_BASE.
'/app/cache/';
137 return $this->cacheDir;
145 private function getCache($section) {
146 $section = $this->sanitizeSection($section);
147 if (!isset($this->caches[$section])) {
148 $this->caches[$section] =
new FilesystemAdapter($section, 0, $this->cacheDir);
150 return $this->caches[$section];
153 private function sanitizeSection($section) {
154 return preg_replace(
'/[^-+_.A-Za-z0-9]/',
'.', $section);
157 private function sanitizeKey($key) {
158 return strlen($key) === 0 ?
'.' : preg_replace(
'/[\{\}\(\)\/@\:]/',
'.', $key);