1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of mundschenk-at/wp-data-storage. |
4
|
|
|
* |
5
|
|
|
* Copyright 2017-2018 Peter Putzer. |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or ( at your option ) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20
|
|
|
* |
21
|
|
|
* @package mundschenk-at/wp-data-storage/tests |
22
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.html |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Mundschenk\Data_Storage; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Implements a generic caching mechanism for WordPress. |
29
|
|
|
* |
30
|
|
|
* @since 1.0.0 |
31
|
|
|
* |
32
|
|
|
* @author Peter Putzer <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
abstract class Abstract_Cache { |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Incrementor for cache invalidation. |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $incrementor; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The prefix added to all keys. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $prefix; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Create new cache instance. |
52
|
|
|
* |
53
|
|
|
* @param string $prefix The prefix automatically added to cache keys. |
54
|
|
|
*/ |
55
|
1 |
|
public function __construct( $prefix ) { |
56
|
1 |
|
$this->prefix = $prefix; |
57
|
|
|
|
58
|
1 |
|
if ( empty( $this->incrementor ) ) { |
59
|
1 |
|
$this->invalidate(); |
60
|
|
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Invalidate all cached elements by reseting the incrementor. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
abstract public function invalidate(); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retrieves a cached value. |
72
|
|
|
* |
73
|
|
|
* @param string $key The cache key root. |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
abstract public function get( $key ); |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Sets an entry in the cache and stores the key. |
81
|
|
|
* |
82
|
|
|
* @param string $key The cache key root. |
83
|
|
|
* @param mixed $value The value to store. |
84
|
|
|
* @param int $duration Optional. The duration in seconds. Default 0 (no expiration). |
85
|
|
|
* |
86
|
|
|
* @return bool True if the cache could be set successfully. |
87
|
|
|
*/ |
88
|
|
|
abstract public function set( $key, $value, $duration = 0 ); |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Deletes an entry from the cache. |
92
|
|
|
* |
93
|
|
|
* @param string $key The cache key root. |
94
|
|
|
* |
95
|
|
|
* @return bool True on successful removal, false on failure. |
96
|
|
|
*/ |
97
|
|
|
abstract public function delete( $key ); |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retrieves the complete key to use. |
101
|
|
|
* |
102
|
|
|
* @param string $key The cache key root. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
1 |
|
protected function get_key( $key ) { |
107
|
1 |
|
return "{$this->prefix}{$this->incrementor}_{$key}"; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieves the set prefix. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
1 |
|
protected function get_prefix() { |
116
|
1 |
|
return $this->prefix; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|