|
| 1 | +<?php |
| 2 | +namespace Transvision; |
| 3 | + |
| 4 | +/** |
| 5 | + * Cache class |
| 6 | + * |
| 7 | + * A simple and fast file caching system. |
| 8 | + * |
| 9 | + * 3 global constants are used: CACHE_ENABLED, CACHE_PATH and CACHE_TIME |
| 10 | + * If those app constants are not available, the system temp folder |
| 11 | + * and the class constants CACHE_ENABLED and CACHE_TIME are used. |
| 12 | + * |
| 13 | + * @package Transvision |
| 14 | + */ |
| 15 | +class Cache |
| 16 | +{ |
| 17 | + /** Fallback for activation of Cache */ |
| 18 | + const CACHE_ENABLED = true; |
| 19 | + |
| 20 | + /** Duration of the cache */ |
| 21 | + const CACHE_TIME = 3600; |
| 22 | + |
| 23 | + /** |
| 24 | + * Create a cache file with serialized data |
| 25 | + * |
| 26 | + * We use PHP serialization and not json for example as it allows |
| 27 | + * storing not only data but also data representations and |
| 28 | + * instantiated objects. |
| 29 | + * |
| 30 | + * @param string $id |
| 31 | + * @param object $data |
| 32 | + * @return boolean True if cache file is created. False if there was an error. |
| 33 | + */ |
| 34 | + public static function setKey($id, $data) |
| 35 | + { |
| 36 | + if (! self::isActivated()) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + |
| 40 | + return file_put_contents(self::getKeyPath($id), serialize($data)) ? true : false; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get the cached serialized data via its UID |
| 45 | + * |
| 46 | + * @param string $id UID of the cache |
| 47 | + * @param int $ttl Number of seconds for time to live. Default to 0. |
| 48 | + * @return object Unserialized cached data. Or False |
| 49 | + */ |
| 50 | + public static function getKey($id, $ttl = 0) |
| 51 | + { |
| 52 | + if (! self::isActivated()) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + if ($ttl == 0) { |
| 57 | + $ttl = defined('CACHE_TIME') ? CACHE_TIME : self::CACHE_TIME; |
| 58 | + } |
| 59 | + |
| 60 | + return self::isValidKey($id, $ttl) |
| 61 | + ? unserialize(file_get_contents(self::getKeyPath($id))) |
| 62 | + : false; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Flush our cache |
| 67 | + * |
| 68 | + * @return boolean True if files in cache are deleted, False if some files were not deleted |
| 69 | + */ |
| 70 | + public static function flush() |
| 71 | + { |
| 72 | + $files = glob(self::getCachePath() . '*.cache'); |
| 73 | + |
| 74 | + return ! in_array(false, array_map('unlink', $files)); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Is the caching system activated? |
| 79 | + * We look at the existence of a CACHE constant and if it's at True |
| 80 | + * |
| 81 | + * @return boolean True if activated, False if deactivated |
| 82 | + */ |
| 83 | + private static function isActivated() |
| 84 | + { |
| 85 | + return defined('CACHE_ENABLED') ? CACHE_ENABLED : self::CACHE_ENABLED; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Check if cached data for a key is usable |
| 90 | + * |
| 91 | + * @param string $id UID for the data |
| 92 | + * @param int $ttl Number of seconds for time to live |
| 93 | + * @return boolean if valid data, false if no usable cached data |
| 94 | + */ |
| 95 | + private static function isValidKey($id, $ttl) |
| 96 | + { |
| 97 | + // No cache file |
| 98 | + if (! file_exists(self::getKeyPath($id))) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + // Cache is obsolete and was deleted |
| 103 | + if (self::isObsoleteKey($id, $ttl)) { |
| 104 | + self::deleteKey($id); |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + // All good, cache is valid |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Delete a cache file thanks to its UID |
| 114 | + * |
| 115 | + * @param string $id UID of the cached data |
| 116 | + * @return boolean True if data was deleted, false if it doesn't exist |
| 117 | + */ |
| 118 | + private static function deleteKey($id) |
| 119 | + { |
| 120 | + $file = self::getKeyPath($id); |
| 121 | + |
| 122 | + if (! file_exists($file)) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + unlink($file); |
| 127 | + clearstatcache(true, $file); |
| 128 | + |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Get the path to the cached file |
| 134 | + * |
| 135 | + * File is of the form a840d513be5240045ccc979208f739a168946332.cache |
| 136 | + * |
| 137 | + * @param string $id UID of the cached file |
| 138 | + * @return string path for the file |
| 139 | + */ |
| 140 | + public static function getKeyPath($id) |
| 141 | + { |
| 142 | + return self::getCachePath() . sha1($id) . '.cache'; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get the path to the cache folder |
| 147 | + * |
| 148 | + * If a CACHE_PATH global constant is defined, use it, |
| 149 | + * otherwise write to OS folder for temporary files. |
| 150 | + * |
| 151 | + * @return string path to Cache |
| 152 | + */ |
| 153 | + private static function getCachePath() |
| 154 | + { |
| 155 | + return defined('CACHE_PATH') ? CACHE_PATH : sys_get_temp_dir() . '/'; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Check if the data has not expired |
| 160 | + * @param string $id UID of the cached file |
| 161 | + * @param int $ttl Number of seconds for time to live |
| 162 | + * @return boolean True if file is obsolete, False if it is still fresh |
| 163 | + */ |
| 164 | + private static function isObsoleteKey($id, $ttl) |
| 165 | + { |
| 166 | + return filemtime(self::getKeyPath($id)) < (time() - $ttl); |
| 167 | + } |
| 168 | +} |
0 commit comments