|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Cmp\Cache\Cache; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Psr\Log\LogLevel; |
|
8
|
|
|
use Redis; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class CacheFactory |
|
12
|
|
|
* |
|
13
|
|
|
* @package Cmp\Cache\Application |
|
14
|
|
|
*/ |
|
15
|
|
|
class CacheBuilder |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var CacheFactoryInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $factory; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $caches = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
private $withExceptions = true; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var LoggerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $logger = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $logLevel = LogLevel::ALERT; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* CacheBuilder constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param CacheFactoryInterface $cacheFactory |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function __construct(CacheFactoryInterface $cacheFactory = null) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$this->factory = $cacheFactory ? $cacheFactory : new CacheFactory(); |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function withoutExceptions() |
|
56
|
|
|
{ |
|
57
|
1 |
|
$this->withExceptions = false; |
|
58
|
|
|
|
|
59
|
1 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Sets a logger to log exceptions |
|
64
|
|
|
* |
|
65
|
|
|
* @param LoggerInterface $logger |
|
66
|
|
|
* @param string $logLevel |
|
67
|
|
|
* |
|
68
|
|
|
* @return $this |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function withLogging(LoggerInterface $logger, $logLevel = LogLevel::ALERT) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->logger = $logger; |
|
73
|
1 |
|
$this->logLevel = $logLevel; |
|
74
|
|
|
|
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string $host |
|
80
|
|
|
* @param int $port |
|
81
|
|
|
* @param int $db |
|
82
|
|
|
* @param float $timeOut |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function withRedisCacheFromParams($host = '127.0.0.1', $port = 6379, $db = 0, $timeOut = 0.0) |
|
87
|
|
|
{ |
|
88
|
1 |
|
$this->caches[] = $this->factory->redisFromParams($host, $port, $db, $timeOut); |
|
89
|
|
|
|
|
90
|
1 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function withArrayCache() |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->caches[] = $this->factory->arrayCache(); |
|
99
|
|
|
|
|
100
|
1 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param Redis $redis |
|
105
|
|
|
* |
|
106
|
|
|
* @return $this |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function withRedis(Redis $redis) |
|
109
|
|
|
{ |
|
110
|
1 |
|
$this->caches[] = $this->factory->redisCache($redis); |
|
111
|
|
|
|
|
112
|
1 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param Cache $cache |
|
117
|
|
|
* |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function withCache(Cache $cache) |
|
121
|
|
|
{ |
|
122
|
1 |
|
$this->caches[] = $cache; |
|
123
|
|
|
|
|
124
|
1 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return Cache |
|
129
|
|
|
*/ |
|
130
|
1 |
|
private function buildCache() |
|
131
|
|
|
{ |
|
132
|
1 |
|
$count = count($this->caches); |
|
133
|
1 |
|
if ($count == 0) { |
|
134
|
1 |
|
return $this->factory->arrayCache(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
if ($count == 1) { |
|
138
|
1 |
|
return $this->caches[0]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
return $this->factory->chainCache($this->caches); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return Cache |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function build() |
|
148
|
|
|
{ |
|
149
|
1 |
|
$cache = $this->buildCache(); |
|
150
|
|
|
|
|
151
|
1 |
|
if ($this->logger || !$this->withExceptions) { |
|
152
|
1 |
|
$cache = $this->factory->loggerCache($cache, $this->withExceptions, $this->logger, $this->logLevel); |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
return $cache; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|