Cache.php
5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
namespace AIOSEO\Plugin\Common\Utils;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles our cache.
*
* @since 4.1.5
*/
class Cache {
/**
* Our cache table.
*
* @since 4.1.5
*
* @var string
*/
private $table = 'aioseo_cache';
/**
* Our cached cache.
*
* @since 4.1.5
*
* @var array
*/
private static $cache = [];
/**
* The Cache Prune class.
*
* @since 4.1.5
*
* @var CachePrune
*/
public $prune;
/**
* Prefix for this cache.
*
* @since 4.1.5
*
* @var string
*/
protected $prefix = '';
/**
* Returns the cache value for a key if it exists and is not expired.
*
* @since 4.1.5
*
* @param string $key The cache key name. Use a '%' for a like query.
* @return mixed The value or null if the cache does not exist.
*/
public function get( $key ) {
$key = $this->prepareKey( $key );
if ( isset( self::$cache[ $key ] ) ) {
return self::$cache[ $key ];
}
// Are we searching for a group of keys?
$isLikeGet = preg_match( '/%/', $key );
$result = aioseo()->db
->start( $this->table )
->select( '`key`, `value`' )
->whereRaw( '( `expiration` IS NULL OR `expiration` > \'' . aioseo()->helpers->timeToMysql( time() ) . '\' )' );
$isLikeGet ?
$result->whereRaw( '`key` LIKE \'' . $key . '\'' ) :
$result->where( 'key', $key );
$result->output( ARRAY_A )->run();
// If we have nothing in the cache let's return a hard null.
$values = $result->nullSet() ? null : $result->result();
// If we have something let's normalize it.
if ( $values ) {
foreach ( $values as &$value ) {
$value['value'] = maybe_unserialize( $value['value'] );
}
// Return only the single cache value.
if ( ! $isLikeGet ) {
$values = $values[0]['value'];
}
}
// Return values without a static cache.
// This is here because clearing the like cache is not simple.
if ( $isLikeGet ) {
return $values;
}
self::$cache[ $key ] = $values;
return self::$cache[ $key ];
}
/**
* Updates the given cache or creates it if it doesn't exist.
*
* @since 4.1.5
*
* @param string $key The cache key name.
* @param mixed $value The value.
* @param int $expiration The expiration time in seconds. Defaults to 24 hours. 0 to no expiration.
* @return void
*/
public function update( $key, $value, $expiration = DAY_IN_SECONDS ) {
// If the value is null we'll convert it and give it a shorter expiration.
if ( null === $value ) {
$value = false;
$expiration = 10 * MINUTE_IN_SECONDS;
}
$value = serialize( $value );
$expiration = 0 < $expiration ? aioseo()->helpers->timeToMysql( time() + $expiration ) : null;
aioseo()->db->insert( $this->table )
->set( [
'key' => $this->prepareKey( $key ),
'value' => $value,
'expiration' => $expiration,
'created' => aioseo()->helpers->timeToMysql( time() ),
'updated' => aioseo()->helpers->timeToMysql( time() )
] )
->onDuplicate( [
'value' => $value,
'expiration' => $expiration,
'updated' => aioseo()->helpers->timeToMysql( time() )
] )
->run();
$this->clearStatic( $key );
}
/**
* Deletes the given cache key.
*
* @since 4.1.5
*
* @param string $key The cache key.
* @return void
*/
public function delete( $key ) {
$key = $this->prepareKey( $key );
aioseo()->db->delete( $this->table )
->where( 'key', $key )
->run();
$this->clearStatic( $key );
}
/**
* Prepares the key before using the cache.
*
* @since 4.1.5
*
* @param string $key The key to prepare.
* @return string The prepared key.
*/
private function prepareKey( $key ) {
$key = trim( $key );
$key = $this->prefix && 0 !== strpos( $key, $this->prefix ) ? $this->prefix . $key : $key;
if ( aioseo()->helpers->isDev() && 80 < mb_strlen( $key, 'UTF-8' ) ) {
throw new \Exception( 'You are using a cache key that is too large, shorten your key and try again: [' . $key . ']' );
die;
}
return $key;
}
/**
* Clears all of our cache.
*
* @since 4.1.5
*
* @return void
*/
public function clear() {
if ( $this->prefix ) {
$this->clearPrefix( '' );
return;
}
aioseo()->db->truncate( $this->table )->run();
$this->clearStatic();
}
/**
* Clears all of our cache under a certain prefix.
*
* @since 4.1.5
*
* @param string $prefix A prefix to clear or empty to clear everything.
* @return void
*/
public function clearPrefix( $prefix ) {
$prefix = $this->prepareKey( $prefix );
aioseo()->db->delete( $this->table )
->whereRaw( "`key` LIKE '$prefix%'" )
->run();
$this->clearStaticPrefix( $prefix );
}
/**
* Clears all of our static in-memory cache of a prefix.
*
* @since 4.1.5
*
* @param string $prefix A prefix to clear.
* @return void
*/
private function clearStaticPrefix( $prefix ) {
$prefix = $this->prepareKey( $prefix );
foreach ( array_keys( self::$cache ) as $key ) {
if ( 0 === strpos( $key, $prefix ) ) {
unset( self::$cache[ $key ] );
}
}
}
/**
* Clears all of our static in-memory cache.
*
* @since 4.1.5
*
* @param string $key A key to clear.
* @return void
*/
private function clearStatic( $key = null ) {
if ( empty( $key ) ) {
self::$cache = [];
return;
}
unset( self::$cache[ $this->prepareKey( $key ) ] );
}
/**
* Returns the cache table name.
*
* @since 4.1.5
*
* @return string
*/
public function getTableName() {
return $this->table;
}
}