Elgg  Version 1.11
ElggFileCache.php
Go to the documentation of this file.
1 <?php
9 class ElggFileCache extends \ElggCache {
19  public function __construct($cache_path, $max_age = 0, $max_size = 0) {
20  $this->setVariable("cache_path", $cache_path);
21  $this->setVariable("max_age", $max_age);
22  $this->setVariable("max_size", $max_size);
23 
24  if ($cache_path == "") {
25  throw new \ConfigurationException("Cache path set to nothing!");
26  }
27  }
28 
29  // @codingStandardsIgnoreStart
40  protected function create_file($filename, $rw = "rb") {
41  elgg_deprecated_notice('\ElggFileCache::create_file() is deprecated by ::createFile()', 1.8);
42 
43  return $this->createFile($filename, $rw);
44  }
45  // @codingStandardsIgnoreEnd
46 
55  protected function createFile($filename, $rw = "rb") {
56  // Create a filename matrix
57  $matrix = "";
58  $depth = strlen($filename);
59  if ($depth > 5) {
60  $depth = 5;
61  }
62 
63  // Create full path
64  $path = $this->getVariable("cache_path") . $matrix;
65  if (!is_dir($path)) {
66  mkdir($path, 0700, true);
67  }
68 
69  // Open the file
70  if ((!file_exists($path . $filename)) && ($rw == "rb")) {
71  return false;
72  }
73 
74  return fopen($path . $filename, $rw);
75  }
76 
77  // @codingStandardsIgnoreStart
87  protected function sanitise_filename($filename) {
88  // @todo : Writeme
89 
90  return $filename;
91  }
92  // @codingStandardsIgnoreEnd
93 
101  protected function sanitizeFilename($filename) {
102  // @todo : Writeme
103 
104  return $filename;
105  }
106 
115  public function save($key, $data) {
116  $f = $this->createFile($this->sanitizeFilename($key), "wb");
117  if ($f) {
118  $result = fwrite($f, $data);
119  fclose($f);
120 
121  return $result;
122  }
123 
124  return false;
125  }
126 
136  public function load($key, $offset = 0, $limit = null) {
137  $f = $this->createFile($this->sanitizeFilename($key));
138  if ($f) {
139  if (!$limit) {
140  $limit = -1;
141  }
142 
143  $data = stream_get_contents($f, $limit, $offset);
144 
145  fclose($f);
146 
147  return $data;
148  }
149 
150  return false;
151  }
152 
160  public function delete($key) {
161  $dir = $this->getVariable("cache_path");
162 
163  if (file_exists($dir . $key)) {
164  return unlink($dir . $key);
165  }
166  return true;
167  }
168 
174  public function clear() {
175  $dir = $this->getVariable("cache_path");
176 
177  $exclude = array(".", "..");
178 
179  $files = scandir($dir);
180  if (!$files) {
181  return;
182  }
183 
184  foreach ($files as $f) {
185  if (!in_array($f, $exclude)) {
186  unlink($dir . $f);
187  }
188  }
189  }
190 
196  public function __destruct() {
197  // @todo Check size and age, clean up accordingly
198  $size = 0;
199  $dir = $this->getVariable("cache_path");
200 
201  // Short circuit if both size and age are unlimited
202  if (($this->getVariable("max_age") == 0) && ($this->getVariable("max_size") == 0)) {
203  return;
204  }
205 
206  $exclude = array(".", "..");
207 
208  $files = scandir($dir);
209  if (!$files) {
210  throw new \IOException($dir . " is not a directory.");
211  }
212 
213  // Perform cleanup
214  foreach ($files as $f) {
215  if (!in_array($f, $exclude)) {
216  $stat = stat($dir . $f);
217 
218  // Add size
219  $size .= $stat['size'];
220 
221  // Is this older than my maximum date?
222  if (($this->getVariable("max_age") > 0) && (time() - $stat['mtime'] > $this->getVariable("max_age"))) {
223  unlink($dir . $f);
224  }
225 
226  // @todo Size
227  }
228  }
229  }
230 }
create_file($filename, $rw="rb")
Create and return a handle to a file.
createFile($filename, $rw="rb")
Create and return a handle to a file.
sanitise_filename($filename)
Create a sanitised filename for the file.
$size
Definition: view.php:10
__destruct()
Preform cleanup and invalidates cache upon object destruction.
$data
Definition: opendd.php:13
$files
Definition: crop.php:36
if(!$count) $offset
Definition: pagination.php:25
$limit
Definition: userpicker.php:31
$key
Definition: summary.php:34
load($key, $offset=0, $limit=null)
Load a key.
elgg_deprecated_notice($msg, $dep_version, $backtrace_level=1)
Log a notice about deprecated use of a function, view, etc.
Definition: elgglib.php:1006
save($key, $data)
Save a key.
__construct($cache_path, $max_age=0, $max_size=0)
Set the Elgg cache.
clear()
Delete all files in the directory of this file cache.
$filename
Definition: crop.php:23
sanitizeFilename($filename)
Create a sanitised filename for the file.
setVariable($variable, $value)
Set a cache variable.
Definition: ElggCache.php:49
$path
Definition: invalid.php:17
getVariable($variable)
Get variables for this cache.
Definition: ElggCache.php:80