diff --git a/jumpapp/api/unsplashdata.php b/jumpapp/api/unsplashdata.php index ffa8873..f146e70 100644 --- a/jumpapp/api/unsplashdata.php +++ b/jumpapp/api/unsplashdata.php @@ -14,10 +14,10 @@ $cache = new Jump\Cache($config); // If this script is run via CLI then clear the cache and repopulate it, // otherwise if run via web then get image data from cache and run this -// script asynchronously to refresh the cahce for next time. +// script asynchronously to refresh the cache for next time. if (http_response_code() === false) { - $cache->clear('unsplash'); - load_cache_unsplash_data(); + $unsplashdata = load_cache_unsplash_data($config); + $cache->save(cachename: 'unsplash', data: $unsplashdata); die('Cached data from Unsplash'); } @@ -45,44 +45,46 @@ if (!$token || !hash_equals($csrfsection->get('token'), $token)) { die(json_encode(['error' => 'API token is incorrect or missing'])); } -$unsplashdata = load_cache_unsplash_data(); +$unsplashdata = $cache->load(cachename: 'unsplash'); +if ($unsplashdata == null) { + $unsplashdata = load_cache_unsplash_data($config); + $cache->save(cachename: 'unsplash', data: $unsplashdata); +} + echo json_encode($unsplashdata); shell_exec('/usr/bin/nohup /usr/bin/php -f unsplashdata.php >/dev/null 2>&1 &'); -function load_cache_unsplash_data() { - global $cache, $config; - return $cache->load(cachename: 'unsplash', callback: function() use ($config) { - Crew\Unsplash\HttpClient::init([ - 'utmSource' => 'jump_startpage', - 'applicationId' => $config->get('unsplashapikey'), +function load_cache_unsplash_data($config) { + Crew\Unsplash\HttpClient::init([ + 'utmSource' => 'jump_startpage', + 'applicationId' => $config->get('unsplashapikey'), + ]); + // Try to get a random image via the API. + try { + $photo = Crew\Unsplash\Photo::random([ + 'collections' => $config->get('unsplashcollections', false), ]); - // Try to get a random image via the API. - try { - $photo = Crew\Unsplash\Photo::random([ - 'collections' => $config->get('unsplashcollections', false), - ]); - } catch (Exception $e) { - http_response_code(500); - die(json_encode(['error' => json_decode($e->getMessage())])); - } - // Download the image data from Unsplash. - $ch = curl_init(); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_URL, $photo->urls['raw'].'&auto=compress&w=1920'); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); - curl_setopt($ch, CURLOPT_FAILONERROR, true); - $response = curl_exec($ch); - // Create the response and return it. - $description = 'Photo'; - if ($photo->description !== null && - strlen($photo->description) <= 45) { - $description = $photo->description; - } - $unsplashdata = new stdClass(); - $unsplashdata->color = $photo->color; - $unsplashdata->attribution = ''.$description.' by '.$photo->user['name'].''; - $unsplashdata->imagedatauri = 'data: '.(new finfo(FILEINFO_MIME_TYPE))->buffer($response).';base64,'.base64_encode($response); - return $unsplashdata; - }); + } catch (Exception $e) { + http_response_code(500); + die(json_encode(['error' => json_decode($e->getMessage())])); + } + // Download the image data from Unsplash. + $ch = curl_init(); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_URL, $photo->urls['raw'].'&auto=compress&w=1920'); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_FAILONERROR, true); + $response = curl_exec($ch); + // Create the response and return it. + $description = 'Photo'; + if ($photo->description !== null && + strlen($photo->description) <= 45) { + $description = $photo->description; + } + $unsplashdata = new stdClass(); + $unsplashdata->color = $photo->color; + $unsplashdata->attribution = ''.$description.' by '.$photo->user['name'].''; + $unsplashdata->imagedatauri = 'data: '.(new finfo(FILEINFO_MIME_TYPE))->buffer($response).';base64,'.base64_encode($response); + return $unsplashdata; } diff --git a/jumpapp/classes/Cache.php b/jumpapp/classes/Cache.php index 9a24b78..9ea3b2e 100644 --- a/jumpapp/classes/Cache.php +++ b/jumpapp/classes/Cache.php @@ -124,6 +124,21 @@ class Cache { ); } + /** + * Save data into the specified cache item. + * + * @param string $cachename The name of a cache, must match a key in $caches definition. + * @param string|null $key A key used to represent an object within a cache. + * @param mixed $data + * @return void + */ + public function save(string $cachename, ?string $key = 'default', mixed $data) { + $this->init_cache($cachename, $key); + $dependencies = [$this->caches[$cachename]['expirationtype'] => $this->caches[$cachename]['expirationparams']]; + // Retrieve the initialised cache object from $caches. + return $this->caches[$cachename]['cache'][$key]->save($cachename.'/'.$key, $data, $dependencies); + } + /** * Remove the specified item from the cache. *