FileCache.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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 
16 
17 /**
18  * FileCache provides simple caching functionality.
19  *
20  * The cache is divided into different sections. All sections share
21  * the same base path.
22  *
23  * @author ingo herwig <ingo@wemove.com>
24  */
25 class FileCache implements Cache {
26 
27  private $_cacheDir = null;
28  private $_cache = null;
29  private $_fileUtil = null;
30 
31  /**
32  * Constructor
33  */
34  public function __construct() {
35  $this->_fileUtil = new FileUtil();
36  }
37 
38  /**
39  * Set the cache directory (defaults to session_save_path if not given).
40  * If not existing, the directory will be created relative to WCMF_BASE.
41  * @param $cacheDir String
42  */
43  public function setCacheDir($cacheDir) {
44  $this->_cacheDir = WCMF_BASE.$cacheDir;
45  }
46 
47  /**
48  * @see Cache::exists()
49  */
50  public function exists($section, $key) {
51  $this->initializeCache($section);
52  return isset($this->_cache[$section][$key]);
53  }
54 
55  /**
56  * @see Cache::get()
57  */
58  public function get($section, $key) {
59  $this->initializeCache($section);
60  if (isset($this->_cache[$section][$key])) {
61  return $this->_cache[$section][$key];
62  }
63  return null;
64  }
65 
66  /**
67  * @see Cache::put()
68  */
69  public function put($section, $key, $value) {
70  $this->initializeCache($section);
71  $this->_cache[$section][$key] = $value;
72  $this->saveCache($section);
73  }
74 
75  /**
76  * @see Cache::clear()
77  */
78  public function clear($section) {
79  if (preg_match('/\*$/', $section)) {
80  // handle wildcards
81  $cachBaseDir = $this->getCacheDir();
82  $directory = $cachBaseDir.dirname($section);
83  if (is_dir($directory)) {
84  $pattern = '/^'.preg_replace('/\*$/', '', basename($section)).'/';
85  $files = $this->_fileUtil->getFiles($directory, $pattern, true, true);
86  foreach ($files as $file) {
87  $this->clear(str_replace($cachBaseDir, '', $file));
88  }
89  $directories = $this->_fileUtil->getDirectories($directory, $pattern, true, true);
90  foreach ($directories as $directory) {
91  $this->clear(str_replace($cachBaseDir, '', $directory).'/*');
92  @rmdir($directory);
93  }
94  }
95  }
96  else {
97  $file = $this->getCacheFile($section);
98  @unlink($file);
99  unset($this->_cache[$section]);
100  }
101  }
102 
103  /**
104  * @see Cache::clearAll()
105  */
106  public function clearAll() {
107  $cacheDir = $this->getCacheDir();
108  if (is_dir($cacheDir)) {
109  $this->_fileUtil->emptyDir($cacheDir);
110  }
111  $this->_cache = null;
112  }
113 
114  /**
115  * Initialize the cache
116  * @param $section The caching section
117  */
118  private function initializeCache($section) {
119  if (!isset($this->_cache[$section])) {
120  $file = $this->getCacheFile($section);
121  if (file_exists($file)) {
122  $this->_cache[$section] = unserialize(file_get_contents($file));
123  }
124  else {
125  $this->_cache[$section] = array();
126  }
127  }
128  }
129 
130  /**
131  * Save the cache
132  * @param $section The caching section
133  */
134  private function saveCache($section) {
135  $content = serialize($this->_cache[$section]);
136  $file = $this->getCacheFile($section);
137  $this->_fileUtil->mkdirRec(dirname($file));
138  $fh = fopen($file, "w");
139  if ($fh !== false) {
140  fwrite($fh, $content);
141  fclose($fh);
142  }
143  else {
144  throw new ConfigurationException("The cache path is not writable: ".$file);
145  }
146  }
147 
148  /**
149  * Get the cache root directory
150  * @return String
151  */
152  private function getCacheDir() {
153  if ($this->_cacheDir == null) {
154  $this->_cacheDir = session_save_path().DIRECTORY_SEPARATOR;
155  }
156  return $this->_cacheDir;
157  }
158 
159  /**
160  * Get the cache file for the specified cache
161  * @param $section The caching section
162  * @return String
163  */
164  private function getCacheFile($section) {
165  return $this->getCacheDir().$section;
166  }
167 }
168 ?>
exists($section, $key)
Definition: FileCache.php:50
setCacheDir($cacheDir)
Set the cache directory (defaults to session_save_path if not given).
Definition: FileCache.php:43
FileCache provides simple caching functionality.
Definition: FileCache.php:25
__construct()
Constructor.
Definition: FileCache.php:34
Cache defines the interface for cache implementations.
Definition: Cache.php:21
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22
put($section, $key, $value)
Definition: FileCache.php:69