Skip to content

Commit

Permalink
core: Add support for custom cache types via config.ini.php
Browse files Browse the repository at this point in the history
This commit adds support for a new parameter which specifies the type
of cache to use for caching. It is specified in config.ini.php:

 [cache]

 type = "..."

Currently only one type of cache is supported (see /caches). All uses
of 'FileCache' were replaced by this configuration option.

Note: Caching currently depends on files and folders (due to FileCache).
Experience may vary depending on the selected cache type. For now always
check if FileCache is working before testing alternative types.

References #1000
  • Loading branch information
logmanoriginal committed Feb 6, 2019
1 parent 51ee541 commit 556a417
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function execute() {
);

// Initialize cache
$cache = Cache::create('FileCache');
$cache = Cache::create(Configuration::getConfig('cache', 'type'));
$cache->setPath(PATH_CACHE);
$cache->purgeCache(86400); // 24 hours
$cache->setParameters($cache_params);
Expand Down
2 changes: 1 addition & 1 deletion bridges/ElloBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private function getUsername($post, $postData) {
}

private function getAPIKey() {
$cache = Cache::create('FileCache');
$cache = Cache::create(Configuration::getConfig('cache', 'type'));
$cache->setPath(PATH_CACHE);
$cache->setParameters(['key']);
$key = $cache->loadData();
Expand Down
2 changes: 1 addition & 1 deletion bridges/WordPressPluginUpdateBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function getName(){
private function getCachedDate($url){
Debug::log('getting pubdate from url ' . $url . '');
// Initialize cache
$cache = Cache::create('FileCache');
$cache = Cache::create(Configuration::getConfig('cache', 'type'));
$cache->setPath(PATH_CACHE . 'pages/');
$params = [$url];
$cache->setParameters($params);
Expand Down
4 changes: 4 additions & 0 deletions config.default.ini.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

[cache]

; Defines the cache type used by RSS-Bridge
; "file" = FileCache (default)
type = "file"

; Allow users to specify custom timeout for specific requests.
; true = enabled
; false = disabled (default)
Expand Down
73 changes: 73 additions & 0 deletions lib/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public function __construct(){
* @return object|bool The cache object or false if the class is not instantiable.
*/
public static function create($name){
$name = self::sanitizeCacheName($name) . 'Cache';

if(!self::isCacheName($name)) {
throw new \InvalidArgumentException('Cache name invalid!');
}
Expand Down Expand Up @@ -137,4 +139,75 @@ public static function getWorkingDir(){
public static 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 static function getCacheNames(){

static $cacheNames = array(); // Initialized on first call

if(empty($cacheNames)) {
$files = scandir(self::getWorkingDir());

if($files !== false) {
foreach($files as $file) {
if(preg_match('/^([^.]+)Cache\.php$/U', $file, $out)) {
$cacheNames[] = $out[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 static 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', self::getCacheNames()))) {
$index = array_search(strtolower($name), array_map('strtolower', self::getCacheNames()));
return self::getCacheNames()[$index];
}

Debug::log('Invalid cache name specified: "' . $name . '"!');

}

return null; // Bad parameter

}
}
3 changes: 3 additions & 0 deletions lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public static function loadConfiguration() {
/** Name of the proxy server */
define('PROXY_NAME', self::getConfig('proxy', 'name'));

if(!is_string(self::getConfig('cache', 'type')))
die('Parameter [cache] => "type" is not a valid string! Please check "config.ini.php"!');

if(!is_bool(self::getConfig('cache', 'custom_timeout')))
die('Parameter [cache] => "custom_timeout" is not a valid Boolean! Please check "config.ini.php"!');

Expand Down
4 changes: 2 additions & 2 deletions lib/contents.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function getContents($url, $header = array(), $opts = array()){
Debug::log('Reading contents from "' . $url . '"');

// Initialize cache
$cache = Cache::create('FileCache');
$cache = Cache::create(Configuration::getConfig('cache', 'type'));
$cache->setPath(PATH_CACHE . 'server/');
$cache->purgeCache(86400); // 24 hours (forced)

Expand Down Expand Up @@ -268,7 +268,7 @@ function getSimpleHTMLDOMCached($url,
Debug::log('Caching url ' . $url . ', duration ' . $duration);

// Initialize cache
$cache = Cache::create('FileCache');
$cache = Cache::create(Configuration::getConfig('cache', 'type'));
$cache->setPath(PATH_CACHE . 'pages/');
$cache->purgeCache(86400); // 24 hours (forced)

Expand Down

0 comments on commit 556a417

Please sign in to comment.