TAPCCache::flush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * TAPCCache class file
5
 *
6
 * @author Alban Hanry <[email protected]>
7
 * @link https://github.com/pradosoft/prado
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 */
10
11
namespace Prado\Caching;
12
13
use Prado\Exceptions\TConfigurationException;
14
15
/**
16
 * TAPCCache class
17
 *
18
 * TAPCCache implements a cache application module based on {@see http://www.php.net/apcu APCu}.
19
 *
20
 * By definition, cache does not ensure the existence of a value
21
 * even if it never expires. Cache is not meant to be an persistent storage.
22
 *
23
 * To use this module, the APCu PHP extension must be loaded and set in the php.ini file.
24
 *
25
 * Some usage examples of TAPCCache are as follows,
26
 * ```php
27
 * $cache=new TAPCCache;  // TAPCCache may also be loaded as a Prado application module
28
 * $cache->init(null);
29
 * $cache->add('object',$object);
30
 * $object2=$cache->get('object');
31
 * ```
32
 *
33
 * If loaded, TAPCCache will register itself with {@see \Prado\TApplication} as the
34
 * cache module. It can be accessed via {@see \Prado\TApplication::getCache()}.
35
 *
36
 * TAPCCache may be configured in application configuration file as follows
37
 * ```php
38
 * <module id="cache" class="Prado\Caching\TAPCCache" />
39
 * ```
40
 *
41
 * @author Alban Hanry <[email protected]>
42
 * @author Knut Urdalen <[email protected]>
43
 * @since 3.0b
44
 */
45
class TAPCCache extends TCache
46
{
47
	/**
48
	 * Initializes this module.
49
	 * This method is required by the IModule interface.
50
	 * @param \Prado\Xml\TXmlElement $config configuration for this module, can be null
51
	 * @throws TConfigurationException if apc extension is not installed or not started, check your php.ini
52
	 */
53
	public function init($config)
54
	{
55
		if (!extension_loaded('apcu')) {
56
			throw new TConfigurationException('apccache_extension_required');
57
		}
58
59
		if (ini_get('apc.enabled') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing ini_get('apc.enabled') of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
60
			throw new TConfigurationException('apccache_extension_not_enabled');
61
		}
62
63
		if (substr(php_sapi_name(), 0, 3) === 'cli' && ini_get('apc.enable_cli') == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing ini_get('apc.enable_cli') of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
64
			throw new TConfigurationException('apccache_extension_not_enabled_cli');
65
		}
66
67
		parent::init($config);
68
	}
69
70
	/**
71
	 * Retrieves a value from cache with a specified key.
72
	 * This is the implementation of the method declared in the parent class.
73
	 * @param string $key a unique key identifying the cached value
74
	 * @return false|string the value stored in cache, false if the value is not in the cache or expired.
75
	 */
76
	protected function getValue($key)
77
	{
78
		return apcu_fetch($key);
79
	}
80
81
	/**
82
	 * Stores a value identified by a key in cache.
83
	 * This is the implementation of the method declared in the parent class.
84
	 *
85
	 * @param string $key the key identifying the value to be cached
86
	 * @param string $value the value to be cached
87
	 * @param int $expire the number of seconds in which the cached value will expire. 0 means never expire.
88
	 * @return bool true if the value is successfully stored into cache, false otherwise
89
	 */
90
	protected function setValue($key, $value, $expire)
91
	{
92
		return apcu_store($key, $value, $expire);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_store($key, $value, $expire) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
93
	}
94
95
	/**
96
	 * Stores a value identified by a key into cache if the cache does not contain this key.
97
	 * This is the implementation of the method declared in the parent class.
98
	 *
99
	 * @param string $key the key identifying the value to be cached
100
	 * @param string $value the value to be cached
101
	 * @param int $expire the number of seconds in which the cached value will expire. 0 means never expire.
102
	 * @return bool true if the value is successfully stored into cache, false otherwise
103
	 */
104
	protected function addValue($key, $value, $expire)
105
	{
106
		return apcu_add($key, $value, $expire);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_add($key, $value, $expire) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
107
	}
108
109
	/**
110
	 * Deletes a value with the specified key from cache
111
	 * This is the implementation of the method declared in the parent class.
112
	 * @param string $key the key of the value to be deleted
113
	 * @return bool if no error happens during deletion
114
	 */
115
	protected function deleteValue($key)
116
	{
117
		return apcu_delete($key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_delete($key) also could return the type string[] which is incompatible with the documented return type boolean.
Loading history...
118
	}
119
120
	/**
121
	 * Deletes all values from cache.
122
	 * Be careful of performing this operation if the cache is shared by multiple applications.
123
	 * @return bool if no error happens during flush
124
	 */
125
	public function flush()
126
	{
127
		return apcu_clear_cache();
128
	}
129
}
130