144 lines
3.5 KiB
PHP
144 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Redis Cache
|
|
* Version: 2.1.1
|
|
* Copyright (c) 2020-2022. Mateusz Szymański Teamwant
|
|
* https://teamwant.pl
|
|
*
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Open Software License (OSL 3.0)
|
|
* that is bundled with this package in the file LICENSE.txt.
|
|
* It is also available through the world-wide-web at this URL:
|
|
* http://opensource.org/licenses/osl-3.0.php
|
|
*
|
|
* @author Teamwant <kontakt@teamwant.pl>
|
|
* @copyright Copyright 2020-2023 © Teamwant Mateusz Szymański All right reserved
|
|
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
|
|
* @category Teamwant
|
|
* @package Teamwant
|
|
*/
|
|
|
|
namespace Teamwant\Prestashop17\Redis\Classes\Cache;
|
|
|
|
use InvalidArgumentException;
|
|
use Predis\Client;
|
|
use Predis\Connection\ConnectionException;
|
|
use Predis\Response\ServerException;
|
|
use Throwable;
|
|
|
|
class Redis extends CacheRedis
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
const REDIS_HEALTHCHECK_KEY = "TW_REDIS_HEALTHCHECKKEY";
|
|
|
|
public static function testConnection($scheme, $host, $port, $alias = null, $username = null, $password = null, $database = null)
|
|
{
|
|
if (class_exists('\Predis\Client')) {
|
|
$row = [
|
|
'scheme' => $scheme,
|
|
'host' => $host,
|
|
'port' => $port,
|
|
'alias' => 'first-node'
|
|
];
|
|
|
|
if ($alias) {
|
|
$row['alias'] = $alias;
|
|
}
|
|
|
|
if ($username) {
|
|
$row['username'] = $username;
|
|
}
|
|
|
|
if ($password) {
|
|
$row['password'] = $password;
|
|
}
|
|
|
|
if ($database) {
|
|
$row['database'] = (int)$database;
|
|
}
|
|
|
|
try {
|
|
if ($row['scheme'] === 'unix') {
|
|
$row['path'] = $row['host'];
|
|
unset($row['host']);
|
|
}
|
|
|
|
$client = new Client($row);
|
|
$client->connect();
|
|
|
|
if ($client->isConnected()) {
|
|
$uniqid = uniqid();
|
|
$result = $client->set($uniqid, serialize(['k' => 'v']), 'ex', strtotime('now +1min'));
|
|
if ((string)$result === 'OK') {
|
|
return true;
|
|
}
|
|
}
|
|
} catch (ConnectionException $e) {
|
|
return false;
|
|
} catch (InvalidArgumentException $e) {
|
|
return false;
|
|
} catch (ServerException $e) {
|
|
return false;
|
|
} catch (Throwable $e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function delete($key)
|
|
{
|
|
if (!$this->is_connected) {
|
|
return false;
|
|
}
|
|
|
|
if ($key === '*') {
|
|
$this->_flush();
|
|
} else {
|
|
$this->_delete($key);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function flush()
|
|
{
|
|
if (!$this->is_connected) {
|
|
return false;
|
|
}
|
|
|
|
return $this->_flush();
|
|
}
|
|
|
|
public function set($key, $value, $ttl = 0)
|
|
{
|
|
if (!$this->is_connected) {
|
|
return false;
|
|
}
|
|
|
|
return $this->_set($key, $value, $ttl);
|
|
}
|
|
|
|
public function get($key)
|
|
{
|
|
if (!$this->is_connected) {
|
|
return false;
|
|
}
|
|
|
|
return $this->_get($key);
|
|
}
|
|
|
|
public function exists($key)
|
|
{
|
|
if (!$this->is_connected) {
|
|
return false;
|
|
}
|
|
|
|
return $this->_exists($key);
|
|
}
|
|
}
|