initial commit; version 22.5.12042
This commit is contained in:
114
libs/Phpfastcache8/Drivers/Ssdb/Config.php
Normal file
114
libs/Phpfastcache8/Drivers/Ssdb/Config.php
Normal file
@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* This file is part of phpFastCache.
|
||||
*
|
||||
* @license MIT License (MIT)
|
||||
*
|
||||
* For full copyright and license information, please see the docs/CREDITS.txt file.
|
||||
*
|
||||
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
|
||||
* @author Georges.L (Geolim4) <contact@geolim4.com>
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Phpfastcache\Drivers\Ssdb;
|
||||
|
||||
use Phpfastcache\Config\ConfigurationOption;
|
||||
|
||||
class Config extends ConfigurationOption
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $host = '127.0.0.1';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $port = 8888;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $password = '';
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $timeout = 2000;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
* @return Config
|
||||
*/
|
||||
public function setHost(string $host): Config
|
||||
{
|
||||
$this->host = $host;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPort(): int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $port
|
||||
* @return Config
|
||||
*/
|
||||
public function setPort(int $port): Config
|
||||
{
|
||||
$this->port = $port;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
* @return Config
|
||||
*/
|
||||
public function setPassword(string $password): Config
|
||||
{
|
||||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getTimeout(): int
|
||||
{
|
||||
return $this->timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $timeout
|
||||
* @return Config
|
||||
*/
|
||||
public function setTimeout(int $timeout): Config
|
||||
{
|
||||
$this->timeout = $timeout;
|
||||
return $this;
|
||||
}
|
||||
}
|
156
libs/Phpfastcache8/Drivers/Ssdb/Driver.php
Normal file
156
libs/Phpfastcache8/Drivers/Ssdb/Driver.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* This file is part of phpFastCache.
|
||||
*
|
||||
* @license MIT License (MIT)
|
||||
*
|
||||
* For full copyright and license information, please see the docs/CREDITS.txt file.
|
||||
*
|
||||
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
|
||||
* @author Georges.L (Geolim4) <contact@geolim4.com>
|
||||
*
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Phpfastcache\Drivers\Ssdb;
|
||||
|
||||
use Phpfastcache\Cluster\AggregatablePoolInterface;
|
||||
use Phpfastcache\Core\Pool\{DriverBaseTrait, ExtendedCacheItemPoolInterface};
|
||||
use Phpfastcache\Entities\DriverStatistic;
|
||||
use Phpfastcache\Exceptions\{PhpfastcacheDriverCheckException, PhpfastcacheDriverException, PhpfastcacheInvalidArgumentException};
|
||||
use phpssdb\Core\{SimpleSSDB, SSDBException};
|
||||
use Psr\Cache\CacheItemInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Class Driver
|
||||
* @package phpFastCache\Drivers
|
||||
* @property SimpleSSDB $instance Instance of driver service
|
||||
* @property Config $config Config object
|
||||
* @method Config getConfig() Return the config object
|
||||
*/
|
||||
class Driver implements ExtendedCacheItemPoolInterface, AggregatablePoolInterface
|
||||
{
|
||||
use DriverBaseTrait;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function driverCheck(): bool
|
||||
{
|
||||
static $driverCheck;
|
||||
if ($driverCheck === null) {
|
||||
return ($driverCheck = class_exists('phpssdb\Core\SSDB'));
|
||||
}
|
||||
|
||||
return $driverCheck;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DriverStatistic
|
||||
*/
|
||||
public function getStats(): DriverStatistic
|
||||
{
|
||||
$stat = new DriverStatistic();
|
||||
$info = $this->instance->info();
|
||||
|
||||
/**
|
||||
* Data returned by Ssdb are very poorly formatted
|
||||
* using hardcoded offset of pair key-value :-(
|
||||
*/
|
||||
$stat->setInfo(sprintf("Ssdb-server v%s with a total of %s call(s).\n For more information see RawData.", $info[2], $info[6]))
|
||||
->setRawData($info)
|
||||
->setData(implode(', ', array_keys($this->itemInstances)))
|
||||
->setSize($this->instance->dbsize());
|
||||
|
||||
return $stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws PhpfastcacheDriverException
|
||||
*/
|
||||
protected function driverConnect(): bool
|
||||
{
|
||||
try {
|
||||
$clientConfig = $this->getConfig();
|
||||
|
||||
$this->instance = new SimpleSSDB($clientConfig->getHost(), $clientConfig->getPort(), $clientConfig->getTimeout());
|
||||
if (!empty($clientConfig->getPassword())) {
|
||||
$this->instance->auth($clientConfig->getPassword());
|
||||
}
|
||||
|
||||
if (!$this->instance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (SSDBException $e) {
|
||||
throw new PhpfastcacheDriverCheckException('Ssdb failed to connect with error: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CacheItemInterface $item
|
||||
* @return null|array
|
||||
*/
|
||||
protected function driverRead(CacheItemInterface $item)
|
||||
{
|
||||
$val = $this->instance->get($item->getEncodedKey());
|
||||
if ($val == false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->decode($val);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CacheItemInterface $item
|
||||
* @return mixed
|
||||
* @throws PhpfastcacheInvalidArgumentException
|
||||
*/
|
||||
protected function driverWrite(CacheItemInterface $item): bool
|
||||
{
|
||||
/**
|
||||
* Check for Cross-Driver type confusion
|
||||
*/
|
||||
if ($item instanceof Item) {
|
||||
return (bool)$this->instance->setx($item->getEncodedKey(), $this->encode($this->driverPreWrap($item)), $item->getTtl());
|
||||
}
|
||||
|
||||
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CacheItemInterface $item
|
||||
* @return bool
|
||||
* @throws PhpfastcacheInvalidArgumentException
|
||||
*/
|
||||
protected function driverDelete(CacheItemInterface $item): bool
|
||||
{
|
||||
/**
|
||||
* Check for Cross-Driver type confusion
|
||||
*/
|
||||
if ($item instanceof Item) {
|
||||
return (bool)$this->instance->del($item->getEncodedKey());
|
||||
}
|
||||
|
||||
throw new PhpfastcacheInvalidArgumentException('Cross-Driver type confusion detected');
|
||||
}
|
||||
|
||||
/********************
|
||||
*
|
||||
* PSR-6 Extended Methods
|
||||
*
|
||||
*******************/
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected function driverClear(): bool
|
||||
{
|
||||
return (bool)$this->instance->flushdb('kv');
|
||||
}
|
||||
}
|
60
libs/Phpfastcache8/Drivers/Ssdb/Item.php
Normal file
60
libs/Phpfastcache8/Drivers/Ssdb/Item.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
*
|
||||
* This file is part of phpFastCache.
|
||||
*
|
||||
* @license MIT License (MIT)
|
||||
*
|
||||
* For full copyright and license information, please see the docs/CREDITS.txt file.
|
||||
*
|
||||
* @author Khoa Bui (khoaofgod) <khoaofgod@gmail.com> https://www.phpfastcache.com
|
||||
* @author Georges.L (Geolim4) <contact@geolim4.com>
|
||||
*
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Phpfastcache\Drivers\Ssdb;
|
||||
|
||||
use Phpfastcache\Core\Item\{ExtendedCacheItemInterface, ItemBaseTrait};
|
||||
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
|
||||
use Phpfastcache\Drivers\Ssdb\Driver as SsdbDriver;
|
||||
use Phpfastcache\Exceptions\{PhpfastcacheInvalidArgumentException};
|
||||
|
||||
/**
|
||||
* Class Item
|
||||
* @package phpFastCache\Drivers\Ssdb
|
||||
*/
|
||||
class Item implements ExtendedCacheItemInterface
|
||||
{
|
||||
use ItemBaseTrait {
|
||||
ItemBaseTrait::__construct as __BaseConstruct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Item constructor.
|
||||
* @param Driver $driver
|
||||
* @param $key
|
||||
* @throws PhpfastcacheInvalidArgumentException
|
||||
*/
|
||||
public function __construct(SsdbDriver $driver, $key)
|
||||
{
|
||||
$this->__BaseConstruct($driver, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExtendedCacheItemPoolInterface $driver
|
||||
* @return static
|
||||
* @throws PhpfastcacheInvalidArgumentException
|
||||
*/
|
||||
public function setDriver(ExtendedCacheItemPoolInterface $driver)
|
||||
{
|
||||
if ($driver instanceof SsdbDriver) {
|
||||
$this->driver = $driver;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
throw new PhpfastcacheInvalidArgumentException('Invalid driver instance');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user