2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * The library file for the file cache store.
20 * This file is part of the file cache store, it contains the API for interacting with an instance of the store.
21 * This is used as a default cache store within the Cache API. It should never be deleted.
23 * @package cachestore_file
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 * The file store class.
32 * Configuration options
33 * path: string: path to the cache directory, if left empty one will be created in the cache directory
34 * autocreate: true, false
35 * prescan: true, false
37 * @copyright 2012 Sam Hemelryk
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class cachestore_file extends cache_store implements cache_is_key_aware, cache_is_configurable, cache_is_searchable {
43 * The name of the store.
49 * The path used to store files for this store and the definition it was initialised with.
52 protected $path = false;
55 * The path in which definition specific sub directories will be created for caching.
58 protected $filestorepath = false;
61 * Set to true when a prescan has been performed.
64 protected $prescan = false;
67 * Set to true if we should store files within a single directory.
68 * By default we use a nested structure in order to reduce the chance of conflicts and avoid any file system
69 * limitations such as maximum files per directory.
72 protected $singledirectory = false;
75 * Set to true when the path should be automatically created if it does not yet exist.
78 protected $autocreate = false;
81 * Set to true if a custom path is being used.
84 protected $custompath = false;
87 * An array of keys we are sure about presently.
90 protected $keys = array();
93 * True when the store is ready to be initialised.
96 protected $isready = false;
99 * The cache definition this instance has been initialised with.
100 * @var cache_definition
102 protected $definition;
105 * A reference to the global $CFG object.
107 * You may be asking yourself why on earth this is here, but there is a good reason.
108 * By holding onto a reference of the $CFG object we can be absolutely sure that it won't be destroyed before
109 * we are done with it.
110 * This makes it possible to use a cache within a destructor method for the purposes of
111 * delayed writes. Like how the session mechanisms work.
118 * Constructs the store instance.
120 * Noting that this function is not an initialisation. It is used to prepare the store for use.
121 * The store will be initialised when required and will be provided with a cache_definition at that time.
123 * @param string $name
124 * @param array $configuration
126 public function __construct($name, array $configuration = array()) {
130 // Hold onto a reference of the global $CFG object.
135 if (array_key_exists('path', $configuration) && $configuration['path'] !== '') {
136 $this->custompath = true;
137 $this->autocreate = !empty($configuration['autocreate']);
138 $path = (string)$configuration['path'];
139 if (!is_dir($path)) {
140 if ($this->autocreate) {
141 if (!make_writable_directory($path, false)) {
143 debugging('Error trying to autocreate file store path. '.$path, DEBUG_DEVELOPER);
147 debugging('The given file cache store path does not exist. '.$path, DEBUG_DEVELOPER);
150 if ($path !== false && !is_writable($path)) {
152 debugging('The file cache store path is not writable for `'.$name.'`', DEBUG_DEVELOPER);
155 $path = make_cache_directory('cachestore_file/'.preg_replace('#[^a-zA-Z0-9\.\-_]+#', '', $name));
157 $this->isready = $path !== false;
158 $this->filestorepath = $path;
159 // This will be updated once the store has been initialised for a definition.
162 // Check if we should prescan the directory.
163 if (array_key_exists('prescan', $configuration)) {
164 $this->prescan = (bool)$configuration['prescan'];
166 // Default is no, we should not prescan.
167 $this->prescan = false;
169 // Check if we should be storing in a single directory.
170 if (array_key_exists('singledirectory', $configuration)) {
171 $this->singledirectory = (bool)$configuration['singledirectory'];
173 // Default: No, we will use multiple directories.
174 $this->singledirectory = false;
179 * Performs any necessary operation when the file store instance has been created.
181 public function instance_created() {
182 if ($this->isready && !$this->prescan) {
183 // It is supposed the store instance to expect an empty folder.
184 $this->purge_all_definitions();
189 * Returns true if this store instance is ready to be used.
192 public function is_ready() {
193 return $this->isready;
197 * Returns true once this instance has been initialised.
201 public function is_initialised() {
206 * Returns the supported features as a combined int.
208 * @param array $configuration
211 public static function get_supported_features(array $configuration = array()) {
212 $supported = self::SUPPORTS_DATA_GUARANTEE +
213 self::SUPPORTS_NATIVE_TTL +
219 * Returns false as this store does not support multiple identifiers.
220 * (This optional function is a performance optimisation; it must be
221 * consistent with the value from get_supported_features.)
225 public function supports_multiple_identifiers() {
230 * Returns the supported modes as a combined int.
232 * @param array $configuration
235 public static function get_supported_modes(array $configuration = array()) {
236 return self::MODE_APPLICATION + self::MODE_SESSION;
240 * Returns true if the store requirements are met.
244 public static function are_requirements_met() {
249 * Returns true if the given mode is supported by this store.
251 * @param int $mode One of cache_store::MODE_*
254 public static function is_supported_mode($mode) {
255 return ($mode === self::MODE_APPLICATION || $mode === self::MODE_SESSION);
259 * Initialises the cache.
261 * Once this has been done the cache is all set to be used.
263 * @param cache_definition $definition
265 public function initialise(cache_definition $definition) {
266 $this->definition = $definition;
267 $hash = preg_replace('#[^a-zA-Z0-9]+#', '_', $this->definition->get_id());
268 $this->path = $this->filestorepath.'/'.$hash;
269 make_writable_directory($this->path, false);
270 if ($this->prescan && $definition->get_mode() !== self::MODE_REQUEST) {
271 $this->prescan = false;
273 if ($this->prescan) {
274 $this->prescan_keys();
279 * Pre-scan the cache to see which keys are present.
281 protected function prescan_keys() {
282 $files = glob($this->glob_keys_pattern(), GLOB_MARK | GLOB_NOSORT);
283 if (is_array($files)) {
284 foreach ($files as $filename) {
285 $this->keys[basename($filename)] = filemtime($filename);
291 * Gets a pattern suitable for use with glob to find all keys in the cache.
293 * @param string $prefix A prefix to use.
294 * @return string The pattern.
296 protected function glob_keys_pattern($prefix = '') {
297 if ($this->singledirectory) {
298 return $this->path . '/'.$prefix.'*.cache';
300 return $this->path . '/*/'.$prefix.'*.cache';
305 * Returns the file path to use for the given key.
307 * @param string $key The key to generate a file path for.
308 * @param bool $create If set to the true the directory structure the key requires will be created.
309 * @return string The full path to the file that stores a particular cache key.
311 protected function file_path_for_key($key, $create = false) {
312 if ($this->singledirectory) {
313 // Its a single directory, easy, just the store instances path + the file name.
314 return $this->path . '/' . $key . '.cache';
316 // We are using a single subdirectory to achieve 1 level.
317 // We suffix the subdir so it does not clash with any windows
318 // reserved filenames like 'con'.
319 $subdir = substr($key, 0, 3) . '-cache';
320 $dir = $this->path . '/' . $subdir;
322 // Create the directory. This function does it recursivily!
323 make_writable_directory($dir, false);
325 return $dir . '/' . $key . '.cache';
330 * Retrieves an item from the cache store given its key.
332 * @param string $key The key to retrieve
333 * @return mixed The data that was associated with the key, or false if the key did not exist.
335 public function get($key) {
336 $filename = $key.'.cache';
337 $file = $this->file_path_for_key($key);
338 $ttl = $this->definition->get_ttl();
341 $maxtime = cache::now() - $ttl;
344 if ($this->prescan && array_key_exists($key, $this->keys)) {
345 if (!$ttl || $this->keys[$filename] >= $maxtime && file_exists($file)) {
350 } else if (file_exists($file) && (!$ttl || filemtime($file) >= $maxtime)) {
356 // Check the filesize first, likely not needed but important none the less.
357 $filesize = filesize($file);
361 // Open ensuring the file for writing, truncating it and setting the pointer to the start.
362 if (!$handle = fopen($file, 'rb')) {
366 // We don't care if this succeeds or not, on some systems it will, on some it won't, meah either way.
367 flock($handle, LOCK_SH);
369 // There is a problem when reading from the file during PHPUNIT tests. For one reason or another the filesize is not correct
370 // Doesn't happen during normal operation, just during unit tests.
372 $data = fread($handle, $filesize+128);
374 flock($handle, LOCK_UN);
375 // Return it unserialised.
376 return $this->prep_data_after_read($data);
380 * Retrieves several items from the cache store in a single transaction.
382 * If not all of the items are available in the cache then the data value for those that are missing will be set to false.
384 * @param array $keys The array of keys to retrieve
385 * @return array An array of items from the cache. There will be an item for each key, those that were not in the store will
388 public function get_many($keys) {
390 foreach ($keys as $key) {
391 $result[$key] = $this->get($key);
397 * Deletes an item from the cache store.
399 * @param string $key The key to delete.
400 * @return bool Returns true if the operation was a success, false otherwise.
402 public function delete($key) {
403 $filename = $key.'.cache';
404 $file = $this->file_path_for_key($key);
405 if (@unlink($file)) {
406 unset($this->keys[$filename]);
414 * Deletes several keys from the cache in a single action.
416 * @param array $keys The keys to delete
417 * @return int The number of items successfully deleted.
419 public function delete_many(array $keys) {
421 foreach ($keys as $key) {
422 if ($this->delete($key)) {
430 * Sets an item in the cache given its key and data value.
432 * @param string $key The key to use.
433 * @param mixed $data The data to set.
434 * @return bool True if the operation was a success false otherwise.
436 public function set($key, $data) {
437 $this->ensure_path_exists();
438 $filename = $key.'.cache';
439 $file = $this->file_path_for_key($key, true);
440 $result = $this->write_file($file, $this->prep_data_before_save($data));
442 // Couldn't write the file.
445 // Record the key if required.
446 if ($this->prescan) {
447 $this->keys[$filename] = cache::now() + 1;
449 // Return true.. it all worked **miracles**.
454 * Prepares data to be stored in a file.
459 protected function prep_data_before_save($data) {
460 return serialize($data);
464 * Prepares the data it has been read from the cache. Undoing what was done in prep_data_before_save.
466 * @param string $data
468 * @throws coding_exception
470 protected function prep_data_after_read($data) {
471 $result = @unserialize($data);
472 if ($result === false) {
473 throw new coding_exception('Failed to unserialise data from file. Either failed to read, or failed to write.');
479 * Sets many items in the cache in a single transaction.
481 * @param array $keyvaluearray An array of key value pairs. Each item in the array will be an associative array with two
482 * keys, 'key' and 'value'.
483 * @return int The number of items successfully set. It is up to the developer to check this matches the number of items
484 * sent ... if they care that is.
486 public function set_many(array $keyvaluearray) {
488 foreach ($keyvaluearray as $pair) {
489 if ($this->set($pair['key'], $pair['value'])) {
497 * Checks if the store has a record for the given key and returns true if so.
502 public function has($key) {
503 $filename = $key.'.cache';
504 $maxtime = cache::now() - $this->definition->get_ttl();
505 if ($this->prescan) {
506 return array_key_exists($filename, $this->keys) && $this->keys[$filename] >= $maxtime;
508 $file = $this->file_path_for_key($key);
509 return (file_exists($file) && ($this->definition->get_ttl() == 0 || filemtime($file) >= $maxtime));
513 * Returns true if the store contains records for all of the given keys.
518 public function has_all(array $keys) {
519 foreach ($keys as $key) {
520 if (!$this->has($key)) {
528 * Returns true if the store contains records for any of the given keys.
533 public function has_any(array $keys) {
534 foreach ($keys as $key) {
535 if ($this->has($key)) {
543 * Purges the cache definition deleting all the items within it.
545 * @return boolean True on success. False otherwise.
547 public function purge() {
548 if ($this->isready) {
549 $files = glob($this->glob_keys_pattern(), GLOB_MARK | GLOB_NOSORT);
550 if (is_array($files)) {
551 foreach ($files as $filename) {
555 $this->keys = array();
561 * Purges all the cache definitions deleting all items within them.
563 * @return boolean True on success. False otherwise.
565 protected function purge_all_definitions() {
566 // Warning: limit the deletion to what file store is actually able
567 // to create using the internal {@link purge()} providing the
568 // {@link $path} with a wildcard to perform a purge action over all the definitions.
569 $currpath = $this->path;
570 $this->path = $this->filestorepath.'/*';
571 $result = $this->purge();
572 $this->path = $currpath;
577 * Given the data from the add instance form this function creates a configuration array.
579 * @param stdClass $data
582 public static function config_get_configuration_array($data) {
585 if (isset($data->path)) {
586 $config['path'] = $data->path;
588 if (isset($data->autocreate)) {
589 $config['autocreate'] = $data->autocreate;
591 if (isset($data->singledirectory)) {
592 $config['singledirectory'] = $data->singledirectory;
594 if (isset($data->prescan)) {
595 $config['prescan'] = $data->prescan;
602 * Allows the cache store to set its data against the edit form before it is shown to the user.
604 * @param moodleform $editform
605 * @param array $config
607 public static function config_set_edit_form_data(moodleform $editform, array $config) {
609 if (!empty($config['path'])) {
610 $data['path'] = $config['path'];
612 if (isset($config['autocreate'])) {
613 $data['autocreate'] = (bool)$config['autocreate'];
615 if (isset($config['singledirectory'])) {
616 $data['singledirectory'] = (bool)$config['singledirectory'];
618 if (isset($config['prescan'])) {
619 $data['prescan'] = (bool)$config['prescan'];
621 $editform->set_data($data);
625 * Checks to make sure that the path for the file cache exists.
628 * @throws coding_exception
630 protected function ensure_path_exists() {
632 if (!is_writable($this->path)) {
633 if ($this->custompath && !$this->autocreate) {
634 throw new coding_exception('File store path does not exist. It must exist and be writable by the web server.');
638 // This can only happen during destruction of objects.
639 // A cache is being used within a destructor, php is ending a request and $CFG has
640 // already being cleaned up.
641 // Rebuild $CFG with directory permissions just to complete this write.
645 if (!make_writable_directory($this->path, false)) {
646 throw new coding_exception('File store path does not exist and can not be created.');
649 // We re-created it so we'll clean it up.
657 * Performs any necessary clean up when the file store instance is being deleted.
659 * 1. Purges the cache directory.
660 * 2. Deletes the directory we created for the given definition.
662 public function instance_deleted() {
663 $this->purge_all_definitions();
664 @rmdir($this->filestorepath);
668 * Generates an instance of the cache store that can be used for testing.
670 * Returns an instance of the cache store, or false if one cannot be created.
672 * @param cache_definition $definition
673 * @return cachestore_file
675 public static function initialise_test_instance(cache_definition $definition) {
677 $path = make_cache_directory('cachestore_file_test');
678 $cache = new cachestore_file($name, array('path' => $path));
679 $cache->initialise($definition);
684 * Writes your madness to a file.
686 * There are several things going on in this function to try to ensure what we don't end up with partial writes etc.
687 * 1. Files for writing are opened with the mode xb, the file must be created and can not already exist.
688 * 2. Renaming, data is written to a temporary file, where it can be verified using md5 and is then renamed.
690 * @param string $file Absolute file path
691 * @param string $content The content to write.
694 protected function write_file($file, $content) {
695 // Generate a temp file that is going to be unique. We'll rename it at the end to the desired file name.
696 // in this way we avoid partial writes.
697 $path = dirname($file);
699 $tempfile = $path.'/'.uniqid(sesskey().'.', true) . '.temp';
700 if (!file_exists($tempfile)) {
705 // Open the file with mode=x. This acts to create and open the file for writing only.
706 // If the file already exists this will return false.
707 // We also force binary.
708 $handle = @fopen($tempfile, 'xb+');
709 if ($handle === false) {
710 // File already exists... lock already exists, return false.
713 fwrite($handle, $content);
715 // Close the handle, we're done.
718 if (md5_file($tempfile) !== md5($content)) {
719 // The md5 of the content of the file must match the md5 of the content given to be written.
724 // Finally rename the temp file to the desired file, returning the true|false result.
725 $result = rename($tempfile, $file);
726 @chmod($file, $this->cfg->filepermissions);
728 // Failed to rename, don't leave files lying around.
735 * Returns the name of this instance.
738 public function my_name() {
743 * Finds all of the keys being used by this cache store instance.
747 public function find_all() {
748 $this->ensure_path_exists();
749 $files = glob($this->glob_keys_pattern(), GLOB_MARK | GLOB_NOSORT);
751 if ($files === false) {
754 foreach ($files as $file) {
755 $return[] = substr(basename($file), 0, -6);
761 * Finds all of the keys whose keys start with the given prefix.
763 * @param string $prefix
765 public function find_by_prefix($prefix) {
766 $this->ensure_path_exists();
767 $prefix = preg_replace('#(\*|\?|\[)#', '[$1]', $prefix);
768 $files = glob($this->glob_keys_pattern($prefix), GLOB_MARK | GLOB_NOSORT);
770 if ($files === false) {
773 foreach ($files as $file) {
774 // Trim off ".cache" from the end.
775 $return[] = substr(basename($file), 0, -6);