mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-01-17 06:08:27 +01:00
refactor: CacheFactory (#2834)
This commit is contained in:
parent
b7b9378484
commit
07927008eb
@ -92,7 +92,7 @@ class DisplayAction extends ActionAbstract {
|
||||
|
||||
// Initialize cache
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope('');
|
||||
$cache->purgeCache(86400); // 24 hours
|
||||
|
@ -123,7 +123,7 @@ class ElloBridge extends BridgeAbstract {
|
||||
|
||||
private function getAPIKey() {
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope(get_called_class());
|
||||
$cache->setKey(array('key'));
|
||||
|
@ -90,7 +90,7 @@ class InstagramBridge extends BridgeAbstract {
|
||||
if(is_numeric($username)) return $username;
|
||||
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope(get_called_class());
|
||||
$cache->setKey(array($username));
|
||||
|
@ -115,7 +115,7 @@ HTML;
|
||||
return;
|
||||
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$this->clientIDCache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$this->clientIDCache->setScope(get_called_class());
|
||||
$this->clientIDCache->setKey(array('client_id'));
|
||||
|
@ -88,7 +88,7 @@ class SpotifyBridge extends BridgeAbstract {
|
||||
|
||||
private function getToken() {
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope(get_called_class());
|
||||
$cache->setKey(array('token'));
|
||||
|
@ -487,7 +487,7 @@ EOD;
|
||||
private function getApiKey($forceNew = 0) {
|
||||
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$r_cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$r_cache->setScope(get_called_class());
|
||||
$r_cache->setKey(array('refresh'));
|
||||
@ -502,7 +502,7 @@ EOD;
|
||||
}
|
||||
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope(get_called_class());
|
||||
$cache->setKey(array('api_key'));
|
||||
@ -539,7 +539,7 @@ EOD;
|
||||
}
|
||||
|
||||
$cacheFac2 = new CacheFactory();
|
||||
$cacheFac2->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$gt_cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$gt_cache->setScope(get_called_class());
|
||||
$gt_cache->setKey(array('guest_token'));
|
||||
|
@ -390,7 +390,7 @@ abstract class BridgeAbstract implements BridgeInterface {
|
||||
*/
|
||||
protected function loadCacheValue($key, $duration = 86400){
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope(get_called_class());
|
||||
$cache->setKey($key);
|
||||
@ -407,7 +407,7 @@ abstract class BridgeAbstract implements BridgeInterface {
|
||||
*/
|
||||
protected function saveCacheValue($key, $value){
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope(get_called_class());
|
||||
$cache->setKey($key);
|
||||
|
@ -11,137 +11,57 @@
|
||||
* @link https://github.com/rss-bridge/rss-bridge
|
||||
*/
|
||||
|
||||
/**
|
||||
* Factory class responsible for creating cache objects from a given working
|
||||
* directory.
|
||||
*
|
||||
* This class is capable of:
|
||||
* - Locating cache classes in the specified working directory (see {@see Cache::$workingDir})
|
||||
* - Creating new cache instances based on the cache's name (see {@see Cache::create()})
|
||||
*
|
||||
* The following example illustrates the intended use for this class.
|
||||
*
|
||||
* ```PHP
|
||||
* require_once __DIR__ . '/rssbridge.php';
|
||||
*
|
||||
* // Step 1: Set the working directory
|
||||
* Cache::setWorkingDir(__DIR__ . '/../caches/');
|
||||
*
|
||||
* // Step 2: Create a new instance of a cache object (based on the name)
|
||||
* $cache = Cache::create('FileCache');
|
||||
* ```
|
||||
*/
|
||||
class CacheFactory extends FactoryAbstract {
|
||||
class CacheFactory
|
||||
{
|
||||
private $folder;
|
||||
private $cacheNames;
|
||||
|
||||
public function __construct(string $folder = PATH_LIB_CACHES)
|
||||
{
|
||||
$this->folder = $folder;
|
||||
// create cache names
|
||||
foreach(scandir($this->folder) as $file) {
|
||||
if(preg_match('/^([^.]+)Cache\.php$/U', $file, $m)) {
|
||||
$this->cacheNames[] = $m[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new cache object from the working directory.
|
||||
*
|
||||
* @throws \InvalidArgumentException if the requested cache name is invalid.
|
||||
* @throws \Exception if the requested cache file doesn't exist in the
|
||||
* working directory.
|
||||
* @param string $name Name of the cache object.
|
||||
* @return object|bool The cache object or false if the class is not instantiable.
|
||||
* @param string $name The name of the cache e.g. "File", "Memcached" or "SQLite"
|
||||
*/
|
||||
public function create($name){
|
||||
public function create(string $name): CacheInterface
|
||||
{
|
||||
$name = $this->sanitizeCacheName($name) . 'Cache';
|
||||
|
||||
if(!$this->isCacheName($name)) {
|
||||
if(! preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {
|
||||
throw new \InvalidArgumentException('Cache name invalid!');
|
||||
}
|
||||
|
||||
$filePath = $this->getWorkingDir() . $name . '.php';
|
||||
|
||||
$filePath = $this->folder . $name . '.php';
|
||||
if(!file_exists($filePath)) {
|
||||
throw new \Exception('Cache file ' . $filePath . ' does not exist!');
|
||||
throw new \Exception('Invalid cache');
|
||||
}
|
||||
|
||||
if((new \ReflectionClass($name))->isInstantiable()) {
|
||||
return new $name();
|
||||
}
|
||||
|
||||
return false;
|
||||
$className = '\\' . $name;
|
||||
return new $className();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the provided name is a valid cache name.
|
||||
*
|
||||
* A valid cache name starts with a capital letter ([A-Z]), followed by
|
||||
* zero or more alphanumeric characters or hyphen ([A-Za-z0-9-]).
|
||||
*
|
||||
* @param string $name The cache name.
|
||||
* @return bool true if the name is a valid cache name, false otherwise.
|
||||
*/
|
||||
public function isCacheName($name){
|
||||
return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of cache names from the working directory.
|
||||
*
|
||||
* The list is cached internally to allow for successive calls.
|
||||
*
|
||||
* @return array List of cache names
|
||||
*/
|
||||
public function getCacheNames(){
|
||||
|
||||
static $cacheNames = array(); // Initialized on first call
|
||||
|
||||
if(empty($cacheNames)) {
|
||||
$files = scandir($this->getWorkingDir());
|
||||
|
||||
if($files !== false) {
|
||||
foreach($files as $file) {
|
||||
if(preg_match('/^([^.]+)Cache\.php$/U', $file, $out)) {
|
||||
$cacheNames[] = $out[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
protected function sanitizeCacheName(string $name)
|
||||
{
|
||||
// Trim trailing '.php' if exists
|
||||
if (preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
||||
$name = $matches[1];
|
||||
}
|
||||
|
||||
return $cacheNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sanitized cache name.
|
||||
*
|
||||
* The cache name can be specified in various ways:
|
||||
* * The PHP file name (i.e. `FileCache.php`)
|
||||
* * The PHP file name without file extension (i.e. `FileCache`)
|
||||
* * The cache name (i.e. `file`)
|
||||
*
|
||||
* Casing is ignored (i.e. `FILE` and `fIlE` are the same).
|
||||
*
|
||||
* A cache file matching the given cache name must exist in the working
|
||||
* directory!
|
||||
*
|
||||
* @param string $name The cache name
|
||||
* @return string|null The sanitized cache name if the provided name is
|
||||
* valid, null otherwise.
|
||||
*/
|
||||
protected function sanitizeCacheName($name) {
|
||||
|
||||
if(is_string($name)) {
|
||||
|
||||
// Trim trailing '.php' if exists
|
||||
if(preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
||||
$name = $matches[1];
|
||||
}
|
||||
|
||||
// Trim trailing 'Cache' if exists
|
||||
if(preg_match('/(.+)(?:Cache)$/i', $name, $matches)) {
|
||||
$name = $matches[1];
|
||||
}
|
||||
|
||||
// The name is valid if a corresponding file is found on disk
|
||||
if(in_array(strtolower($name), array_map('strtolower', $this->getCacheNames()))) {
|
||||
$index = array_search(strtolower($name), array_map('strtolower', $this->getCacheNames()));
|
||||
return $this->getCacheNames()[$index];
|
||||
}
|
||||
|
||||
Debug::log('Invalid cache name specified: "' . $name . '"!');
|
||||
|
||||
// Trim trailing 'Cache' if exists
|
||||
if (preg_match('/(.+)(?:Cache)$/i', $name, $matches)) {
|
||||
$name = $matches[1];
|
||||
}
|
||||
|
||||
return null; // Bad parameter
|
||||
|
||||
if(in_array(strtolower($name), array_map('strtolower', $this->cacheNames))) {
|
||||
$index = array_search(strtolower($name), array_map('strtolower', $this->cacheNames));
|
||||
return $this->cacheNames[$index];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ function getContents(
|
||||
bool $returnFull = false
|
||||
) {
|
||||
$cacheFactory = new CacheFactory();
|
||||
$cacheFactory->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFactory->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope('server');
|
||||
$cache->purgeCache(86400); // 24 hours (forced)
|
||||
@ -312,7 +312,7 @@ function getSimpleHTMLDOMCached($url,
|
||||
|
||||
// Initialize cache
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope('pages');
|
||||
$cache->purgeCache(86400); // 24 hours (forced)
|
||||
|
@ -52,7 +52,6 @@ function returnServerError($message){
|
||||
*/
|
||||
function logBridgeError($bridgeName, $code) {
|
||||
$cacheFac = new CacheFactory();
|
||||
$cacheFac->setWorkingDir(PATH_LIB_CACHES);
|
||||
|
||||
$cache = $cacheFac->create(Configuration::getConfig('cache', 'type'));
|
||||
$cache->setScope('error_reporting');
|
||||
|
Loading…
x
Reference in New Issue
Block a user