1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-08-05 08:07:33 +02:00

refacor: improve cache interface (#3492)

* fix: proper typehint on setScope

* refactor: type hint setKey()

* typehint
This commit is contained in:
Dag
2023-07-06 15:10:30 +02:00
committed by GitHub
parent f8801d8cb3
commit caac7f572c
13 changed files with 56 additions and 220 deletions

View File

@@ -410,11 +410,10 @@ abstract class BridgeAbstract implements BridgeInterface
/**
* Loads a cached value for the specified key
*
* @param string $key Key name
* @param int $duration Cache duration (optional)
* @return mixed Cached value or null if the key doesn't exist or has expired
*/
protected function loadCacheValue($key, $duration = null)
protected function loadCacheValue(string $key, $duration = null)
{
$cacheFactory = new CacheFactory();
@@ -422,7 +421,7 @@ abstract class BridgeAbstract implements BridgeInterface
// Create class name without the namespace part
$scope = $this->getShortName();
$cache->setScope($scope);
$cache->setKey($key);
$cache->setKey([$key]);
$timestamp = $cache->getTime();
if (
@@ -438,17 +437,16 @@ abstract class BridgeAbstract implements BridgeInterface
/**
* Stores a value to cache with the specified key
*
* @param string $key Key name
* @param mixed $value Value to cache
*/
protected function saveCacheValue($key, $value)
protected function saveCacheValue(string $key, $value)
{
$cacheFactory = new CacheFactory();
$cache = $cacheFactory->create();
$scope = $this->getShortName();
$cache->setScope($scope);
$cache->setKey($key);
$cache->setKey([$key]);
$cache->saveData($value);
}

View File

@@ -1,68 +1,16 @@
<?php
/**
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
* Atom feeds for websites that don't have one.
*
* For the full license information, please view the UNLICENSE file distributed
* with this source code.
*
* @package Core
* @license http://unlicense.org/ UNLICENSE
* @link https://github.com/rss-bridge/rss-bridge
*/
/**
* The cache interface
*/
interface CacheInterface
{
/**
* Set scope of the current cache
*
* If $scope is an empty string, the cache is set to a global context.
*
* @param string $scope The scope the data is related to
*/
public function setScope($scope);
public function setScope(string $scope): void;
/**
* Set key to assign the current data
*
* Since $key can be anything, the cache implementation must ensure to
* assign the related data reliably; most commonly by serializing and
* hashing the key in an appropriate way.
*
* @param array $key The key the data is related to
*/
public function setKey($key);
public function setKey(array $key): void;
/**
* Loads data from cache
*
* @return mixed The cached data or null
*/
public function loadData();
/**
* Stores data to the cache
*
* @param mixed $data The data to store
* @return self The cache object
*/
public function saveData($data);
public function saveData($data): void;
/**
* Returns the modification time of the current cache item.
* In unix timestamp.
* Example: 1688570578
*/
public function getTime(): ?int;
/**
* Removes any data that is older than the specified age from cache
*
* @param int $seconds The cache age in seconds
*/
public function purgeCache($seconds);
public function purgeCache(int $seconds): void;
}

View File

@@ -103,7 +103,7 @@ function getContents(
$cache = $cacheFactory->create();
$cache->setScope('server');
$cache->purgeCache(86400); // 24 hours (forced)
$cache->purgeCache(86400);
$cache->setKey([$url]);
// Snagged from https://github.com/lwthiker/curl-impersonate/blob/main/firefox/curl_ff102