From c95cdcd4da646a68b3e95e78307e1f5deac98b28 Mon Sep 17 00:00:00 2001 From: netkas Date: Mon, 17 Mar 2025 13:47:09 -0400 Subject: [PATCH] Refactor cache handling in VariableManager and update configuration options --- src/Socialbox/Classes/CacheLayer.php | 1 + .../Classes/CliCommands/InitializeCommand.php | 6 +- src/Socialbox/Managers/VariableManager.php | 257 +++++++++--------- tests/docker/coffee/config/socialbox.conf | 8 +- tests/docker/teapot/config/socialbox.conf | 8 +- 5 files changed, 146 insertions(+), 134 deletions(-) diff --git a/src/Socialbox/Classes/CacheLayer.php b/src/Socialbox/Classes/CacheLayer.php index 64d6009..d1bbd62 100644 --- a/src/Socialbox/Classes/CacheLayer.php +++ b/src/Socialbox/Classes/CacheLayer.php @@ -50,4 +50,5 @@ return self::$instance; } + } \ No newline at end of file diff --git a/src/Socialbox/Classes/CliCommands/InitializeCommand.php b/src/Socialbox/Classes/CliCommands/InitializeCommand.php index 89de809..ae81c57 100644 --- a/src/Socialbox/Classes/CliCommands/InitializeCommand.php +++ b/src/Socialbox/Classes/CliCommands/InitializeCommand.php @@ -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) diff --git a/src/Socialbox/Managers/VariableManager.php b/src/Socialbox/Managers/VariableManager.php index 563ea33..6bcbe6e 100644 --- a/src/Socialbox/Managers/VariableManager.php +++ b/src/Socialbox/Managers/VariableManager.php @@ -1,151 +1,152 @@ 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); } } - } -} \ No newline at end of file + + /** + * 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)); + } + } + } + } \ No newline at end of file diff --git a/tests/docker/coffee/config/socialbox.conf b/tests/docker/coffee/config/socialbox.conf index 0d01283..d36eb07 100755 --- a/tests/docker/coffee/config/socialbox.conf +++ b/tests/docker/coffee/config/socialbox.conf @@ -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", diff --git a/tests/docker/teapot/config/socialbox.conf b/tests/docker/teapot/config/socialbox.conf index bf3b757..04d107b 100755 --- a/tests/docker/teapot/config/socialbox.conf +++ b/tests/docker/teapot/config/socialbox.conf @@ -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",