-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCache.php
More file actions
191 lines (166 loc) · 4.59 KB
/
Cache.php
File metadata and controls
191 lines (166 loc) · 4.59 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
<?php
/**
* Cache Class.
*
* @package SugiPHP.Cache
* @author Plamen Popov <tzappa@gmail.com>
* @license http://opensource.org/licenses/mit-license.php (MIT License)
*/
namespace SugiPHP\Cache;
class Cache
{
/**
* The driver used by a cache.
* @var StoreInterface
*/
protected $driver;
/**
* Key prefix to use.
*
* @var string
*/
protected $prefix = "";
/**
* Class constructor.
*
* @param StoreInterface $driver
*/
public function __construct(StoreInterface $driver)
{
$this->driver = $driver;
}
/**
* Sets key prefix.
*
* @param string $prefix
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
/**
* Returns key prefix.
*
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
/**
* Stores an item in the cache for a specified period of time only if it is not already stored.
* Cache::add() is similar to Cache::set(), but the operation fails if the key already exists.
*
* @param string $key
* @param mixed $value The value to be stored.
* @param integer $ttl Time to live in seconds. 0 means to store it for a maximum time possible
*
* @return boolean TRUE if the value is set, FALSE on failure
*/
public function add($key, $value, $ttl = 0)
{
$key = $this->prefix.$key;
return $this->driver->add($key, $value, $ttl);
}
/**
* Stores an item in the data store
* Cache::set() is similar to Cache::add(), but the operation will not fail if the key already exist.
*
* @param string $key The key under which to store the value
* @param mixed $value The value to store
* @param integer $ttl Expiration time in seconds, after which the value is invalidated (deleted)
*
* @return boolean TRUE on success or FALSE on failure
*/
public function set($key, $value, $ttl = 0)
{
$key = $this->prefix.$key;
return $this->driver->set($key, $value, $ttl);
}
/**
* Fetches a stored variable from the cache
*
* @param string $key The key used to store the value
*
* @return mixed Returns NULL if the key does not exist in the store or the value was expired (see $ttl)
*/
public function get($key, $defaultValue = null)
{
$key = $this->prefix.$key;
$result = $this->driver->get($key);
return is_null($result) ? $defaultValue : $result;
}
/**
* Checks if the key exists
*
* @param string $key
*
* @return boolean TRUE if the key exists, otherwise FALSE
*/
public function has($key)
{
$key = $this->prefix.$key;
return $this->driver->has($key);
}
/**
* Removes a stored variable from the cache
*
* @param string $key
*/
public function delete($key)
{
$key = $this->prefix.$key;
$this->driver->delete($key);
}
/**
* Invalidate all items in the cache
*/
public function flush()
{
$this->driver->flush();
}
/**
* Increment numeric item's value.
* If there is no such key or the stored value is not numeric FALSE is returned
*
* @param string $key
* @param integer $step
*
* @return integer|false Returns the new incremented value or FALSE on failure
*/
public function inc($key, $step = 1)
{
$key = $this->prefix.$key;
if ($this->driver instanceof IncrementorInterface) {
return $this->driver->inc($key, $step);
}
$value = $this->driver->get($key);
if (is_null($value) || !is_numeric($value)) {
return false;
}
$newValue = $value + $step;
return ($this->driver->set($key, $newValue)) ? $newValue : false;
}
/**
* Decrements numeric item's value.
* If there is no such key or the stored value is not numeric FALSE is returned
*
* @param string $key
* @param integer $step
*
* @return integer|false Returns the new decremented value or FALSE on failure
*/
public function dec($key, $step = 1)
{
$key = $this->prefix.$key;
if ($this->driver instanceof IncrementorInterface) {
return $this->driver->dec($key, $step);
}
$value = $this->driver->get($key);
if (is_null($value) || !is_numeric($value)) {
return false;
}
$newValue = $value - $step;
return ($this->driver->set($key, $newValue)) ? $newValue : false;
}
}