forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheManagerBase.php
More file actions
120 lines (101 loc) · 3.4 KB
/
CacheManagerBase.php
File metadata and controls
120 lines (101 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace ProcessMaker\Cache;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
abstract class CacheManagerBase
{
/**
* The available cache connections.
*
* @var array
*/
protected const AVAILABLE_CONNECTIONS = ['redis', 'cache_settings'];
/**
* Check if a connection is available.
*
* @param string $connection The connection to check.
*
* @return bool True if the connection is available, false otherwise.
*/
private function checkAvailableConnections(string $connection): bool
{
return in_array($connection, self::AVAILABLE_CONNECTIONS);
}
/**
* Get the prefix for a specific connection.
*
* @param string $connection The connection to get the prefix for.
*
* @return string The prefix for the connection.
*/
private function getPrefix(string $connection): string
{
$prefix = config('cache.prefix');
if ($connection === 'cache_settings') {
$prefix = config('cache.stores.' . $connection . '.prefix');
}
return $prefix;
}
/**
* Retrieve an array of cache keys that match a specific pattern.
*
* @param string $pattern The pattern to match.
* @param string|null $connection The cache connection to use.
*
* @return array An array of cache keys that match the pattern.
*/
public function getKeysByPattern(string $pattern, ?string $connection = null, ?string $prefix = null): array
{
if (!$connection) {
$connection = config('cache.default');
}
if (!$this->checkAvailableConnections($connection)) {
throw new CacheManagerException('`getKeysByPattern` method only supports Redis connections.');
}
if (!$prefix) {
$prefix = $this->getPrefix($connection);
}
try {
// Get all keys
$keys = Redis::connection($connection)->keys($prefix . '*');
// Get the Redis prefix
$redisPrefix = config('database.redis.options.prefix');
// Filter keys by pattern and remove the REDIS_PREFIX
$keys = array_map(function ($key) use ($redisPrefix) {
return str_replace($redisPrefix, '', $key);
}, preg_grep('/' . $pattern . '/', $keys));
return $keys;
} catch (Exception $e) {
Log::info('CacheManagerBase: ' . $e->getMessage());
}
return [];
}
/**
* Check if a key exists in the cache.
*
* @param string $key The key to check.
* @param string|null $connection The cache connection to use.
* @param string|null $prefix The cache prefix to use.
*
* @return bool True if the key exists, false otherwise.
*/
public function keyExists(string $key, ?string $connection = null, ?string $prefix = null): bool
{
if (!$connection) {
$connection = config('cache.stores.cache_settings.connection');
}
if (!$this->checkAvailableConnections($connection)) {
return false;
}
if (!$prefix) {
$prefix = $this->getPrefix($connection);
}
try {
return Redis::connection($connection)->exists($prefix . $key);
} catch (Exception $e) {
Log::info('CacheManagerBase: ' . $e->getMessage());
}
return false;
}
}