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