Minor optimizations

This commit is contained in:
Netkas 2023-06-21 15:38:31 -04:00
parent 8c5237ff7f
commit e6a869c216
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
3 changed files with 7 additions and 180 deletions

View file

@ -1,148 +0,0 @@
<?php
namespace FederationLib\Interfaces;
use FederationLib\Classes\Configuration\CacheServerConfiguration;
use Memcached;
use Redis;
interface CacheDriverInterface
{
/**
* Constructs a cache server driver
*
* @param CacheServerConfiguration $configuration
*/
public function __construct(CacheServerConfiguration $configuration);
/**
* Returns the configuration for the driver
*
* @return CacheServerConfiguration
*/
public function getConfiguration(): CacheServerConfiguration;
/**
* Returns the driver connection instance.
*
* @return Redis|Memcached
*/
public function getConnection(): Redis|Memcached;
/**
* Returns the values of the specified key
*
* For every key that does not hold a string value or does not exist, the special value false is returned.
* Because of this, the operation never fails.
*
* @param string $key
* @return mixed
*/
public function get(string $key): mixed;
/**
* Gets a value from the hash stored at key. If the hash table doesn't exist,
* or the key doesn't exist, FALSE is returned
*
* @param string $key
* @param string $field
* @return mixed
*/
public function hGet(string $key, string $hashKey): mixed;
/**
* Returns the whole hash, as an array of strings indexed by string
*
* @param string $key
* @return array
*/
public function hGetAll(string $key): array;
/**
* Sets a value in the cache server
*
* @param string $key
* @param mixed $value
* @return void
*/
public function set(string $key, mixed $value): void;
/**
* Fills in a whole hash. Non-string values are converted to string, using the standard (string) cast.
* NULL values are stored as empty string
*
* @param string $key
* @param array $values
* @return void
*/
public function hMSet(string $key, array $values): void;
/**
* Adds a value to the hash stored at key. If this value is already in the hash, FALSE is returned.
*
* @param string $key
* @param string $hash_key
* @param mixed $value
* @return void
*/
public function hSet(string $key, string $hash_key, mixed $value): void;
/**
* Verify if the specified key exists
*
* @param string $key
* @return bool
*/
public function exists($key): bool;
/**
* Remove specified keys
*
* @param $key1
* @param mixed ...$other_keys
* @return void
*/
public function delete($key1, ...$other_keys): void;
/**
* Sets a key to expire in a certain amount of seconds
*
* @param string $key
* @param int $seconds
* @return void
*/
public function expire(string $key, int $seconds): void;
/**
* Determines if the cache server is available (does not necessarily mean that the server is connected)
* This is useful for checking if the cache server is available before attempting to use it
*
* @return bool
*/
public function isAvailable(): bool;
/**
* Determines if the cache server is currently connected
* If the ping parameter is set to true, this will ping the server to ensure that it is connected
* otherwise it will assume the connection is stable if a connection has been established
*
* @param bool $ping This will ping the server to check if it is connected
* @return bool
*/
public function isConnected(bool $ping=false): bool;
/**
* Connects to the cache server
*
* @return void
*/
public function connect(): void;
/**
* Disconnects from the cache server
*
* @return void
*/
public function disconnect(): void;
}

View file

@ -1,24 +0,0 @@
<?php
namespace FederationLib\Interfaces;
use FederationLib\Objects\InvokeResults;
interface CommandAppletInterface
{
/**
* Returns the command to execute.
*
* @return string
*/
public static function getCommand(): string;
/**
* Executes the command and returns the results.
*
* @param string $uid
* @param array $args
* @return InvokeResults
*/
public static function execute(string $uid, array $args): InvokeResults;
}

View file

@ -192,20 +192,21 @@
*/
public function getMetadata(string|ParsedFederatedAddress $federated_address): PeerMetadataInterface
{
$cache_key = sprintf('telegram_chat_metadata:%s', $federated_address->getAddress());
if(Configuration::getObjectCacheEnabled('peer_objects'))
{
try
{
$redis = RedisConnectionManager::getConnectionFromConfig('peer_objects');
$key = sprintf('telegram_chat_metadata:%s', $federated_address->getAddress());
if($redis->exists($key))
if($redis->exists($cache_key))
{
$telegram_chat_metadata = TelegramChatMetadata::fromArray($redis->hGetAll($key), true);
$telegram_chat_metadata = TelegramChatMetadata::fromArray($redis->hGetAll($cache_key), true);
if(Configuration::getObjectCacheTTL('peer_objects') > 0)
{
$redis->expire($key, Configuration::getObjectCacheTTL('peer_objects'));
$redis->expire($cache_key, Configuration::getObjectCacheTTL('peer_objects'));
}
Log::debug(Misc::FEDERATIONLIB, sprintf('Loaded peer metadata object %s from cache', $federated_address->getAddress()));
@ -260,13 +261,11 @@
try
{
$redis = RedisConnectionManager::getConnectionFromConfig('peer_objects');
$key = sprintf('telegram_chat_metadata:%s', $federated_address);
$redis->hMSet($key, $telegram_chat_metadata->toArray());
$redis->hMSet($cache_key, $telegram_chat_metadata->toArray());
if(Configuration::getObjectCacheTTL('peer_objects') > 0)
{
$redis->expire($key, Configuration::getObjectCacheTTL('peer_objects'));
$redis->expire($cache_key, Configuration::getObjectCacheTTL('peer_objects'));
}
Log::debug(Misc::FEDERATIONLIB, sprintf('Cached peer metadata object %s', $federated_address->getAddress()));