Add VariableManager, RpcClient classes, and cache enhancements
This commit is contained in:
parent
38092a639e
commit
e55f4d57f9
27 changed files with 606 additions and 56 deletions
|
@ -5,6 +5,7 @@ namespace Socialbox\Classes\CacheLayer;
|
|||
use Memcached;
|
||||
use RuntimeException;
|
||||
use Socialbox\Abstracts\CacheLayer;
|
||||
use Socialbox\Classes\Configuration;
|
||||
|
||||
class MemcachedCacheLayer extends CacheLayer
|
||||
{
|
||||
|
@ -12,11 +13,8 @@ class MemcachedCacheLayer extends CacheLayer
|
|||
|
||||
/**
|
||||
* Memcached cache layer constructor.
|
||||
*
|
||||
* @param string $host The Memcached server host.
|
||||
* @param int $port The Memcached server port.
|
||||
*/
|
||||
public function __construct(string $host, int $port)
|
||||
public function __construct()
|
||||
{
|
||||
if (!extension_loaded('memcached'))
|
||||
{
|
||||
|
@ -24,9 +22,10 @@ class MemcachedCacheLayer extends CacheLayer
|
|||
}
|
||||
|
||||
$this->memcached = new Memcached();
|
||||
if (!$this->memcached->addServer($host, $port))
|
||||
$this->memcached->addServer(Configuration::getConfiguration()['cache']['host'], (int)Configuration::getConfiguration()['cache']['port']);
|
||||
if(Configuration::getConfiguration()['cache']['username'] !== null || Configuration::getConfiguration()['cache']['password'] !== null)
|
||||
{
|
||||
throw new RuntimeException('Failed to connect to the Memcached server.');
|
||||
$this->memcached->setSaslAuthData(Configuration::getConfiguration()['cache']['username'], Configuration::getConfiguration()['cache']['password']);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,6 +79,25 @@ class MemcachedCacheLayer extends CacheLayer
|
|||
return $this->memcached->getResultCode() === Memcached::RES_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getPrefixCount(string $prefix): int
|
||||
{
|
||||
$stats = $this->memcached->getStats();
|
||||
$count = 0;
|
||||
|
||||
foreach ($stats as $server => $data)
|
||||
{
|
||||
if (str_starts_with($server, $prefix))
|
||||
{
|
||||
$count += $data['curr_items'];
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
|
|
@ -6,6 +6,7 @@ use Redis;
|
|||
use RedisException;
|
||||
use RuntimeException;
|
||||
use Socialbox\Abstracts\CacheLayer;
|
||||
use Socialbox\Classes\Configuration;
|
||||
|
||||
class RedisCacheLayer extends CacheLayer
|
||||
{
|
||||
|
@ -13,12 +14,8 @@ class RedisCacheLayer extends CacheLayer
|
|||
|
||||
/**
|
||||
* Redis cache layer constructor.
|
||||
*
|
||||
* @param string $host The Redis server host.
|
||||
* @param int $port The Redis server port.
|
||||
* @param string|null $password Optional. The Redis server password.
|
||||
*/
|
||||
public function __construct(string $host, int $port, ?string $password=null)
|
||||
public function __construct()
|
||||
{
|
||||
if (!extension_loaded('redis'))
|
||||
{
|
||||
|
@ -29,10 +26,15 @@ class RedisCacheLayer extends CacheLayer
|
|||
|
||||
try
|
||||
{
|
||||
$this->redis->connect($host, $port);
|
||||
if ($password !== null)
|
||||
$this->redis->connect(Configuration::getConfiguration()['cache']['host'], (int)Configuration::getConfiguration()['cache']['port']);
|
||||
if (Configuration::getConfiguration()['cache']['password'] !== null)
|
||||
{
|
||||
$this->redis->auth($password);
|
||||
$this->redis->auth(Configuration::getConfiguration()['cache']['password']);
|
||||
}
|
||||
|
||||
if (Configuration::getConfiguration()['cache']['database'] !== 0)
|
||||
{
|
||||
$this->redis->select((int)Configuration::getConfiguration()['cache']['database']);
|
||||
}
|
||||
}
|
||||
catch (RedisException $e)
|
||||
|
@ -101,6 +103,21 @@ class RedisCacheLayer extends CacheLayer
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getPrefixCount(string $prefix): int
|
||||
{
|
||||
try
|
||||
{
|
||||
return count($this->redis->keys($prefix . '*'));
|
||||
}
|
||||
catch (RedisException $e)
|
||||
{
|
||||
throw new RuntimeException('Failed to get the count of keys with the specified prefix in the Redis cache.', 0, $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue