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; return self::$instance;
} }
} }

View file

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

View file

@ -1,151 +1,152 @@
<?php <?php
namespace Socialbox\Managers; namespace Socialbox\Managers;
use PDO; use PDO;
use PDOException; use PDOException;
use Socialbox\Abstracts\CacheLayer; use Socialbox\Classes\CacheLayer;
use Socialbox\Classes\Configuration; use Socialbox\Classes\Configuration;
use Socialbox\Classes\Database; use Socialbox\Classes\Database;
use Socialbox\Exceptions\DatabaseOperationException; use Socialbox\Exceptions\DatabaseOperationException;
class VariableManager 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
{ {
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=?"); try
$statement->bindParam(1, $name); {
$statement->bindParam(2, $value); $statement = Database::getConnection()->prepare("INSERT INTO variables (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value=?");
$statement->bindParam(3, $value); $statement->bindParam(1, $name);
$statement->execute(); $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); * Retrieves the value of a variable from the database based on its name.
} *
finally * @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']['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 $cachedValue;
{ }
// Return early if the cache is full }
return;
} 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'];
} }
} catch(PDOException $e)
}
/**
* 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)
{ {
return $cachedValue; throw new DatabaseOperationException(sprintf('Failed to get variable %s from the database', $name), $e);
} }
} }
try /**
{ * Checks if a variable with the specified name exists in the database.
$statement = Database::getConnection()->prepare("SELECT value FROM variables WHERE name=?"); *
$statement->bindParam(1, $name); * @param string $name The name of the variable to check for existence.
$statement->execute(); * @return bool Returns true if the variable exists, false otherwise.
* @throws DatabaseOperationException If the database operation fails.
if($statement->rowCount() === 0) */
{ public static function variableExists(string $name): bool
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
{ {
if(Configuration::getConfiguration()['cache']['enabled'] && Configuration::getConfiguration()['cache']['variables']['enabled']) 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_email_address_privacy": "CONTACTS",
"default_phone_number_privacy": "CONTACTS", "default_phone_number_privacy": "CONTACTS",
"default_birthday_privacy": "PRIVATE", "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": { "storage": {
"path": "/etc/socialbox", "path": "/etc/socialbox",

View file

@ -107,7 +107,13 @@
"default_email_address_privacy": "CONTACTS", "default_email_address_privacy": "CONTACTS",
"default_phone_number_privacy": "CONTACTS", "default_phone_number_privacy": "CONTACTS",
"default_birthday_privacy": "PRIVATE", "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": { "storage": {
"path": "/etc/socialbox", "path": "/etc/socialbox",