-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWriteOnlyCachePoolDecorator.php
More file actions
200 lines (173 loc) · 4.06 KB
/
WriteOnlyCachePoolDecorator.php
File metadata and controls
200 lines (173 loc) · 4.06 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
192
193
194
195
196
197
198
199
200
<?php declare(strict_types = 1);
namespace Dms\Package\Analytics;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
/**
* The write-only cache item pool decorator.
*
* @author Elliot Levin <elliotlevin@hotmail.com>
*/
class WriteOnlyCachePoolDecorator implements CacheItemPoolInterface
{
/**
* @var CacheItemPoolInterface
*/
protected $cache;
/**
* WriteOnlyCachePoolDecorator constructor.
*
* @param CacheItemPoolInterface $cache
*/
public function __construct(CacheItemPoolInterface $cache)
{
$this->cache = $cache;
}
/**
* @return CacheItemPoolInterface
*/
public function getCache()
{
return $this->cache;
}
protected function cacheItemDecorator(CacheItemInterface $cacheItem)
{
return new class ($cacheItem) implements CacheItemInterface
{
/**
* @var CacheItemInterface
*/
protected $cacheItem;
/**
* @param CacheItemInterface $cacheItem
*/
public function __construct(CacheItemInterface $cacheItem)
{
$this->cacheItem = $cacheItem;
}
/**
* @return CacheItemInterface
*/
public function getInnerCacheItem() : CacheItemInterface
{
return $this->cacheItem;
}
/**
* @inheritDoc
*/
public function getKey()
{
return $this->cacheItem->getKey();
}
/**
* @inheritDoc
*/
public function get()
{
return $this->cacheItem->get();
}
/**
* @inheritDoc
*/
public function isHit()
{
return false;
}
/**
* @inheritDoc
*/
public function set($value)
{
$this->cacheItem->set($value);
}
/**
* @inheritDoc
*/
public function expiresAt($expiration)
{
$this->cacheItem->expiresAt($expiration);
}
/**
* @inheritDoc
*/
public function expiresAfter($time)
{
$this->cacheItem->expiresAfter($time);
}
};
}
/**
* @inheritDoc
*/
public function getItem($key)
{
return $this->cacheItemDecorator($this->cache->getItem($key));
}
/**
* @inheritDoc
*/
public function getItems(array $keys = array())
{
$items = [];
foreach ($this->cache->getItems($keys) as $key => $item) {
$items[$key] = $this->cacheItemDecorator($item);
}
return $items;
}
/**
* @inheritDoc
*/
public function hasItem($key)
{
return false;
}
/**
* @inheritDoc
*/
public function clear()
{
return $this->cache->clear();
}
/**
* @inheritDoc
*/
public function deleteItem($key)
{
return $this->cache->deleteItem($key);
}
/**
* @inheritDoc
*/
public function deleteItems(array $keys)
{
return $this->cache->deleteItems($keys);
}
/**
* @inheritDoc
*/
public function save(CacheItemInterface $item)
{
return $this->cache->save($this->undecorate($item));
}
/**
* @inheritDoc
*/
public function saveDeferred(CacheItemInterface $item)
{
return $this->cache->saveDeferred($this->undecorate($item));
}
/**
* @inheritDoc
*/
public function commit()
{
return $this->cache->commit();
}
private function undecorate(CacheItemInterface $item) : CacheItemInterface
{
if (method_exists($item, 'getInnerCacheItem')) {
return $item->getInnerCacheItem();
} else {
return $item;
}
}
}