Using forked repo and cache engine
This commit is contained in:
parent
a0c60397af
commit
86f6c86c4c
16 changed files with 179 additions and 282 deletions
26
helpers/CacheEngines/JSONCache.php
Normal file
26
helpers/CacheEngines/JSONCache.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
namespace Helpers\CacheEngines;
|
||||
|
||||
class JSONCache {
|
||||
const CACHE_PATH = __DIR__ . '/../../cache/api/';
|
||||
public function get(string $cache_key): object|false {
|
||||
if (is_file(self::CACHE_PATH . $cache_key . '.json')) {
|
||||
$time = time();
|
||||
$json_string = file_get_contents(self::CACHE_PATH . $cache_key . '.json');
|
||||
$element = json_decode($json_string);
|
||||
if ($time < $element->expires) {
|
||||
return $element->data;
|
||||
}
|
||||
// Remove file if expired
|
||||
unlink(self::CACHE_PATH . $cache_key . '.json');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function set(string $cache_key, mixed $data, $timeout = 3600) {
|
||||
file_put_contents(self::CACHE_PATH . $cache_key . '.json', json_encode([
|
||||
'data' => $data,
|
||||
'expires' => time() + $timeout
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
|
@ -2,24 +2,11 @@
|
|||
namespace Helpers;
|
||||
|
||||
class Error {
|
||||
const list = [
|
||||
'api' => [
|
||||
'code' => 500,
|
||||
'message' => 'API unknown error, please check back later'
|
||||
],
|
||||
'latte' => [
|
||||
'code' => 500,
|
||||
'message' => 'Template render crash, please check back later'
|
||||
]
|
||||
];
|
||||
static public function show(object $meta) {
|
||||
$http_code = $meta->http_code;
|
||||
http_response_code($http_code);
|
||||
|
||||
static public function show(string $type) {
|
||||
$keys = array_keys(self::list);
|
||||
if (in_array($type, $keys)) {
|
||||
$error = self::list[$type];
|
||||
http_response_code($error['code']);
|
||||
$latte = Misc::latte();
|
||||
$latte->render(Misc::getView('error'), ['type' => $type, 'error' => $error]);
|
||||
}
|
||||
$latte = Misc::latte();
|
||||
$latte->render(Misc::getView('error'), ['error' => $meta]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
<?php
|
||||
namespace Helpers;
|
||||
|
||||
use Helpers\CacheEngines\JSONCache;
|
||||
use Helpers\Settings;
|
||||
|
||||
class Misc {
|
||||
|
|
@ -13,13 +15,22 @@ class Misc {
|
|||
|
||||
static public function api(): \Sovit\TikTok\Api {
|
||||
$options = [];
|
||||
$cacheEngine = false;
|
||||
// Proxy config
|
||||
if (in_array(Settings::PROXY, $_COOKIE)) {
|
||||
foreach (Settings::PROXY as $proxy_element) {
|
||||
$options[$proxy_element] = $_COOKIE[$proxy_element];
|
||||
foreach(Settings::PROXY as $proxy_element) {
|
||||
if (isset($_COOKIE[$proxy_element])) {
|
||||
$options['proxy'][$proxy_element] = $_COOKIE[$proxy_element];
|
||||
}
|
||||
}
|
||||
$api = new \Sovit\TikTok\Api($options);
|
||||
// Cache config
|
||||
if (isset($_ENV['APP_CACHE'])) {
|
||||
switch ($_ENV['APP_CACHE']) {
|
||||
case 'json':
|
||||
$cacheEngine = new JSONCache();
|
||||
break;
|
||||
}
|
||||
}
|
||||
$api = new \Sovit\TikTok\Api($options, $cacheEngine);
|
||||
return $api;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue