<?php
/**
* Message Queue Result Caching for Monitoring
*
* This script provides a mechanism to cache the results of message queue operations
* for monitoring purposes. It includes verbose logging to track cache hits, misses,
* and errors.
*/
class MessageQueueCache {
private $cache = []; // Simple in-memory cache
private $logFile = 'message_queue_cache.log'; // Log file path
/**
* Constructor
*/
public function __construct($logFile = 'message_queue_cache.log') {
$this->logFile = $logFile;
$this->logMessage("Cache initialized.");
}
/**
* Cache a message queue operation result.
*
* @param string $key The cache key (e.g., message queue name + operation).
* @param mixed $result The result to be cached.
* @param int $expiryTime (optional) Time to live in seconds. Defaults to 3600 (1 hour).
* @return bool True on success, false on failure.
*/
public function cacheResult(string $key, $result, int $expiryTime = 3600): bool {
$expiry = time() + $expiryTime;
$this->cache[$key] = [
'result' => $result,
'expiry' => $expiry
];
$this->logMessage("Cached result for key: $key, expiry: {$expiry}");
return true;
}
/**
* Retrieve a cached result.
*
* @param string $key The cache key.
* @return mixed The cached result, or null if not found or expired.
*/
public function getResult(string $key) {
if (isset($this->cache[$key])) {
$entry = $this->cache[$key];
if ($entry['expiry'] > time()) {
$this->logMessage("Cache hit for key: $key");
return $entry['result'];
} else {
$this->logMessage("Cache expired for key: $key");
unset($this->cache[$key]); // Remove expired entry
return null;
}
} else {
$this->logMessage("Cache miss for key: $key");
return null;
}
}
/**
* Remove a cached item.
*
* @param string $key The key of the item to remove.
* @return bool True on success, false if the key doesn't exist.
*/
public function removeResult(string $key): bool {
if (isset($this->cache[$key])) {
unset($this->cache[$key]);
$this->logMessage("Removed result for key: $key");
return true;
} else {
$this->logMessage("Key not found for removal: $key");
return false;
}
}
/**
* Log a message to the log file.
*
* @param string $message The message to log.
*/
private function logMessage(string $message) {
$timestamp = date('Y-m-d H:i:s');
$logEntry = "[$timestamp] $message\n";
file_put_contents($this->logFile, $logEntry, FILE_APPEND);
}
}
// Example Usage:
$cache = new MessageQueueCache();
// Simulate a message queue operation
$result = 'Data from message queue';
// Cache the result
if ($cache->cacheResult('my_queue_task', $result, 60)) { //Cache for 60 seconds
// Use the cached result
$retrievedResult = $cache->getResult('my_queue_task');
echo "Retrieved result: " . $retrievedResult . "\n";
} else {
echo "Failed to cache result.\n";
}
//Try to get expired result
sleep(61);
$retrievedResult = $cache->getResult('my_queue_task');
echo "Retrieved result after expiry: " . ($retrievedResult === null ? 'null' : $retrievedResult) . "\n";
$cache->removeResult('my_queue_task');
echo "Removed result.\n";
?>
Add your comment