Added Redis cache
This commit is contained in:
parent
b7563d3ee9
commit
282ad20a6b
|
@ -1,2 +1,7 @@
|
||||||
APP_SUBDIR=/ # Subpath your app is running, defaults to /
|
# APP_SUBDIR=/ # Subpath your app is running, defaults to /
|
||||||
APP_CACHE=json # Cache engine for TikTok Api, defaults to json (more info on README)
|
# APP_CACHE=json # Cache engine for TikTok Api, defaults to json (more info on README)
|
||||||
|
|
||||||
|
# Redis, used on Helpers\CahceEngines\RedisCache (CHOOSE ONE)
|
||||||
|
|
||||||
|
# REDIS_URL=redis://:password@host:port # If using password
|
||||||
|
# REDIS_URL=redis://host:port # If not using password
|
||||||
|
|
|
@ -28,6 +28,7 @@ Move the .env.example file to .env and modify it.
|
||||||
|
|
||||||
### Cache engine
|
### Cache engine
|
||||||
Available cache engines:
|
Available cache engines:
|
||||||
|
* redis: Writes response to Redis (check .env.example for config!)
|
||||||
* json: Writes response to JSON file
|
* json: Writes response to JSON file
|
||||||
|
|
||||||
### Apache
|
### Apache
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"ext-curl": "*",
|
"ext-redis": "*",
|
||||||
"ssovit/tiktok-api": "dev-rework",
|
"ssovit/tiktok-api": "dev-rework",
|
||||||
"steampixel/simple-php-router": "^0.7.0",
|
"steampixel/simple-php-router": "^0.7.0",
|
||||||
"latte/latte": "^2.10",
|
"latte/latte": "^2.10",
|
||||||
|
|
31
helpers/CacheEngines/RedisCache.php
Normal file
31
helpers/CacheEngines/RedisCache.php
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
namespace Helpers\CacheEngines;
|
||||||
|
|
||||||
|
use \Redis;
|
||||||
|
|
||||||
|
class RedisCache {
|
||||||
|
private \Redis $client;
|
||||||
|
function __construct(string $host, int $port, ?string $password) {
|
||||||
|
$this->client = new Redis();
|
||||||
|
$this->client->connect($host, $port);
|
||||||
|
if ($password) {
|
||||||
|
$this->client->auth($password);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function __destruct() {
|
||||||
|
$this->client->close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get(string $cache_key): object|false {
|
||||||
|
if ($this->client->exists($cache_key)) {
|
||||||
|
$data_string = $this->client->get($cache_key);
|
||||||
|
return json_decode($data_string);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function set(string $cache_key, mixed $data, $timeout = 3600) {
|
||||||
|
$this->client->set($cache_key, json_encode($data), $timeout);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Helpers;
|
namespace Helpers;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Helpers\CacheEngines\JSONCache;
|
use Helpers\CacheEngines\JSONCache;
|
||||||
|
use Helpers\CacheEngines\RedisCache;
|
||||||
use Helpers\Settings;
|
use Helpers\Settings;
|
||||||
|
|
||||||
class Misc {
|
class Misc {
|
||||||
|
@ -28,6 +30,17 @@ class Misc {
|
||||||
case 'json':
|
case 'json':
|
||||||
$cacheEngine = new JSONCache();
|
$cacheEngine = new JSONCache();
|
||||||
break;
|
break;
|
||||||
|
case 'redis':
|
||||||
|
if (!isset($_ENV['REDIS_URL'])) {
|
||||||
|
throw new Exception('You need to set REDIS_URL to use Redis Cache!');
|
||||||
|
}
|
||||||
|
|
||||||
|
$url = parse_url($_ENV['REDIS_URL']);
|
||||||
|
$host = $url['host'];
|
||||||
|
$port = $url['port'];
|
||||||
|
$password = $url['pass'] ?? null;
|
||||||
|
$cacheEngine = new RedisCache($host, $port, $password);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$api = new \Sovit\TikTok\Api($options, $cacheEngine);
|
$api = new \Sovit\TikTok\Api($options, $cacheEngine);
|
||||||
|
|
Loading…
Reference in a new issue