Completed
Push — master ( 0f341b...3e6c76 )
by Tobias
21:32
created

CachePool::start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\CacheBundle\Cache\Recording;
13
14
use Psr\Cache\CacheItemInterface;
15
use Psr\Cache\CacheItemPoolInterface;
16
use Psr\Log\LoggerInterface;
17
18
/**
19
 * A pool that logs and collects all your cache calls.
20
 *
21
 * @author Aaron Scherer <[email protected]>
22
 * @author Tobias Nyholm <[email protected]>
23
 * @author Nicolas Grekas <[email protected]>
24
 */
25
class CachePool implements CacheItemPoolInterface
26
{
27
    /**
28
     * @type CacheItemPoolInterface
29
     */
30
    protected $pool;
31
32
    /**
33
     * @type LoggerInterface
34
     */
35
    private $logger;
36
37
    /**
38
     * @type string
39
     */
40
    private $name;
41
42
    /**
43
     * @type string
44
     */
45
    private $level = 'info';
46
47
    /**
48
     * @type array calls
49
     */
50
    private $calls = [];
51
52
    /**
53
     * @param CacheItemPoolInterface $pool
54
     */
55
    public function __construct(CacheItemPoolInterface $pool)
56
    {
57
        $this->pool = $pool;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getItem($key)
64
    {
65
        $event = $this->start(__FUNCTION__, $key);
66
        try {
67
            $item = $this->pool->getItem($key);
68
        } finally {
69
            $event->end = microtime(true);
70
        }
71
        if ($item->isHit()) {
72
            ++$event->hits;
73
        } else {
74
            ++$event->misses;
75
        }
76
        $event->result = $item->get();
77
78
        return $item;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function hasItem($key)
85
    {
86
        $event = $this->start(__FUNCTION__, $key);
87
        try {
88
            $event->result = $this->pool->hasItem($key);
89
        } finally {
90
            $event->end = microtime(true);
91
        }
92
93
        if (!$event->result) {
94
            ++$event->misses;
95
        }
96
97
        return $event->result;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 View Code Duplication
    public function deleteItem($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
    {
105
        $event = $this->start(__FUNCTION__, $key);
106
        try {
107
            return $event->result = $this->pool->deleteItem($key);
108
        } finally {
109
            $event->end = microtime(true);
110
        }
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 View Code Duplication
    public function save(CacheItemInterface $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
117
    {
118
        $event = $this->start(__FUNCTION__, $item);
119
        try {
120
            return $event->result = $this->pool->save($item);
121
        } finally {
122
            $event->end = microtime(true);
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 View Code Duplication
    public function saveDeferred(CacheItemInterface $item)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
130
    {
131
        $event = $this->start(__FUNCTION__, $item);
132
        try {
133
            return $event->result = $this->pool->saveDeferred($item);
134
        } finally {
135
            $event->end = microtime(true);
136
        }
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getItems(array $keys = [])
143
    {
144
        $event = $this->start(__FUNCTION__, $keys);
145
        try {
146
            $result = $this->pool->getItems($keys);
147
        } finally {
148
            $event->end = microtime(true);
149
        }
150
        $f = function () use ($result, $event) {
151
            $event->result = [];
152
            foreach ($result as $key => $item) {
153
                if ($item->isHit()) {
154
                    ++$event->hits;
155
                } else {
156
                    ++$event->misses;
157
                }
158
                $event->result[$key] = $item->get();
159
                yield $key => $item;
160
            }
161
        };
162
163
        return $f();
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function clear()
170
    {
171
        $event = $this->start(__FUNCTION__);
172
        try {
173
            return $event->result = $this->pool->clear();
174
        } finally {
175
            $event->end = microtime(true);
176
        }
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 View Code Duplication
    public function deleteItems(array $keys)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
183
    {
184
        $event = $this->start(__FUNCTION__, $keys);
185
        try {
186
            return $event->result = $this->pool->deleteItems($keys);
187
        } finally {
188
            $event->end = microtime(true);
189
        }
190
    }
191
192
    /**
193
     * {@inheritdoc}
194
     */
195
    public function commit()
196
    {
197
        $event = $this->start(__FUNCTION__);
198
        try {
199
            return $event->result = $this->pool->commit();
200
        } finally {
201
            $event->end = microtime(true);
202
        }
203
    }
204
205
    public function getCalls()
206
    {
207
        return $this->calls;
208
    }
209
210
    protected function start($name, $argument = null)
211
    {
212
        $this->calls[]   = $event   = new TraceableAdapterEvent();
213
        $event->name     = $name;
214
        $event->argument = $argument;
215
        $event->start    = microtime(true);
216
217
        return $event;
218
    }
219
220
    /**
221
     * @param LoggerInterface $logger
222
     */
223
    public function setLogger(LoggerInterface $logger = null)
224
    {
225
        $this->logger = $logger;
226
    }
227
228
    /**
229
     * @param string $name
230
     */
231
    public function setName($name)
232
    {
233
        $this->name = $name;
234
235
        return $this;
236
    }
237
238
    /**
239
     * @param string $level
240
     */
241
    public function setLevel($level)
242
    {
243
        $this->level = $level;
244
245
        return $this;
246
    }
247
}
248
249
/**
250
 * @internal
251
 */
252
class TraceableAdapterEvent
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
253
{
254
    public $name;
255
    public $argument;
256
    public $start;
257
    public $end;
258
    public $result;
259
    public $hits   = 0;
260
    public $misses = 0;
261
}
262