1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
*TechDivision\Import\Cache\CacheItem |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cache; |
22
|
|
|
|
23
|
|
|
use Psr\Cache\CacheItemInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* A simple, PSR-6 conform cache item implementation. |
27
|
|
|
* |
28
|
|
|
* @author Tim Wagner <[email protected]> |
29
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/techdivision/import |
32
|
|
|
* @link http://www.techdivision.com |
33
|
|
|
*/ |
34
|
|
|
class CacheItem implements CacheItemInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The default timeout. |
39
|
|
|
* |
40
|
|
|
* @var integer |
41
|
|
|
*/ |
42
|
|
|
const DEFAULT_TIMEOUT = 1400; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The cache key. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $key; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The cached value. |
53
|
|
|
* |
54
|
|
|
* @var mixed |
55
|
|
|
*/ |
56
|
|
|
protected $value; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The UNIX timestamp with the expiration date. |
60
|
|
|
* |
61
|
|
|
* @var integer |
62
|
|
|
*/ |
63
|
|
|
protected $expiry = 0; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Initializes the cache item with the key. |
67
|
|
|
* |
68
|
|
|
* @param mixed $key The cache key |
69
|
|
|
*/ |
70
|
|
|
public function __construct($key) |
71
|
|
|
{ |
72
|
|
|
$this->key = $key; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the key for the current cache item. |
77
|
|
|
* |
78
|
|
|
* The key is loaded by the Implementing Library, but should be available to |
79
|
|
|
* the higher level callers when needed. |
80
|
|
|
* |
81
|
|
|
* @return string The key string for this cache item. |
82
|
|
|
* @see \Psr\Cache\CacheItemInterface::getKey() |
83
|
|
|
*/ |
84
|
|
|
public function getKey() |
85
|
|
|
{ |
86
|
|
|
return $this->key; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the value represented by this cache item. |
91
|
|
|
* |
92
|
|
|
* The $value argument may be any item that can be serialized by PHP, |
93
|
|
|
* although the method of serialization is left up to the Implementing |
94
|
|
|
* Library. |
95
|
|
|
* |
96
|
|
|
* @param mixed $value The serializable value to be stored. |
97
|
|
|
* |
98
|
|
|
* @return static The invoked object. |
99
|
|
|
* @see \Psr\Cache\CacheItemInterface::set() |
100
|
|
|
*/ |
101
|
|
|
public function set($value) |
102
|
|
|
{ |
103
|
|
|
$this->value = $value; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Confirms if the cache item lookup resulted in a cache hit. |
109
|
|
|
* |
110
|
|
|
* Note: This method MUST NOT have a race condition between calling isHit() |
111
|
|
|
* and calling get(). |
112
|
|
|
* |
113
|
|
|
* @return boolean TRUE if the request resulted in a cache hit. FALSE otherwise. |
114
|
|
|
* @see \Psr\Cache\CacheItemInterface::isHit() |
115
|
|
|
*/ |
116
|
|
|
public function isHit() |
117
|
|
|
{ |
118
|
|
|
return ($this->expiry === 0 && $this->value !== null) || $this->expiry > time(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Retrieves the value of the item from the cache associated with this object's key. |
123
|
|
|
* |
124
|
|
|
* The value returned must be identical to the value originally stored by set(). |
125
|
|
|
* |
126
|
|
|
* If isHit() returns false, this method MUST return null. Note that null |
127
|
|
|
* is a legitimate cached value, so the isHit() method SHOULD be used to |
128
|
|
|
* differentiate between "null value was found" and "no value was found." |
129
|
|
|
* |
130
|
|
|
* @return mixed The value corresponding to this cache item's key, or null if not found. |
131
|
|
|
* @see \Psr\Cache\CacheItemInterface::get() |
132
|
|
|
*/ |
133
|
|
|
public function get() |
134
|
|
|
{ |
135
|
|
|
return $this->value; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Sets the expiration time for this cache item. |
140
|
|
|
* |
141
|
|
|
* @param \DateTimeInterface|null $expiration The point in time after which the item MUST be considered expired. If null is passed explicitly, a default value MAY be used. If none is set, the value should be stored permanently or for as long as the implementation allows. |
142
|
|
|
* |
143
|
|
|
* @return static The called object. |
144
|
|
|
* @see \Psr\Cache\CacheItemInterface::expiresAt() |
145
|
|
|
* @throws \InvalidArgumentException |
146
|
|
|
*/ |
147
|
|
|
public function expiresAt($expiration) |
148
|
|
|
{ |
149
|
|
|
|
150
|
|
|
if (null === $expiration) { |
151
|
|
|
$this->expiry = CacheItem::DEFAULT_TIMEOUT > 0 ? time() + CacheItem::DEFAULT_TIMEOUT : null; |
152
|
|
|
} elseif ($expiration instanceof \DateTimeInterface) { |
153
|
|
|
$this->expiry = (int) $expiration->format('U'); |
154
|
|
View Code Duplication |
} else { |
|
|
|
|
155
|
|
|
throw new \InvalidArgumentException( |
156
|
|
|
sprintf( |
157
|
|
|
'Expiration date must implement DateTimeInterface or be null, "%s" given', |
158
|
|
|
is_object($expiration) ? get_class($expiration) : gettype($expiration) |
159
|
|
|
) |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Sets the expiration time for this cache item. |
168
|
|
|
* |
169
|
|
|
* @param int|\DateInterval|null $time The period of time from the present after which the item MUST be considered expired. An integer parameter is understood to be the time in seconds until expiration. If null is passed explicitly, a default value MAY be used. If none is set, the value should be stored permanently or for as long as the implementation allows. |
170
|
|
|
* |
171
|
|
|
* @return static The called object. |
172
|
|
|
* @see \Psr\Cache\CacheItemInterface::expiresAfter() |
173
|
|
|
* @throws \InvalidArgumentException |
174
|
|
|
*/ |
175
|
|
|
public function expiresAfter($time) |
176
|
|
|
{ |
177
|
|
|
|
178
|
|
|
if (null === $time) { |
179
|
|
|
$this->expiry = CacheItem::DEFAULT_TIMEOUT > 0 ? time() + CacheItem::DEFAULT_TIMEOUT : null; |
180
|
|
|
} elseif ($time instanceof \DateInterval) { |
181
|
|
|
$this->expiry = (int) \DateTime::createFromFormat('U', time())->add($time)->format('U'); |
182
|
|
|
} elseif (\is_int($time)) { |
183
|
|
|
$this->expiry = $time + time(); |
184
|
|
View Code Duplication |
} else { |
|
|
|
|
185
|
|
|
throw new \InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', \is_object($time) ? \get_class($time) : \gettype($time))); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.