chg: [internal] New method RedisTool::unlink

pull/8634/head
Jakub Onderka 2022-10-01 09:48:13 +02:00
parent 24c17ede94
commit 341ac2d0a8
3 changed files with 34 additions and 17 deletions

View File

@ -45,14 +45,11 @@ class RedisTool
/**
* @param Redis $redis
* @param string $pattern
* @return int Number of deleted keys
* @return int|Redis Number of deleted keys or instance of Redis if used in MULTI mode
* @throws RedisException
*/
public static function deleteKeysByPattern(Redis $redis, $pattern)
{
// Unlink is non blocking way how to delete keys from Redis, but it must be supported by PHP extension and
// Redis itself
$unlinkSupported = method_exists($redis, 'unlink') && $redis->unlink(null) !== false;
$iterator = null;
$allKeys = [];
while (false !== ($keys = $redis->scan($iterator, $pattern))) {
@ -65,7 +62,25 @@ class RedisTool
return 0;
}
return $unlinkSupported ? $redis->unlink($allKeys) : $redis->del($allKeys);
return self::unlink($redis, $allKeys);
}
/**
* Unlink is non blocking way how to delete keys from Redis, but it must be supported by PHP extension and Redis itself
*
* @param Redis $redis
* @param string|array $keys
* @return int|Redis Number of deleted keys or instance of Redis if used in MULTI mode
* @throws RedisException
*/
public static function unlink(Redis $redis, $keys)
{
static $unlinkSupported;
if ($unlinkSupported === null) {
// Check if unlink is supported
$unlinkSupported = method_exists($redis, 'unlink') && $redis->unlink(null) === 0;
}
return $unlinkSupported ? $redis->unlink($keys) : $redis->del($keys);
}
/**

View File

@ -627,7 +627,7 @@ class Correlation extends AppModel
public function generateTopCorrelations($jobId = false)
{
try {
$redis = $this->setupRedisWithException();
$redis = RedisTool::init();
} catch (Exception $e) {
throw new NotFoundException(__('No redis connection found.'));
}
@ -652,7 +652,7 @@ class Correlation extends AppModel
}
$maxId = $maxId[0]['max_id'];
$redis->del(self::CACHE_NAME);
RedisTool::unlink($redis, self::CACHE_NAME);
$redis->set(self::CACHE_AGE, time());
$chunkSize = 1000000;
$maxPage = ceil($maxId / $chunkSize);
@ -682,7 +682,7 @@ class Correlation extends AppModel
public function findTop(array $query)
{
try {
$redis = $this->setupRedisWithException();
$redis = RedisTool::init();
} catch (Exception $e) {
return false;
}
@ -712,7 +712,7 @@ class Correlation extends AppModel
public function getTopTime()
{
try {
$redis = $this->setupRedisWithException();
$redis = RedisTool::init();
} catch (Exception $e) {
return false;
}
@ -754,22 +754,22 @@ class Correlation extends AppModel
*/
public function updateCidrList()
{
$redis = $this->setupRedisWithException();
$redis = RedisTool::init();
$cidrList = [];
$this->cidrListCache = null;
if ($redis) {
$cidrList = $this->getCidrListFromDatabase();
$redis->pipeline();
$redis->del('misp:cidr_cache_list');
RedisTool::unlink($redis, 'misp:cidr_cache_list');
if (method_exists($redis, 'saddArray')) {
$redis->sAddArray('misp:cidr_cache_list', $cidrList);
} else {
$redis->pipeline();
foreach ($cidrList as $cidr) {
$redis->sadd('misp:cidr_cache_list', $cidr);
}
$redis->exec();
}
$redis->exec();
}
return $cidrList;
}
@ -791,16 +791,17 @@ class Correlation extends AppModel
return $this->cidrListCache;
}
$redis = $this->setupRedisWithException();
if ($redis) {
try {
$redis = RedisTool::init();
if (!$redis->exists('misp:cidr_cache_list')) {
$cidrList = $this->updateCidrList();
} else {
$cidrList = $redis->smembers('misp:cidr_cache_list');
}
} else {
} catch (Exception $e) {
$cidrList = $this->getCidrListFromDatabase();
}
$this->cidrListCache = $cidrList;
return $cidrList;
}

View File

@ -797,6 +797,7 @@ class Server extends AppModel
* @throws HttpSocketHttpException
* @throws HttpSocketJsonException
* @throws JsonException
* @throws RedisException
*/
public function getEventIndexFromServer(ServerSyncTool $serverSync, $ignoreFilterRules = false)
{
@ -838,7 +839,7 @@ class Server extends AppModel
$data = RedisTool::serialize([$response->headers["ETag"], $eventIndex]);
$redis->setex("misp:event_index:{$serverSync->serverId()}", 3600 * 24, $data);
} else if ($indexFromCache) {
$redis->del("misp:event_index:{$serverSync->serverId()}");
RedisTool::unlink($redis, "misp:event_index:{$serverSync->serverId()}");
}
return $eventIndex;