1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file contains functions that deal with getting and setting cache values. |
5
|
|
|
* |
6
|
|
|
* @name ElkArte Forum |
7
|
|
|
* @copyright ElkArte Forum contributors |
8
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
9
|
|
|
* |
10
|
|
|
* @version 1.1 dev |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace ElkArte\sources\subs\CacheMethod; |
15
|
|
|
|
16
|
|
|
use FilesystemIterator; |
17
|
|
|
use UnexpectedValueException; |
18
|
|
|
|
19
|
1 |
|
if (!defined('ELK')) |
20
|
1 |
|
die('No access...'); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Filebased caching is the fallback if nothing else is available, it simply |
24
|
|
|
* uses the filesystem to store queries results in order to try to reduce the |
25
|
|
|
* number of queries per time period. |
26
|
|
|
* |
27
|
|
|
* The performance gain may or may not exist depending on many factors. |
28
|
|
|
* |
29
|
|
|
* It requires the CACHEDIR constant to be defined and pointing to a |
30
|
|
|
* writable directory. |
31
|
|
|
*/ |
32
|
|
|
class Filebased extends Cache_Method_Abstract |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc } |
36
|
|
|
*/ |
37
|
|
|
public function init() |
38
|
|
|
{ |
39
|
|
|
return @is_dir(CACHEDIR) && @is_writable(CACHEDIR); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc } |
44
|
|
|
*/ |
45
|
1 |
|
public function put($key, $value, $ttl = 120) |
46
|
|
|
{ |
47
|
|
|
// Clearing this data |
48
|
1 |
|
if ($value === null) |
49
|
1 |
|
@unlink(CACHEDIR . '/data_' . $key . '.php'); |
|
|
|
|
50
|
|
|
// Or stashing it away |
51
|
|
|
else |
52
|
|
|
{ |
53
|
1 |
|
$cache_data = '<?php if (!defined(\'ELK\')) die; if (' . (time() + $ttl) . ' < time()) return false; else{return \'' . addcslashes($value, '\\\'') . '\';}'; |
54
|
|
|
|
55
|
|
|
// Write out the cache file, check that the cache write was successful; all the data must be written |
56
|
|
|
// If it fails due to low diskspace, or other, remove the cache file |
57
|
1 |
|
if (@file_put_contents(CACHEDIR . '/data_' . $key . '.php', $cache_data, LOCK_EX) !== strlen($cache_data)) |
58
|
1 |
|
@unlink(CACHEDIR . '/data_' . $key . '.php'); |
|
|
|
|
59
|
|
|
} |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc } |
64
|
|
|
*/ |
65
|
1 |
|
public function get($key, $ttl = 120) |
66
|
|
|
{ |
67
|
|
|
// Otherwise it's ElkArte data! |
68
|
1 |
|
if (file_exists(CACHEDIR . '/data_' . $key . '.php') && filesize(CACHEDIR . '/data_' . $key . '.php') > 10) |
69
|
1 |
|
{ |
70
|
|
|
// php will cache file_exists et all, we can't 100% depend on its results so proceed with caution |
71
|
1 |
|
$value = @include(CACHEDIR . '/data_' . $key . '.php'); |
72
|
1 |
|
if ($value === false) |
73
|
1 |
|
{ |
74
|
|
|
@unlink(CACHEDIR . '/data_' . $key . '.php'); |
|
|
|
|
75
|
|
|
$return = null; |
76
|
|
|
} |
77
|
|
|
else |
78
|
1 |
|
$return = $value; |
79
|
|
|
|
80
|
1 |
|
unset($value); |
81
|
|
|
|
82
|
1 |
|
$this->is_miss = $return === null; |
83
|
|
|
|
84
|
1 |
|
return $return; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->is_miss = true; |
88
|
|
|
|
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc } |
94
|
|
|
*/ |
95
|
|
|
public function clean($type = '') |
96
|
|
|
{ |
97
|
|
|
// To be complete, we also clear out the cache dir so we get any js/css hive files |
98
|
|
|
// Remove the cache files in our disk cache directory |
99
|
|
|
try |
100
|
|
|
{ |
101
|
|
|
$files = new FilesystemIterator(CACHEDIR, FilesystemIterator::SKIP_DOTS); |
102
|
|
|
|
103
|
|
|
foreach ($files as $file) |
104
|
|
|
{ |
105
|
|
|
if ($file !== 'index.php' && $file !== '.htaccess' && (!$type || $file->getExtension() == $type)) |
106
|
|
|
@unlink($file->getPathname()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
catch (UnexpectedValueException $e) |
110
|
|
|
{ |
111
|
|
|
// @todo |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc } |
117
|
|
|
*/ |
118
|
|
|
public function fixkey($key) |
119
|
|
|
{ |
120
|
|
|
return strtr($key, ':/', '-_'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc } |
125
|
|
|
*/ |
126
|
|
|
public static function available() |
127
|
|
|
{ |
128
|
|
|
return @is_dir(CACHEDIR) && @is_writable(CACHEDIR); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc } |
133
|
|
|
*/ |
134
|
|
|
public static function details() |
135
|
|
|
{ |
136
|
|
|
return array('title' => self::title(), 'version' => 'N/A'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc } |
141
|
|
|
*/ |
142
|
|
|
public static function title() |
143
|
|
|
{ |
144
|
|
|
if (self::available()) |
145
|
|
|
add_integration_function('integrate_modify_cache_settings', 'Filebased_Cache::settings', '', false); |
146
|
|
|
|
147
|
|
|
return 'File-based caching'; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Adds the settings to the settings page. |
152
|
|
|
* |
153
|
|
|
* Used by integrate_modify_cache_settings added in the title method |
154
|
|
|
*/ |
155
|
|
|
public static function settings(&$config_vars) |
156
|
|
|
{ |
157
|
|
|
global $txt; |
158
|
|
|
|
159
|
|
|
$config_vars[] = array('cachedir', $txt['cachedir'], 'file', 'text', 36, 'cache_cachedir', 'force_div_id' => 'filebased_cachedir'); |
160
|
|
|
} |
161
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.