FileCache.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\io\impl;
12 
15 
16 use Symfony\Component\Cache\Adapter\FilesystemAdapter;
17 
18 /**
19  * FileCache provides simple caching functionality.
20  *
21  * The cache is divided into different sections. All sections share
22  * the same base path.
23  *
24  * @author ingo herwig <ingo@wemove.com>
25  */
26 class FileCache implements Cache {
27 
28  private $cacheDir = null;
29  private $cache = null;
30  private $fileUtil = null;
31  private $caches = [];
32 
33  /**
34  * Constructor
35  * @param $cacheDir String
36  */
37  public function __construct($cacheDir=null) {
38  $this->fileUtil = new FileUtil();
39  $this->cacheDir = WCMF_BASE.$cacheDir;
40  }
41 
42  /**
43  * Set the cache directory (defaults to WCMF_BASE/app/cache/ if not given).
44  * If not existing, the directory will be created relative to WCMF_BASE.
45  * @param $cacheDir String
46  */
47  public function setCacheDir($cacheDir) {
48  $this->cacheDir = WCMF_BASE.$cacheDir;
49  }
50 
51  /**
52  * @see Cache::exists()
53  */
54  public function exists($section, $key) {
55  $cache = $this->getCache($section);
56  return $cache->hasItem($this->sanitizeKey($key));
57  }
58 
59  /**
60  * @see Cache::getDate()
61  */
62  public function getDate($section, $key) {
63  $cache = $this->getCache($section);
64  $item = $cache->getItem($this->sanitizeKey($key));
65  if ($item->isHit()) {
66  return (new \DateTime())->setTimeStamp($item->get()[0]);
67  }
68  return null;
69  }
70 
71  /**
72  * @see Cache::get()
73  */
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;
78  }
79 
80  /**
81  * @see Cache::put()
82  */
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);
89  }
90  $cache->save($item);
91  }
92 
93  /**
94  * @see Cache::clear()
95  */
96  public function clear($section) {
97  if (preg_match('/\*$/', $section)) {
98  // handle wildcards
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);
107  @rmdir($directory);
108  }
109  }
110  }
111  }
112  else {
113  $directory = $this->getCacheDir().$this->sanitizeSection($section);
114  $this->fileUtil->emptyDir($directory);
115  rmdir($directory);
116  }
117  }
118 
119  /**
120  * @see Cache::clearAll()
121  */
122  public function clearAll() {
123  $cacheDir = $this->getCacheDir();
124  if (is_dir($cacheDir)) {
125  $this->fileUtil->emptyDir($cacheDir);
126  }
127  }
128 
129  /**
130  * Get the cache root directory
131  * @return String
132  */
133  private function getCacheDir() {
134  if ($this->cacheDir == null) {
135  $this->cacheDir = WCMF_BASE.'/app/cache/';
136  }
137  return $this->cacheDir;
138  }
139 
140  /**
141  * Get the cache for the specified section
142  * @param $section The caching section
143  * @return FilesystemAdapter
144  */
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);
149  }
150  return $this->caches[$section];
151  }
152 
153  private function sanitizeSection($section) {
154  return preg_replace('/[^-+_.A-Za-z0-9]/', '.', $section);
155  }
156 
157  private function sanitizeKey($key) {
158  return strlen($key) === 0 ? '.' : preg_replace('/[\{\}\(\)\/@\:]/', '.', $key);
159  }
160 }
161 ?>
exists($section, $key)
Definition: FileCache.php:54
FileCache provides simple caching functionality.
Definition: FileCache.php:26
put($section, $key, $value, $lifetime=null)
Definition: FileCache.php:83
setCacheDir($cacheDir)
Set the cache directory (defaults to WCMF_BASE/app/cache/ if not given).
Definition: FileCache.php:47
__construct($cacheDir=null)
Constructor.
Definition: FileCache.php:37
getDate($section, $key)
Definition: FileCache.php:62
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22
Cache defines the interface for cache implementations.
Definition: Cache.php:21