2017-01-13 16:53:56 -05:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
/**
|
2017-02-15 16:13:32 -05:00
|
|
|
* Hummingbird Anime List Client
|
2017-01-13 16:53:56 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2017-01-13 16:53:56 -05:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2017-01-13 16:53:56 -05:00
|
|
|
*
|
2017-02-15 16:13:32 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2017-01-13 16:53:56 -05:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2021-01-13 01:52:03 -05:00
|
|
|
* @copyright 2015 - 2021 Timothy J. Warren
|
2017-01-13 16:53:56 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2017-01-13 16:53:56 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\AnimeClient\API;
|
|
|
|
|
2020-05-08 19:15:21 -04:00
|
|
|
use Psr\SimpleCache\CacheInterface;
|
|
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
2017-01-13 16:53:56 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper methods for dealing with the Cache
|
|
|
|
*/
|
|
|
|
trait CacheTrait {
|
2018-01-18 16:21:45 -05:00
|
|
|
|
2017-01-13 16:53:56 -05:00
|
|
|
/**
|
2020-05-08 19:15:21 -04:00
|
|
|
* @var CacheInterface
|
2017-01-13 16:53:56 -05:00
|
|
|
*/
|
2020-05-08 19:15:21 -04:00
|
|
|
protected CacheInterface $cache;
|
2018-01-18 16:21:45 -05:00
|
|
|
|
2017-01-13 16:53:56 -05:00
|
|
|
/**
|
|
|
|
* Inject the cache object
|
|
|
|
*
|
2020-05-08 19:15:21 -04:00
|
|
|
* @param CacheInterface $cache
|
2017-01-13 16:53:56 -05:00
|
|
|
* @return $this
|
|
|
|
*/
|
2020-05-08 19:15:21 -04:00
|
|
|
public function setCache(CacheInterface $cache): self
|
2017-01-13 16:53:56 -05:00
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
return $this;
|
|
|
|
}
|
2018-01-18 16:21:45 -05:00
|
|
|
|
2017-01-16 11:26:19 -05:00
|
|
|
/**
|
|
|
|
* Get the cache object if it exists
|
|
|
|
*
|
2020-05-08 19:15:21 -04:00
|
|
|
* @return CacheInterface
|
2017-01-16 11:26:19 -05:00
|
|
|
*/
|
2020-05-08 19:15:21 -04:00
|
|
|
public function getCache(): CacheInterface
|
2017-01-16 11:26:19 -05:00
|
|
|
{
|
|
|
|
return $this->cache;
|
|
|
|
}
|
2018-01-18 16:21:45 -05:00
|
|
|
|
2020-05-08 19:15:21 -04:00
|
|
|
/**
|
|
|
|
* Get the cached value if it exists, otherwise set the cache value
|
|
|
|
* and return it.
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param callable $primer
|
2020-10-21 18:12:22 -04:00
|
|
|
* @param array|null $primeArgs
|
2021-02-12 13:09:57 -05:00
|
|
|
* @return mixed
|
2020-05-08 19:15:21 -04:00
|
|
|
*/
|
2021-02-12 13:09:57 -05:00
|
|
|
public function getCached(string $key, callable $primer, ?array $primeArgs = []): mixed
|
2020-05-08 19:15:21 -04:00
|
|
|
{
|
2021-02-16 14:43:51 -05:00
|
|
|
$value = $this->cache->get($key);
|
2020-05-08 19:15:21 -04:00
|
|
|
|
|
|
|
if ($value === NULL)
|
|
|
|
{
|
2021-02-16 14:43:51 -05:00
|
|
|
$primeArgs ??= [];
|
2020-05-08 19:15:21 -04:00
|
|
|
$value = $primer(...$primeArgs);
|
|
|
|
if ($value === NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cache->set($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2017-01-13 16:53:56 -05:00
|
|
|
}
|