|
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
|
|
|
|