-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSR6Bridge.php
95 lines (76 loc) · 1.93 KB
/
PSR6Bridge.php
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
<?php
namespace Psr\SimpleCache;
use Psr\Cache\CacheItemPoolInterface;
class PSR6Bridge implements CacheInterface
{
private $pool;
public function __construct(CacheItemPoolInterface $pool)
{
$this->pool = $pool;
}
public function get($key)
{
$item = $this->pool->getItem($key);
if ($item->isHit()) {
return $item->get();
}
return null;
}
public function set($key, $value, $ttl = null)
{
$item = $this->pool->getItem($key)->set($value);
if (null !== $ttl) {
$item->expiresAfter($ttl);
}
return $this->pool->save($item);
}
public function delete($key)
{
$this->pool->deleteItem($key);
}
public function clear()
{
$this->pool->clear();
}
public function getMultiple($keys)
{
$result = array();
foreach ($this->pool->getItems($keys) as $key => $item) {
$result[$key] = $item->isHit() ? $item->get() : null;
}
return $result;
}
public function setMultiple($items, $ttl = null)
{
foreach ($items as $key => $value) {
$item = $this->pool->getItem($key)->set($value);
if (null !== $ttl) {
$item->expiresAfter($ttl);
}
if (!$this->pool->saveDeferred($item)) {
return false;
}
}
return $this->pool->commit();
}
public function deleteMultiple($keys)
{
$this->pool->deleteItems($keys);
}
public function increment($key, $step = 1)
{
$value = $this->get($key) + $step;
if ($this->set($key, $value)) {
return $value;
}
return false;
}
public function decrement($key, $step = 1)
{
$value = $this->get($key) - $step;
if ($this->set($key, $value)) {
return $value;
}
return false;
}
}