Completed
Pull Request — master (#70)
by
unknown
13:06
created

DoctrineCacheBridge::getStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * @author Serafim <[email protected]>
5
 * @date 22.03.2016 20:24
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Tmdb\Laravel\Cache;
12
13
use Doctrine\Common\Cache\Cache;
14
use Illuminate\Contracts\Cache\Repository;
15
16
/**
17
 * Class DoctrineCacheBridge
18
 *
19
 * @see https://gist.github.com/SerafimArts/af64ac4edae86a2285fa
20
 */
21
class DoctrineCacheBridge implements Cache
22
{
23
    /**
24
     * @var int
25
     */
26
    private $hitsCount = 0;
27
28
    /**
29
     * @var int
30
     */
31
    private $missesCount = 0;
32
33
    /**
34
     * @var int
35
     */
36
    private $upTime;
37
38
    /**
39
     * @var Repository
40
     */
41
    private $driver;
42
43
    /**
44
     * DoctrineCacheBridge constructor.
45
     * @param Repository $repository
46
     */
47
    public function __construct(Repository $repository)
48
    {
49
        $this->upTime = time();
50
        $this->driver = $repository;
51
    }
52
53
    /**
54
     * @param string $id
55
     * @return mixed
56
     */
57
    public function fetch($id)
58
    {
59
        $result = $this->driver->get($id, false);
60
61
        if (!$result) {
62
            $this->missesCount++;
63
        }
64
65
        $this->hitsCount++;
66
67
        return $result;
68
    }
69
70
    /**
71
     * @param string $id
72
     * @return bool
73
     */
74
    public function contains($id)
75
    {
76
        return $this->driver->has($id);
77
    }
78
79
    /**
80
     * @param string $id
81
     * @param mixed $data
82
     * @param int $lifeTime
83
     * @return bool
84
     */
85
    public function save($id, $data, $lifeTime = 0)
86
    {
87
        return $this->driver->add($id, $data, $lifeTime / 60);
88
    }
89
90
    /**
91
     * @param string $id
92
     * @return bool
93
     */
94
    public function delete($id)
95
    {
96
        return $this->driver->forget($id);
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function getStats()
103
    {
104
        return [
105
            Cache::STATS_HITS             => $this->hitsCount,
106
            Cache::STATS_MISSES           => $this->missesCount,
107
            Cache::STATS_UPTIME           => $this->upTime,
108
            Cache::STATS_MEMORY_USAGE     => null,
109
            Cache::STATS_MEMORY_AVAILABLE => null,
110
        ];
111
    }
112
}
113