1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix\Cache\PsrCache; |
14
|
|
|
|
15
|
|
|
use Apix\Cache\Adapter as CacheAdapter; |
16
|
|
|
use Psr\Cache\CacheItemInterface as ItemInterface; |
17
|
|
|
use Psr\Cache\CacheItemPoolInterface as ItemPoolInterface; |
18
|
|
|
|
19
|
|
|
class Pool implements ItemPoolInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var CacheAdapter |
25
|
|
|
*/ |
26
|
|
|
protected $cache_adapter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Deferred cache items to be saved later. |
30
|
|
|
* |
31
|
|
|
* @var array Collection of \Apix\PsrCache\Item. |
32
|
|
|
*/ |
33
|
|
|
protected $deferred = array(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
*/ |
38
|
204 |
|
public function __construct(CacheAdapter $cache_adapter) |
39
|
|
|
{ |
40
|
204 |
|
$this->cache_adapter = $cache_adapter; |
41
|
|
|
|
42
|
|
|
$options = array( |
43
|
204 |
|
'prefix_key' => 'cask-key-', // prefix cache keys |
44
|
|
|
'tag_enable' => false // wether to enable tagging |
45
|
204 |
|
); |
46
|
204 |
|
$this->cache_adapter->setOptions($options); |
47
|
204 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
136 |
|
public function getItem($key) |
53
|
|
|
{ |
54
|
136 |
|
$value = $this->cache_adapter->loadKey($key); |
55
|
|
|
|
56
|
136 |
|
return new Item( |
57
|
136 |
|
$this->cache_adapter->removePrefixKey($key), |
58
|
136 |
|
$value, |
59
|
136 |
|
$this->cache_adapter->getTtl($key) ?: null, |
60
|
|
|
(bool) $value // indicates wether it is loaded from cache or not. |
61
|
136 |
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
68 |
|
public function getItems(array $keys = array()) |
68
|
|
|
{ |
69
|
68 |
|
$items = array(); |
70
|
68 |
|
foreach ($keys as $key) { |
71
|
68 |
|
$items[$key] = $this->getItem($key); |
72
|
68 |
|
} |
73
|
|
|
|
74
|
68 |
|
return $items; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function hasItem($key) |
81
|
|
|
{ |
82
|
|
|
return $this->getItem($key)->isHit(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
34 |
|
public function clear() |
89
|
|
|
{ |
90
|
34 |
|
return $this->cache_adapter->flush(true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
34 |
|
public function deleteItems(array $keys) |
97
|
|
|
{ |
98
|
34 |
|
foreach ($keys as $key) { |
99
|
34 |
|
$this->cache_adapter->delete($key); |
100
|
34 |
|
} |
101
|
|
|
|
102
|
34 |
|
return $this; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function deleteItem($key) |
109
|
|
|
{ |
110
|
|
|
return $this->deleteItems(array($key)); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
136 |
|
public function save(ItemInterface $item) |
117
|
|
|
{ |
118
|
136 |
|
$ttl = $item->getTtlInSecond(); |
119
|
|
|
|
120
|
136 |
|
$item->setHit(true); |
121
|
136 |
|
$success = $this->cache_adapter->save( |
122
|
136 |
|
$item->get(), // value to store |
123
|
136 |
|
$item->getKey(), // its key |
124
|
136 |
|
null, // disable tags support |
125
|
136 |
|
is_null($ttl) ? 0 : $ttl // ttl in sec or null for ever |
126
|
136 |
|
); |
127
|
136 |
|
$item->setHit($success); |
128
|
|
|
|
129
|
136 |
|
return $this; |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
34 |
|
public function saveDeferred(ItemInterface $item) |
136
|
|
|
{ |
137
|
34 |
|
$this->deferred[] = $item; |
138
|
|
|
|
139
|
34 |
|
return $this; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
34 |
|
public function commit() |
146
|
|
|
{ |
147
|
34 |
|
foreach ($this->deferred as $key => $item) { |
148
|
34 |
|
$this->save($item); |
149
|
34 |
|
if ( $item->isHit() ) { |
150
|
34 |
|
unset($this->deferred[$key]); |
151
|
34 |
|
} |
152
|
34 |
|
} |
153
|
|
|
|
154
|
34 |
|
return empty($this->deferred); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Returns the cache adapter for this pool. |
159
|
|
|
* |
160
|
|
|
* @return CacheAdapter |
161
|
|
|
*/ |
162
|
102 |
|
public function getCacheAdapter() |
163
|
|
|
{ |
164
|
102 |
|
return $this->cache_adapter; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
} |
168
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.