19 * @license GNU General Public License |
19 * @license GNU General Public License |
20 */ |
20 */ |
21 |
21 |
22 class CacheManager |
22 class CacheManager |
23 { |
23 { |
24 /** |
24 /** |
25 * Fetch a cached piece of data. |
25 * Fetch a cached piece of data. |
26 * @param string Cache ID. The timestamp is checked automatically. |
26 * @param string Cache ID. The timestamp is checked automatically. |
27 */ |
27 */ |
28 |
28 |
29 public function fetch($cache_id) |
29 public function fetch($cache_id) |
30 { |
30 { |
31 if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) ) |
31 if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) ) |
32 { |
32 { |
33 throw new Exception('Cache ID must be letters, numbers, and underscores.'); |
33 throw new Exception('Cache ID must be letters, numbers, and underscores.'); |
34 } |
34 } |
35 $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php"; |
35 $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php"; |
36 if ( file_exists($cache_file) ) |
36 if ( file_exists($cache_file) ) |
37 { |
37 { |
38 require($cache_file); |
38 require($cache_file); |
39 if ( isset($_cache_data) && isset($_cache_ttl) && isset($_cache_timestamp) ) |
39 if ( isset($_cache_data) && isset($_cache_ttl) && isset($_cache_timestamp) ) |
40 { |
40 { |
41 $cache_expire = $_cache_timestamp + ( 60 * $_cache_ttl); |
41 $cache_expire = $_cache_timestamp + ( 60 * $_cache_ttl); |
42 if ( $cache_expire >= time() || $_cache_ttl === -1 ) |
42 if ( $cache_expire >= time() || $_cache_ttl === -1 ) |
43 { |
43 { |
44 return $_cache_data; |
44 return $_cache_data; |
45 } |
45 } |
46 } |
46 } |
47 } |
47 } |
48 return false; |
48 return false; |
49 } |
49 } |
50 |
50 |
51 /** |
51 /** |
52 * Stores an array by var_export()ing it and saving it to disk. |
52 * Stores an array by var_export()ing it and saving it to disk. |
53 * @param string Cache ID - human readable name for this store (letters, numbers, underscores) |
53 * @param string Cache ID - human readable name for this store (letters, numbers, underscores) |
54 * @param array Data to store |
54 * @param array Data to store |
55 * @param int TTL for the cached array, in minutes. Defaults to 20. If set to -1, caches indefinitely. |
55 * @param int TTL for the cached array, in minutes. Defaults to 20. If set to -1, caches indefinitely. |
56 * @return bool True on success, false on failure |
56 * @return bool True on success, false on failure |
57 */ |
57 */ |
58 |
58 |
59 public function store($cache_id, $data, $ttl = 20) |
59 public function store($cache_id, $data, $ttl = 20) |
60 { |
60 { |
61 if ( getConfig('cache_thumbs') != '1' ) |
61 if ( getConfig('cache_thumbs') != '1' ) |
62 { |
62 { |
63 // caching disabled |
63 // caching disabled |
64 return false; |
64 return false; |
65 } |
65 } |
66 if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) ) |
66 if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) ) |
67 { |
67 { |
68 throw new Exception('Cache ID must be letters, numbers, and underscores.'); |
68 throw new Exception('Cache ID must be letters, numbers, and underscores.'); |
69 } |
69 } |
70 if ( !is_int($ttl) ) |
70 if ( !is_int($ttl) ) |
71 { |
71 { |
72 throw new Exception('TTL must be an integer'); |
72 throw new Exception('TTL must be an integer'); |
73 } |
73 } |
74 |
74 |
75 $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php"; |
75 $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php"; |
76 if ( file_exists($cache_file) ) |
76 if ( file_exists($cache_file) ) |
77 { |
77 { |
78 @unlink($cache_file); |
78 @unlink($cache_file); |
79 } |
79 } |
80 $fh = @fopen($cache_file, 'w'); |
80 $fh = @fopen($cache_file, 'w'); |
81 if ( !$fh ) |
81 if ( !$fh ) |
82 { |
82 { |
83 throw new Exception('Failed to open file for writing.'); |
83 throw new Exception('Failed to open file for writing.'); |
84 } |
84 } |
85 $exported = Language::var_export_string($data); |
85 $exported = Language::var_export_string($data); |
86 $now = time(); |
86 $now = time(); |
87 $content = <<<EOF |
87 $content = <<<EOF |
88 <?php |
88 <?php |
89 /** |
89 /** |
90 * Automatically generated cache file. |
90 * Automatically generated cache file. |
91 * Do not edit this file as it will expire and be overwritten $ttl minutes from its creation. |
91 * Do not edit this file as it will expire and be overwritten $ttl minutes from its creation. |
92 */ |
92 */ |
94 \$_cache_ttl = $ttl; |
94 \$_cache_ttl = $ttl; |
95 \$_cache_timestamp = $now; |
95 \$_cache_timestamp = $now; |
96 \$_cache_data = $exported; |
96 \$_cache_data = $exported; |
97 |
97 |
98 EOF; |
98 EOF; |
99 fwrite($fh, $content); |
99 fwrite($fh, $content); |
100 fclose($fh); |
100 fclose($fh); |
101 |
101 |
102 return true; |
102 return true; |
103 } |
103 } |
104 |
104 |
105 /** |
105 /** |
106 * Deletes a cached item. |
106 * Deletes a cached item. |
107 * @param string Cache ID. |
107 * @param string Cache ID. |
108 * @return bool true on success or if item doesn't exist, false on failure |
108 * @return bool true on success or if item doesn't exist, false on failure |
109 */ |
109 */ |
110 |
110 |
111 public function purge($cache_id) |
111 public function purge($cache_id) |
112 { |
112 { |
113 if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) ) |
113 if ( !preg_match('/^[a-z0-9_]+$/', $cache_id) ) |
114 { |
114 { |
115 throw new Exception('Cache ID must be letters, numbers, and underscores.'); |
115 throw new Exception('Cache ID must be letters, numbers, and underscores.'); |
116 } |
116 } |
117 |
117 |
118 $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php"; |
118 $cache_file = ENANO_ROOT . "/cache/cache_$cache_id.php"; |
119 if ( file_exists($cache_file) ) |
119 if ( file_exists($cache_file) ) |
120 { |
120 { |
121 if ( unlink($cache_file) ) |
121 if ( unlink($cache_file) ) |
122 { |
122 { |
123 return true; |
123 return true; |
124 } |
124 } |
125 return false; |
125 return false; |
126 } |
126 } |
127 return true; |
127 return true; |
128 } |
128 } |
129 } |
129 } |
130 |
130 |