Refactor cache handling in VariableManager and update configuration options
Some checks are pending
CI / debug (push) Waiting to run
CI / release_executable (push) Waiting to run
CI / release (push) Waiting to run
CI / debug_executable (push) Waiting to run
CI / check-phpunit (push) Waiting to run
CI / check-phpdoc (push) Waiting to run
CI / generate-phpdoc (push) Blocked by required conditions
CI / test (push) Blocked by required conditions
CI / release-documentation (push) Blocked by required conditions
CI / release-artifacts (push) Blocked by required conditions

This commit is contained in:
netkas 2025-03-17 13:47:09 -04:00
parent bbeb388e88
commit c95cdcd4da
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
5 changed files with 146 additions and 134 deletions

View file

@ -50,4 +50,5 @@
return self::$instance;
}
}

View file

@ -3,14 +3,11 @@
namespace Socialbox\Classes\CliCommands;
use Exception;
use ncc\CLI\Main;
use ncc\ThirdParty\Symfony\Process\Exception\InvalidArgumentException;
use PDOException;
use Socialbox\Abstracts\CacheLayer;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Cryptography;
use Socialbox\Classes\Database;
use Socialbox\Classes\Logger;
use Socialbox\Classes\Resources;
use Socialbox\Enums\DatabaseObjects;
use Socialbox\Exceptions\CryptographyException;
@ -92,7 +89,8 @@
if(Configuration::getCacheConfiguration()->isEnabled())
{
Program::getLogger()->verbose('Clearing cache layer...');
CacheLayer::getInstance()->clear();
// TODO: Re-implement cache layer
//CacheLayer::getInstance()->clear();
}
foreach(DatabaseObjects::casesOrdered() as $object)

View file

@ -1,151 +1,152 @@
<?php
namespace Socialbox\Managers;
namespace Socialbox\Managers;
use PDO;
use PDOException;
use Socialbox\Abstracts\CacheLayer;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Database;
use Socialbox\Exceptions\DatabaseOperationException;
use PDO;
use PDOException;
use Socialbox\Classes\CacheLayer;
use Socialbox\Classes\Configuration;
use Socialbox\Classes\Database;
use Socialbox\Exceptions\DatabaseOperationException;
class VariableManager
{
/**
* Sets a variable in the database. If the variable already exists, its value is updated.
*
* @param string $name The name of the variable.
* @param string $value The value of the variable.
* @return void
* @throws DatabaseOperationException If the operation fails.
*/
public static function setVariable(string $name, string $value): void
class VariableManager
{
try
/**
* Sets a variable in the database. If the variable already exists, its value is updated.
*
* @param string $name The name of the variable.
* @param string $value The value of the variable.
* @return void
* @throws DatabaseOperationException If the operation fails.
*/
public static function setVariable(string $name, string $value): void
{
$statement = Database::getConnection()->prepare("INSERT INTO variables (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=?");
$statement->bindParam(1, $name);
$statement->bindParam(2, $value);
$statement->bindParam(3, $value);
$statement->execute();
try
{
$statement = Database::getConnection()->prepare("INSERT INTO variables (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=?");
$statement->bindParam(1, $name);
$statement->bindParam(2, $value);
$statement->bindParam(3, $value);
$statement->execute();
}
catch(PDOException $e)
{
throw new DatabaseOperationException(sprintf('Failed to set variable %s in the database', $name), $e);
}
// TODO: Re-implement caching
//finally
//{
//if(Configuration::getConfiguration()['cache']['enabled'] && Configuration::getConfiguration()['cache']['variables']['enabled'])
//{
// if(Configuration::getConfiguration()['cache']['variables']['max'] > 0)
// {
// if(CacheLayer::getPrefixCount('VARIABLES_') >= Configuration::getConfiguration()['cache']['variables']['max'])
// {
// // Return early if the cache is full
// return;
// }
// }
// CacheLayer::getInstance()->set(sprintf("VARIABLES_%s", $name), $value, (int)Configuration::getConfiguration()['cache']['variables']['ttl']);
//}
//}
}
catch(PDOException $e)
{
throw new DatabaseOperationException(sprintf('Failed to set variable %s in the database', $name), $e);
}
finally
/**
* Retrieves the value of a variable from the database based on its name.
*
* @param string $name The name of the variable to retrieve.
* @return string The value of the variable.
* @throws DatabaseOperationException If the database operation fails.
*/
public static function getVariable(string $name): string
{
if(Configuration::getConfiguration()['cache']['enabled'] && Configuration::getConfiguration()['cache']['variables']['enabled'])
{
if(Configuration::getConfiguration()['cache']['variables']['max'] > 0)
$cachedValue = CacheLayer::getInstance()->get(sprintf("VARIABLES_%s", $name));
if($cachedValue !== false)
{
if(CacheLayer::getInstance()->getPrefixCount('VARIABLES_') >= Configuration::getConfiguration()['cache']['variables']['max'])
{
// Return early if the cache is full
return;
}
return $cachedValue;
}
}
try
{
$statement = Database::getConnection()->prepare("SELECT value FROM variables WHERE name=?");
$statement->bindParam(1, $name);
$statement->execute();
if($statement->rowCount() === 0)
{
throw new DatabaseOperationException(sprintf('Variable with name %s does not exist', $name));
}
CacheLayer::getInstance()->set(sprintf("VARIABLES_%s", $name), $value, (int)Configuration::getConfiguration()['cache']['variables']['ttl']);
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result['value'];
}
}
}
/**
* Retrieves the value of a variable from the database based on its name.
*
* @param string $name The name of the variable to retrieve.
* @return string The value of the variable.
* @throws DatabaseOperationException If the database operation fails.
*/
public static function getVariable(string $name): string
{
if(Configuration::getConfiguration()['cache']['enabled'] && Configuration::getConfiguration()['cache']['variables']['enabled'])
{
$cachedValue = CacheLayer::getInstance()->get(sprintf("VARIABLES_%s", $name));
if($cachedValue !== false)
catch(PDOException $e)
{
return $cachedValue;
throw new DatabaseOperationException(sprintf('Failed to get variable %s from the database', $name), $e);
}
}
try
{
$statement = Database::getConnection()->prepare("SELECT value FROM variables WHERE name=?");
$statement->bindParam(1, $name);
$statement->execute();
if($statement->rowCount() === 0)
{
throw new DatabaseOperationException(sprintf('Variable with name %s does not exist', $name));
}
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result['value'];
}
catch(PDOException $e)
{
throw new DatabaseOperationException(sprintf('Failed to get variable %s from the database', $name), $e);
}
}
/**
* Checks if a variable with the specified name exists in the database.
*
* @param string $name The name of the variable to check for existence.
* @return bool Returns true if the variable exists, false otherwise.
* @throws DatabaseOperationException If the database operation fails.
*/
public static function variableExists(string $name): bool
{
if(Configuration::getConfiguration()['cache']['enabled'] && Configuration::getConfiguration()['cache']['variables']['enabled'])
{
$cachedValue = CacheLayer::getInstance()->get(sprintf("VARIABLES_%s", $name));
if($cachedValue !== false)
{
return true;
}
}
try
{
$statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM variables WHERE name=?");
$statement->bindParam(1, $name);
$statement->execute();
$result = $statement->fetchColumn();
return $result > 0;
}
catch(PDOException $e)
{
throw new DatabaseOperationException(sprintf('Failed to check if the variable %s exists', $name), $e);
}
}
/**
* Deletes a variable from the database using the provided name.
*
* @param string $name The name of the variable to be deleted.
* @return void
* @throws DatabaseOperationException If the database operation fails.
*/
public static function deleteVariable(string $name): void
{
try
{
$statement = Database::getConnection()->prepare("DELETE FROM variables WHERE name=?");
$statement->bindParam(1, $name);
$statement->execute();
}
catch(PDOException $e)
{
throw new DatabaseOperationException(sprintf('Failed to delete variable %s from the database', $name), $e);
}
finally
/**
* Checks if a variable with the specified name exists in the database.
*
* @param string $name The name of the variable to check for existence.
* @return bool Returns true if the variable exists, false otherwise.
* @throws DatabaseOperationException If the database operation fails.
*/
public static function variableExists(string $name): bool
{
if(Configuration::getConfiguration()['cache']['enabled'] && Configuration::getConfiguration()['cache']['variables']['enabled'])
{
CacheLayer::getInstance()->delete(sprintf("VARIABLES_%s", $name));
$cachedValue = CacheLayer::getInstance()->get(sprintf("VARIABLES_%s", $name));
if($cachedValue !== false)
{
return true;
}
}
try
{
$statement = Database::getConnection()->prepare("SELECT COUNT(*) FROM variables WHERE name=?");
$statement->bindParam(1, $name);
$statement->execute();
$result = $statement->fetchColumn();
return $result > 0;
}
catch(PDOException $e)
{
throw new DatabaseOperationException(sprintf('Failed to check if the variable %s exists', $name), $e);
}
}
}
}
/**
* Deletes a variable from the database using the provided name.
*
* @param string $name The name of the variable to be deleted.
* @return void
* @throws DatabaseOperationException If the database operation fails.
*/
public static function deleteVariable(string $name): void
{
try
{
$statement = Database::getConnection()->prepare("DELETE FROM variables WHERE name=?");
$statement->bindParam(1, $name);
$statement->execute();
}
catch(PDOException $e)
{
throw new DatabaseOperationException(sprintf('Failed to delete variable %s from the database', $name), $e);
}
finally
{
if(Configuration::getConfiguration()['cache']['enabled'] && Configuration::getConfiguration()['cache']['variables']['enabled'])
{
CacheLayer::getInstance()->delete(sprintf("VARIABLES_%s", $name));
}
}
}
}

View file

@ -107,7 +107,13 @@
"default_email_address_privacy": "CONTACTS",
"default_phone_number_privacy": "CONTACTS",
"default_birthday_privacy": "PRIVATE",
"default_url_privacy": "PUBLIC"
"default_url_privacy": "PUBLIC",
"max_contact_signing_keys": 50,
"get_encryption_channel_requests_limit": 100,
"get_encryption_channels_limit": 100,
"get_encryption_channel_incoming_limit": 100,
"get_encryption_channel_outgoing_limit": 100,
"encryption_channel_max_messages": 100
},
"storage": {
"path": "/etc/socialbox",

View file

@ -107,7 +107,13 @@
"default_email_address_privacy": "CONTACTS",
"default_phone_number_privacy": "CONTACTS",
"default_birthday_privacy": "PRIVATE",
"default_url_privacy": "PUBLIC"
"default_url_privacy": "PUBLIC",
"max_contact_signing_keys": 50,
"get_encryption_channel_requests_limit": 100,
"get_encryption_channels_limit": 100,
"get_encryption_channel_incoming_limit": 100,
"get_encryption_channel_outgoing_limit": 100,
"encryption_channel_max_messages": 100
},
"storage": {
"path": "/etc/socialbox",