mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-09 16:44:26 +02:00
Create CacheRepository for cache table
This commit is contained in:
parent
59ee2f34e7
commit
a4ca74a3aa
2 changed files with 191 additions and 0 deletions
94
src/Database/Repository/CacheRepository.php
Normal file
94
src/Database/Repository/CacheRepository.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
// Copyright (C) 2010-2024, the Friendica project
|
||||
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Friendica\Database\Repository;
|
||||
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Database\DatabaseException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Repository for cache table
|
||||
*/
|
||||
final class CacheRepository
|
||||
{
|
||||
private Database $database;
|
||||
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->database = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DatabaseException
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getAllKeysValidUntil(string $expires): array
|
||||
{
|
||||
$throw = $this->database->throwExceptionsOnErrors(true);
|
||||
|
||||
try {
|
||||
return $this->getAllKeys($expires, null);
|
||||
} catch (Throwable $th) {
|
||||
if (! $th instanceof DatabaseException) {
|
||||
$th = new DatabaseException('Cannot fetch all keys without prefix', 0, '', $th);
|
||||
}
|
||||
|
||||
throw $th;
|
||||
} finally {
|
||||
$this->database->throwExceptionsOnErrors($throw);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DatabaseException
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getAllKeysValidUntilWithPrefix(string $expires, string $prefix): array
|
||||
{
|
||||
$throw = $this->database->throwExceptionsOnErrors(true);
|
||||
|
||||
try {
|
||||
return $this->getAllKeys($expires, $prefix);
|
||||
} catch (Throwable $th) {
|
||||
if (! $th instanceof DatabaseException) {
|
||||
$th = new DatabaseException(sprintf('Cannot fetch all keys with prefix `%s`', $prefix), 0, '', $th);
|
||||
}
|
||||
|
||||
throw $th;
|
||||
} finally {
|
||||
$this->database->throwExceptionsOnErrors($throw);
|
||||
}
|
||||
}
|
||||
|
||||
private function getAllKeys(string $expires, ?string $prefix = null): array
|
||||
{
|
||||
if ($prefix === null) {
|
||||
$where = ['`expires` >= ?', $expires];
|
||||
} else {
|
||||
$where = ['`expires` >= ? AND `k` LIKE CONCAT(?, \'%\')', $expires, $prefix];
|
||||
}
|
||||
|
||||
$stmt = $this->database->select('cache', ['k'], $where);
|
||||
|
||||
$keys = [];
|
||||
|
||||
try {
|
||||
while ($key = $this->database->fetch($stmt)) {
|
||||
array_push($keys, $key['k']);
|
||||
}
|
||||
} finally {
|
||||
$this->database->close($stmt);
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
}
|
97
tests/Unit/Database/Repository/CacheRepositoryTest.php
Normal file
97
tests/Unit/Database/Repository/CacheRepositoryTest.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
// Copyright (C) 2010-2024, the Friendica project
|
||||
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Friendica\Test\Unit\Database\Repository;
|
||||
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Database\DatabaseException;
|
||||
use Friendica\Database\Repository\CacheRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Throwable;
|
||||
|
||||
class CacheRepositoryTest extends TestCase
|
||||
{
|
||||
public function testGetAllKeysValidUntilReturnsArray(): void
|
||||
{
|
||||
$stmt = new \stdClass;
|
||||
|
||||
$database = $this->createStub(Database::class);
|
||||
$database->method('select')->willReturnMap([
|
||||
['cache', ['k'], ['`expires` >= ?', '2025-04-16 10:12:01'], [], $stmt],
|
||||
]);
|
||||
$database->method('fetch')->willReturnOnConsecutiveCalls(
|
||||
['k' => 'value1'],
|
||||
['k' => 'value2'],
|
||||
['k' => 'value3'],
|
||||
false
|
||||
);
|
||||
|
||||
$repo = new CacheRepository($database);
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
'value1',
|
||||
'value2',
|
||||
'value3',
|
||||
],
|
||||
$repo->getAllKeysValidUntil('2025-04-16 10:12:01')
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetAllKeysValidUntilThrowsException(): void
|
||||
{
|
||||
$database = $this->createStub(Database::class);
|
||||
$database->method('select')->willThrowException($this->createStub(Throwable::class));
|
||||
|
||||
$repo = new CacheRepository($database);
|
||||
|
||||
$this->expectException(DatabaseException::class);
|
||||
|
||||
$repo->getAllKeysValidUntil('2025-04-16 10:12:01');
|
||||
}
|
||||
|
||||
public function testGetAllKeysValidUntilWithPrefixReturnsArray(): void
|
||||
{
|
||||
$stmt = new \stdClass;
|
||||
|
||||
$database = $this->createStub(Database::class);
|
||||
$database->method('select')->willReturnMap([
|
||||
['cache', ['k'], ['`expires` >= ?', '2025-04-16 10:12:01'], [], $stmt],
|
||||
]);
|
||||
$database->method('fetch')->willReturnOnConsecutiveCalls(
|
||||
['k' => 'value1'],
|
||||
['k' => 'value2'],
|
||||
['k' => 'value3'],
|
||||
false
|
||||
);
|
||||
|
||||
$repo = new CacheRepository($database);
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
'value1',
|
||||
'value2',
|
||||
'value3',
|
||||
],
|
||||
$repo->getAllKeysValidUntilWithPrefix('2025-04-16 10:12:01', 'prefix')
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetAllKeysValidUntilWithPrefixThrowsException(): void
|
||||
{
|
||||
$database = $this->createStub(Database::class);
|
||||
$database->method('select')->willThrowException($this->createStub(Throwable::class));
|
||||
|
||||
$repo = new CacheRepository($database);
|
||||
|
||||
$this->expectException(DatabaseException::class);
|
||||
|
||||
$repo->getAllKeysValidUntilWithPrefix('2025-04-16 10:12:01', 'prefix');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue