mirror of
https://github.com/scummvm/scummvm-web.git
synced 2026-05-21 05:40:47 +00:00
47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
namespace ScummVM\Models;
|
|
|
|
use Phpfastcache\Helper\Psr16Adapter;
|
|
use Phpfastcache\Drivers\Redis\Config;
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverConnectException;
|
|
|
|
abstract class BasicModel
|
|
{
|
|
protected static $cache;
|
|
|
|
public function __construct()
|
|
{
|
|
if (is_null(self::$cache)) {
|
|
try {
|
|
$database = $_SERVER['HTTP_HOST'] === 'www.scummvm.org' ? 8 : 7;
|
|
$config = new Config(['database' => $database]);
|
|
self::$cache = new Psr16Adapter('redis', $config);
|
|
} catch (PhpfastcacheDriverConnectException $ex) {
|
|
// Fallback to files based cache
|
|
self::$cache = new Psr16Adapter('files');
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function saveToCache($data, $key = '')
|
|
{
|
|
if ($key) {
|
|
$key = "_$key";
|
|
}
|
|
global $lang;
|
|
$cacheKey = str_replace("\\", "_", \get_called_class() . $key . "_$lang");
|
|
self::$cache->set($cacheKey, $data, 3600);
|
|
}
|
|
|
|
protected function getFromCache($key = '')
|
|
{
|
|
if ($key) {
|
|
$key = "_$key";
|
|
}
|
|
global $lang;
|
|
$cacheKey = str_replace("\\", "_", \get_called_class() . $key . "_$lang");
|
|
$cachedData = self::$cache->get($cacheKey);
|
|
return $cachedData;
|
|
}
|
|
}
|