Files
api/services/nginx/app/tests/redis/redisTest.php
T
2025-01-29 14:27:44 +01:00

149 lines
4.3 KiB
PHP

<?php
// prevent direct access
if (!defined('WD')) {
exit;
}
global $REDIS_CONFIG;
use classes\redis;
function warn($message): void
{
echo "\n\033[33m$message\033[0m\n";
}
// Check if the MINIO array is set
if (!isset($REDIS_CONFIG)) {
throw new Exception('REDIS array is not set in config.php');
}
// Check if the MINIO array has the required keys
if (!isset($REDIS_CONFIG['host']) || !isset($REDIS_CONFIG['database']) || !isset($REDIS_CONFIG['password'])) {
throw new Exception('REDIS array is missing required keys');
}
// Create a new Redis object
$redis = new redis();
// Connect to the Redis server
$redis->connect();
// Check if the Redis server is connected
if ($redis->is_connected()) {
// Show success message
echo "\nConnected to the Redis server";
} else {
// Show error message
warn('Failed to connect to the Redis server');
}
// Set a value in the Redis server
$time_start = microtime(true);
$redis->set('test', 'Hello, World!');
$time_end = microtime(true);
// Check if the request was successful
if ($redis->get('test') === 'Hello, World!') {
// Show success message
echo "\nValue set successfully ( " . ($time_end - $time_start) . 's )';
} else {
// Show error message
warn('Failed to set value in the Redis server ( ' . ($time_end - $time_start) . 's )');
}
// Get the value from the Redis server
$time_start_get = microtime(true);
$value = $redis->get('test');
$time_end_get = microtime(true);
// Check if the request was successful
if ($value === 'Hello, World!') {
// Show success message
echo "\nValue retrieved successfully: $value ( " . ($time_end_get - $time_start_get) . 's )';
} else {
// Show error message
warn('Failed to retrieve value from the Redis server ( ' . ($time_end_get - $time_start_get) . 's )');
}
// Delete the value from the Redis server
$redis->delete('test');
// Get the value from the Redis server
$value = $redis->get('test');
// Check if the request was successful
if ($value === null) {
// Show success message
echo "\nValue deleted successfully";
} else {
// Show error message
warn('Failed to delete value from the Redis server');
}
// Get all keys from the Redis server
$keys = $redis->keys();
// Check if the request was successful
if (count($keys) > 0) {
// Show the keys
echo "\nKeys found: " . implode(', ', $keys);
} else {
// Show warning message
warn('No keys found');
}
// Add 500 keys to the Redis server
$time_start = microtime(true);
for ( $i = 0; $i < 200; $i++ ) {
$redis->set('key_' . $i, 'value_' . $i);
}
$time_end = microtime(true);
// Get all keys from the Redis server
$keys = $redis->keys();
// Check if the request was successful
if (count($keys) === 200) {
// Show success message
echo "\n200 keys added successfully ( " . $time_end - $time_start . 's )';
} else {
// Show error message
warn('Failed to add 200 keys to the Redis server ( ' . ($time_end - $time_start) . 's )');
}
// Remove all keys from the Redis server
$time_start = microtime(true);
foreach ( $keys as $key ) {
$redis->delete($key);
}
$time_end = microtime(true);
// Get all keys from the Redis server
$keys = $redis->keys();
// Check if the request was successful
if (count($keys) === 0) {
// Show success message
echo "\n200 keys removed successfully ( " . ($time_end - $time_start) . 's )';
} else {
// Show error message
warn('Failed to remove 200 keys from the Redis server ( ' . ($time_end - $time_start) . 's )');
}
// Add a pre-defined object to the Redis server
$redis->add_object('order', '1', 'Order 1');
// Get the object from the Redis server
$object = $redis->get_object('order', '1');
// Check if the request was successful
if ($object === 'Order 1') {
// Show success message
echo "\nObject retrieved successfully: $object";
} else {
// Show error message
warn('Failed to retrieve object from the Redis server');
}
// Delete the object from the Redis server
$redis->delete_object('order', '1');
// Get the object from the Redis server
$object = $redis->get_object('order', '1');
// Check if the request was successful
if ($object === null) {
// Show success message
echo "\nObject deleted successfully";
} else {
// Show error message
warn('Failed to delete object from the Redis server');
}
// Disconnect from the Redis server
$redis->disconnect();