Test Failed
Push — master ( d844b5...1ec8b0 )
by Joao
34s
created

CachePool::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace ByJG\Cache\Psr6;
4
5
use ByJG\Cache\Psr16\BaseCacheEngine;
6
use Psr\Cache\CacheItemInterface;
7
use Psr\Cache\CacheItemPoolInterface;
8
use Psr\Log\InvalidArgumentException;
9
10
class CachePool implements CacheItemPoolInterface
11
{
12
    /**
13
     * @var \Psr\SimpleCache\CacheInterface
14
     */
15
    protected $_cacheEngine;
16
17
    /**
18
     * @var CacheItem
19
     */
20
    protected $_lastCacheItem;
21
22
    /**
23
     * @var int
24
     */
25
    protected $bufferSize = 10;
26
27
    /**
28
     * @var CacheItem[]
29
     */
30
    protected $buffer = [];
31
32
    /**
33
     * @var array
34
     */
35
    protected $bufferKeys = [];
36
37
    /**
38
     * CachePool constructor.
39
     * 
40
     * @param BaseCacheEngine $_cacheEngine
41
     * @param int $bufferSize
42
     */
43
    public function __construct(BaseCacheEngine $_cacheEngine, $bufferSize = 10)
44
    {
45
        $this->_cacheEngine = $_cacheEngine;
46
        $this->bufferSize = intval($bufferSize);
47
    }
48
49
    /**
50
     * @return int
51
     */
52
    public function getBufferSize()
53
    {
54
        return $this->bufferSize;
55
    }
56
57
    /**
58
     * @param int $bufferSize
59
     */
60
    public function setBufferSize($bufferSize)
61
    {
62
        $this->bufferSize = $bufferSize;
63
    }
64
65
66
    /**
67
     * Add an element to buffer. If the buffer is full, the first element added will be removed 
68
     * 
69
     * @param CacheItem $cacheItem
70
     */
71
    protected function addElementToBuffer(CacheItem $cacheItem)
72
    {
73
        if ($this->bufferSize < 1) {
74
            return;
75
        }
76
77
        $key = $cacheItem->getKey();
78
        $this->buffer[$key] = $cacheItem;
79
80
        if (in_array($key, $this->bufferKeys)) {
81
            return;
82
        }
83
84
        array_push($this->bufferKeys, $key);
85
86
        if (count($this->bufferKeys) > $this->bufferSize) {
87
            $element = array_shift($this->bufferKeys);
88
            unset($this->buffer[$element]);
89
        }
90
    }
91
92
    /**
93
     * Remove a specific key from buffer
94
     * 
95
     * @param $key
96
     */
97
    protected function removeElementFromBuffer($key)
98
    {
99
        $result = array_search($key, $this->bufferKeys);
100
        if ($result === false) {
101
            return;
102
        }
103
104
        unset($this->buffer[$key]);
105
        unset($this->bufferKeys[$result]);
106
    }
107
108
    /**
109
     * Psr implementation of getItem()
110
     * 
111
     * @param string $key
112
     * @return CacheItem
113
     */
114
    public function getItem($key)
115
    {
116
        // Get the element from the buffer if still remains valid!
117
        if (in_array($key, $this->bufferKeys)) {
118
            $cacheItem = $this->buffer[$key];
119
            if ($cacheItem->getExpiresInSecs() > 1) {
120
                return $cacheItem;
121
            }
122
        }
123
        
124
        // Get the element from the cache!
125
        $result = $this->_cacheEngine->get($key);
126
        $cache = new CacheItem($key, $result, $result !== null);
127
128
        $this->addElementToBuffer($cache);
129
130
        return $cache;
131
    }
132
133
    /**
134
     * Psr implementation of getItems()
135
     * 
136
     * @param array $keys
137
     * @return array
138
     */
139
    public function getItems(array $keys = array())
140
    {
141
        $result = [];
142
        foreach ($keys as $key) {
143
            $result[] = $this->getItem($key);
144
        }
145
146
        return $result;
147
    }
148
149
    /**
150
     * Psr implementation of hasItems()
151
     * 
152
     * @param string $key
153
     * @return bool
154
     */
155
    public function hasItem($key)
156
    {
157
        return $this->getItem($key)->isHit();
158
    }
159
160
    /**
161
     * Psr implementation of clear()
162
     */
163
    public function clear()
164
    {
165
        $this->bufferKeys = [];
166
        $this->buffer = [];
167
    }
168
169
    /**
170
     * Psr implementation of deleteItem()
171
     *
172
     * @param string $key
173
     * @return bool
174
     */
175
    public function deleteItem($key)
176
    {
177
        return $this->deleteItems([$key]);
178
    }
179
180
    /**
181
     * Psr Implementation of deleteItems()
182
     * 
183
     * @param array $keys
184
     * @return bool
185
     */
186
    public function deleteItems(array $keys)
187
    {
188
        foreach ($keys as $key) {
189
            $this->_cacheEngine->delete($key);
190
            $this->removeElementFromBuffer($key);
191
        }
192
        
193
        return true;
194
    }
195
196
    /**
197
     * @param CacheItemInterface $item
198
     * @return bool
199
     */
200
    public function save(CacheItemInterface $item)
201
    {
202
        if (!($item instanceof CacheItem)) {
203
            throw new InvalidArgumentException('The cache item must be an implementation of \ByJG\Cache\Psr\CacheItem');
204
        }
205
        
206
        if ($item->getExpiresInSecs() < 1) {
207
            throw new InvalidArgumentException('Object has expired!');
208
        }
209
        
210
        $this->_cacheEngine->set($item->getKey(), $item->get(), $item->getExpiresInSecs());
211
        $this->addElementToBuffer($item);
212
        
213
        return true;
214
    }
215
216
    /**
217
     * @var CacheItem[]
218
     */
219
    protected $deferredItem = [];
220
221
    /**
222
     * Psr Implementation of saveDeferred()
223
     * 
224
     * @param CacheItemInterface $item
225
     * @return bool
226
     */
227
    public function saveDeferred(CacheItemInterface $item)
228
    {
229
        $this->deferredItem[] = $item;
230
        return true;
231
    }
232
233
    /**
234
     * Psr implementation of commit()
235
     */
236
    public function commit()
237
    {
238
        foreach ($this->deferredItem as $item) {
239
            $this->save($item);
240
        }
241
        
242
        $this->deferredItem = [];
243
    }
244
245
    public function isAvailable()
246
    {
247
        return $this->_cacheEngine->isAvailable();
248
    }
249
}
250