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
|
|
|
*
|
2020-04-10 15:39:39 -04:00
|
|
|
* PHP version 7.4
|
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>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2017-01-13 16:53:56 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-08-04 09:30:21 -04:00
|
|
|
* @version 5.1
|
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
|
2020-05-08 19:15:21 -04:00
|
|
|
* @return mixed|null
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function getCached(string $key, callable $primer, ?array $primeArgs = [])
|
|
|
|
{
|
|
|
|
$value = $this->cache->get($key, NULL);
|
|
|
|
|
|
|
|
if ($value === NULL)
|
|
|
|
{
|
|
|
|
$value = $primer(...$primeArgs);
|
|
|
|
if ($value === NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cache->set($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2017-01-13 16:53:56 -05:00
|
|
|
}
|