1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Reuse\Controllers\Api; |
6
|
|
|
|
7
|
|
|
use App\Component\Cache\Redis\Adapter as RedisAdapter; |
8
|
|
|
use Nymfonya\Component\Config; |
9
|
|
|
use Nymfonya\Component\Container; |
10
|
|
|
|
11
|
|
|
trait TRedisCache |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected $cacheTtl = 60 * 5; |
15
|
|
|
protected $cacheFilename = ''; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Redis client |
19
|
|
|
* |
20
|
|
|
* @var \Redis |
21
|
|
|
*/ |
22
|
|
|
protected $redisClient; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* init redis client |
26
|
|
|
* |
27
|
|
|
* @param Container $container |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
2 |
|
protected function initRedisClient(Container $container): void |
31
|
|
|
{ |
32
|
2 |
|
if (is_null($this->redisClient)) { |
33
|
2 |
|
$redisAdapter = new RedisAdapter( |
34
|
2 |
|
$container->getService(Config::class) |
35
|
|
|
); |
36
|
2 |
|
$this->redisClient = $redisAdapter->getClient(); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* return true if cache is expired |
42
|
|
|
* |
43
|
|
|
* @param string $key |
44
|
|
|
* @return boolean |
45
|
|
|
*/ |
46
|
1 |
|
protected function cacheRedisExpired(string $key = ''): bool |
47
|
|
|
{ |
48
|
1 |
|
$cfn = (empty($key)) |
49
|
1 |
|
? $this->getRedisCacheFilename() |
50
|
1 |
|
: $key; |
51
|
1 |
|
$exist = $this->redisClient->exists($cfn); |
52
|
1 |
|
return !$exist; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* set cache ttl |
57
|
|
|
* |
58
|
|
|
* @param integer $ttl |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
2 |
|
protected function setRedisCacheTtl(int $ttl): void |
62
|
|
|
{ |
63
|
2 |
|
$this->cacheTtl = $ttl; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* returns cache content |
68
|
|
|
* |
69
|
|
|
* @param string $key |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
2 |
|
protected function getRedisCache(string $key = '') |
73
|
|
|
{ |
74
|
2 |
|
$cfn = (empty($key)) |
75
|
2 |
|
? $this->getRedisCacheFilename() |
76
|
2 |
|
: $key; |
77
|
2 |
|
$cache = $this->redisClient->get($cfn); |
78
|
2 |
|
return (\is_bool($cache)) |
|
|
|
|
79
|
1 |
|
? null |
80
|
2 |
|
: json_decode( |
81
|
2 |
|
$cache, |
82
|
2 |
|
true, |
83
|
2 |
|
512, |
84
|
2 |
|
JSON_OBJECT_AS_ARRAY |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* set cache content |
90
|
|
|
* |
91
|
|
|
* @param mixed $content |
92
|
|
|
* @param string $key |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
2 |
|
protected function setRedisCache($content, $key = ''): void |
96
|
|
|
{ |
97
|
2 |
|
$cfn = (empty($key)) |
98
|
2 |
|
? $this->getRedisCacheFilename() |
99
|
2 |
|
: $key; |
100
|
2 |
|
$this->redisClient->set($cfn, json_encode($content)); |
101
|
2 |
|
$this->redisClient->expire($cfn, $this->cacheTtl); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* clear cache entry |
106
|
|
|
* |
107
|
|
|
* @param string $key |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
1 |
|
protected function clearRedisCache(string $key = '') |
111
|
|
|
{ |
112
|
1 |
|
$key = \strval($key); |
113
|
1 |
|
if (empty($key)) { |
114
|
1 |
|
$this->redisClient->del($this->getRedisCacheFilename()); |
115
|
1 |
|
} elseif ($key == '*') { |
116
|
1 |
|
$this->redisClient->flushAll(); |
117
|
|
|
} else { |
118
|
1 |
|
$this->redisClient->del($key); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* returns cache filename from request uri |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
1 |
|
protected function getRedisCacheFilename(): string |
128
|
|
|
{ |
129
|
1 |
|
$path = $this->getRedisCachePath(); |
130
|
1 |
|
$filename = md5($this->request->getUri()); |
131
|
1 |
|
return $path . $filename; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* returns cache path from request script filename |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
1 |
|
protected function getRedisCachePath(): string |
140
|
|
|
{ |
141
|
1 |
|
return 'API_REQUEST_REDIS_CACHE_'; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|