Added apcu caching method and bump
This commit is contained in:
parent
faf1dcee51
commit
d273b35e53
5 changed files with 38 additions and 8 deletions
25
app/Cache/ApcuCache.php
Normal file
25
app/Cache/ApcuCache.php
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
namespace App\Cache;
|
||||
|
||||
use TikScraper\Interfaces\CacheInterface;
|
||||
|
||||
class ApcuCache implements CacheInterface {
|
||||
function __construct() {
|
||||
if (!(extension_loaded('apcu') && apcu_enabled())) {
|
||||
throw new \Exception('APCu not enabled');
|
||||
}
|
||||
}
|
||||
|
||||
public function get(string $cache_key): ?object {
|
||||
$data = apcu_fetch($cache_key);
|
||||
return $data !== false ? json_decode($data) : null;
|
||||
}
|
||||
|
||||
public function exists(string $cache_key): bool {
|
||||
return apcu_exists($cache_key);
|
||||
}
|
||||
|
||||
public function set(string $cache_key, string $data, $timeout = 3600): void {
|
||||
apcu_store($cache_key, $data, $timeout);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,5 +3,6 @@ namespace App\Constants;
|
|||
|
||||
abstract class CacheMethods {
|
||||
const JSON = 'json';
|
||||
const APCU = 'apcu';
|
||||
const REDIS = 'redis';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
namespace App\Helpers;
|
||||
|
||||
use App\Cache\ApcuCache;
|
||||
use App\Cache\JSONCache;
|
||||
use App\Cache\RedisCache;
|
||||
use App\Constants\CacheMethods;
|
||||
|
|
@ -138,6 +139,9 @@ class Wrappers {
|
|||
case CacheMethods::JSON:
|
||||
$cacheEngine = new JSONCache();
|
||||
break;
|
||||
case CacheMethods::APCU:
|
||||
$cacheEngine = new ApcuCache();
|
||||
break;
|
||||
case CacheMethods::REDIS:
|
||||
if (!(isset($_ENV['REDIS_URL']) || isset($_ENV['REDIS_HOST'], $_ENV['REDIS_PORT']))) {
|
||||
throw new \Exception('You need to set REDIS_URL or REDIS_HOST and REDIS_PORT to use Redis Cache!');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue