caches: Refactor the API (#1060)

- For consistency, functions should always return null on non-existing data.

- WordPressPluginUpdateBridge appears to have used its own cache instance in the past. Obviously not used anymore.

- 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.

- Even though the default path for storage is perfectly fine, some people may want to use a different location. This is an example how a cache implementation is responsible for its requirements.
This commit is contained in:
fulmeek 2019-04-29 20:12:43 +02:00 committed by LogMANOriginal
parent 3b8f3da09d
commit 21d3bf3b60
8 changed files with 121 additions and 84 deletions

View file

@ -46,11 +46,11 @@ function getContents($url, $header = array(), $opts = array()){
// Initialize cache
$cache = Cache::create(Configuration::getConfig('cache', 'type'));
$cache->setPath(PATH_CACHE . 'server/');
$cache->setScope('server');
$cache->purgeCache(86400); // 24 hours (forced)
$params = [$url];
$cache->setParameters($params);
$cache->setKey($params);
// Use file_get_contents if in CLI mode with no root certificates defined
if(php_sapi_name() === 'cli' && empty(ini_get('curl.cainfo'))) {
@ -271,11 +271,11 @@ function getSimpleHTMLDOMCached($url,
// Initialize cache
$cache = Cache::create(Configuration::getConfig('cache', 'type'));
$cache->setPath(PATH_CACHE . 'pages/');
$cache->setScope('pages');
$cache->purgeCache(86400); // 24 hours (forced)
$params = [$url];
$cache->setParameters($params);
$cache->setKey($params);
// Determine if cached file is within duration
$time = $cache->getTime();