Add Unsplash background image support

This commit is contained in:
Dale Davies
2022-06-03 22:41:28 +01:00
parent 395c17ecdd
commit ad322ba0c0
13 changed files with 1030 additions and 26 deletions

View File

@@ -60,6 +60,11 @@ class Cache {
$config->get('templatedir').'/errorpage.mustache'
]
],
'unsplash' => [
'cache' => null,
'expirationtype' => Caching\Cache::EXPIRE,
'expirationparams' => '5 minutes'
],
'weatherdata' => [
'cache' => null,
'expirationtype' => Caching\Cache::EXPIRE,
@@ -71,19 +76,13 @@ class Cache {
}
/**
* Read the specified item from the cache or generate it, mostly a wrapper
* around Nette\Caching\Cache::load().
* Validate and inititalise the specified cache.
*
* @param string $cachename The name of a cache, must match a key in $caches definition.
* @param string $key A key used to represent an object within a cache,
* @param callable $callback The code from which the result should be stored in cache.
* @return mixed The result of callback function retreieved from cache.
* @param string $key A key used to represent an object within a cache.
* @return void
*/
public function load(string $cachename, ?string $key = 'default', callable $callback): mixed {
// If cachebypass has been set in config.php then just execute the callback.
if ($this->config->parse_bool($this->config->get('cachebypass'))) {
return $callback();
}
private function init_cache(string $cachename, ?string $key = 'default'): void {
// We can only work with caches that have already been defined.
if (!array_key_exists($cachename, $this->caches)) {
throw new \Exception('Cache name not found ('.$cachename.')');
@@ -92,6 +91,23 @@ class Cache {
if (!isset($this->caches[$cachename]['cache']) || !array_key_exists($key, $this->caches[$cachename]['cache'])) {
$this->caches[$cachename]['cache'][$key] = new Caching\Cache($this->storage, $cachename.'/'.$key);
}
}
/**
* Read the specified item from the cache or generate it, mostly a wrapper
* around Nette\Caching\Cache::load().
*
* @param string $cachename The name of a cache, must match a key in $caches definition.
* @param string $key A key used to represent an object within a cache.
* @param callable $callback The code from which the result should be stored in cache.
* @return mixed The result of callback function retreieved from cache.
*/
public function load(string $cachename, ?string $key = 'default', callable $callback): mixed {
// If cachebypass has been set in config.php then just execute the callback.
if ($this->config->parse_bool($this->config->get('cachebypass'))) {
return $callback();
}
$this->init_cache($cachename, $key);
// Retrieve the initialised cache object from $caches, defines the caches expiry
// and executes the callback.
return $this->caches[$cachename]['cache'][$key]->load($cachename.'/'.$key,
@@ -102,4 +118,15 @@ class Cache {
);
}
/**
* Remove the specified item from the cache.
*
* @param string $cachename The name of a cache, must match a key in $caches definition.
* @param string $key A key used to represent an object within a cache.
* @return void
*/
public function clear(string $cachename, ?string $key = 'default'): void {
$this->init_cache($cachename, $key);
$this->caches[$cachename]['cache'][$key]->remove($cachename.'/'.$key);
}
}