1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-07-30 21:30:14 +02:00

refactor: logger (#3678)

This commit is contained in:
Dag
2023-09-21 22:05:55 +02:00
committed by GitHub
parent 360f953be8
commit 7329b83cc0
30 changed files with 297 additions and 338 deletions

View File

@@ -4,10 +4,14 @@ declare(strict_types=1);
class FileCache implements CacheInterface
{
private Logger $logger;
private array $config;
public function __construct(array $config = [])
{
public function __construct(
Logger $logger,
array $config = []
) {
$this->logger = $logger;
$default = [
'path' => null,
'enable_purge' => true,
@@ -28,7 +32,7 @@ class FileCache implements CacheInterface
}
$item = unserialize(file_get_contents($cacheFile));
if ($item === false) {
Logger::warning(sprintf('Failed to unserialize: %s', $cacheFile));
$this->logger->warning(sprintf('Failed to unserialize: %s', $cacheFile));
$this->delete($key);
return $default;
}

View File

@@ -4,10 +4,15 @@ declare(strict_types=1);
class MemcachedCache implements CacheInterface
{
private Logger $logger;
private \Memcached $conn;
public function __construct(string $host, int $port)
{
public function __construct(
Logger $logger,
string $host,
int $port
) {
$this->logger = $logger;
$this->conn = new \Memcached();
// This call does not actually connect to server yet
if (!$this->conn->addServer($host, $port)) {
@@ -29,7 +34,7 @@ class MemcachedCache implements CacheInterface
$expiration = $ttl === null ? 0 : time() + $ttl;
$result = $this->conn->set($key, $value, $expiration);
if ($result === false) {
Logger::warning('Failed to store an item in memcached', [
$this->logger->warning('Failed to store an item in memcached', [
'key' => $key,
'code' => $this->conn->getLastErrorCode(),
'message' => $this->conn->getLastErrorMessage(),

View File

@@ -8,11 +8,15 @@ declare(strict_types=1);
*/
class SQLiteCache implements CacheInterface
{
private \SQLite3 $db;
private Logger $logger;
private array $config;
private \SQLite3 $db;
public function __construct(array $config)
{
public function __construct(
Logger $logger,
array $config
) {
$this->logger = $logger;
$default = [
'file' => null,
'timeout' => 5000,
@@ -59,7 +63,7 @@ class SQLiteCache implements CacheInterface
$blob = $row['value'];
$value = unserialize($blob);
if ($value === false) {
Logger::error(sprintf("Failed to unserialize: '%s'", mb_substr($blob, 0, 100)));
$this->logger->error(sprintf("Failed to unserialize: '%s'", mb_substr($blob, 0, 100)));
// delete?
return $default;
}
@@ -68,6 +72,7 @@ class SQLiteCache implements CacheInterface
// delete?
return $default;
}
public function set(string $key, $value, int $ttl = null): void
{
$cacheKey = $this->createCacheKey($key);