Cache   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 73
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A invalidate() 0 3 1
A get() 0 2 1
A set() 0 2 1
A delete() 0 2 1
A __construct() 0 7 2
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 an inteface to the WordPress Object Cache.
29
 *
30
 * @since 1.0.0
31
 *
32
 * @author Peter Putzer <[email protected]>
33
 */
34
class Cache extends Abstract_Cache {
35
36
	/**
37
	 * The incrementor cache key.
38
	 *
39
	 * @var string
40
	 */
41
	private $incrementor_key;
42
43
	/**
44
	 * The cache group.
45
	 *
46
	 * @var string
47
	 */
48
	private $group;
49
50
	/**
51
	 * Create new cache instance.
52
	 *
53
	 * @param string      $prefix The prefix automatically added to cache keys.
54
	 * @param string|null $group  Optional. The cache group. Defaults to $prefix.
55
	 */
56 1
	public function __construct( $prefix, $group = null ) {
57
58 1
		$this->group           = ! isset( $group ) ? $prefix : $group;
59 1
		$this->incrementor_key = "{$prefix}cache_incrementor";
60 1
		$this->incrementor     = (int) \wp_cache_get( $this->incrementor_key, $this->group );
61
62 1
		parent::__construct( $prefix );
63 1
	}
64
65
	/**
66
	 * Invalidate all cached elements by reseting the incrementor.
67
	 */
68 1
	public function invalidate() {
69 1
		$this->incrementor = time();
70 1
		\wp_cache_set( $this->incrementor_key, $this->incrementor, $this->group, 0 );
71 1
	}
72
73
	/**
74
	 * Retrieves a cached value.
75
	 *
76
	 * @param string    $key   The cache key.
77
	 * @param bool|null $found Optional. Whether the key was found in the cache. Disambiguates a return of false as a storable value. Passed by reference. Default null.
78
	 *
79
	 * @return mixed
80
	 */
81 1
	public function get( $key, &$found = null ) {
82 1
		return \wp_cache_get( $this->get_key( $key ), $this->group, false, $found );
83
	}
84
85
	/**
86
	 * Sets an entry in the cache and stores the key.
87
	 *
88
	 * @param string $key       The cache key.
89
	 * @param mixed  $value     The value to store.
90
	 * @param int    $duration  Optional. The duration in seconds. Default 0 (no expiration).
91
	 *
92
	 * @return bool True if the cache could be set successfully.
93
	 */
94 1
	public function set( $key, $value, $duration = 0 ) {
95 1
		return \wp_cache_set( $this->get_key( $key ), $value, $this->group, $duration );
96
	}
97
98
	/**
99
	 * Deletes an entry from the cache.
100
	 *
101
	 * @param string $key The cache key root.
102
	 *
103
	 * @return bool True on successful removal, false on failure.
104
	 */
105 1
	public function delete( $key ) {
106 1
		return \wp_cache_delete( $this->get_key( $key ), $this->group );
107
	}
108
}
109