1. <?php
  2. /**
  3. * Message Queue Result Caching for Monitoring
  4. *
  5. * This script provides a mechanism to cache the results of message queue operations
  6. * for monitoring purposes. It includes verbose logging to track cache hits, misses,
  7. * and errors.
  8. */
  9. class MessageQueueCache {
  10. private $cache = []; // Simple in-memory cache
  11. private $logFile = 'message_queue_cache.log'; // Log file path
  12. /**
  13. * Constructor
  14. */
  15. public function __construct($logFile = 'message_queue_cache.log') {
  16. $this->logFile = $logFile;
  17. $this->logMessage("Cache initialized.");
  18. }
  19. /**
  20. * Cache a message queue operation result.
  21. *
  22. * @param string $key The cache key (e.g., message queue name + operation).
  23. * @param mixed $result The result to be cached.
  24. * @param int $expiryTime (optional) Time to live in seconds. Defaults to 3600 (1 hour).
  25. * @return bool True on success, false on failure.
  26. */
  27. public function cacheResult(string $key, $result, int $expiryTime = 3600): bool {
  28. $expiry = time() + $expiryTime;
  29. $this->cache[$key] = [
  30. 'result' => $result,
  31. 'expiry' => $expiry
  32. ];
  33. $this->logMessage("Cached result for key: $key, expiry: {$expiry}");
  34. return true;
  35. }
  36. /**
  37. * Retrieve a cached result.
  38. *
  39. * @param string $key The cache key.
  40. * @return mixed The cached result, or null if not found or expired.
  41. */
  42. public function getResult(string $key) {
  43. if (isset($this->cache[$key])) {
  44. $entry = $this->cache[$key];
  45. if ($entry['expiry'] > time()) {
  46. $this->logMessage("Cache hit for key: $key");
  47. return $entry['result'];
  48. } else {
  49. $this->logMessage("Cache expired for key: $key");
  50. unset($this->cache[$key]); // Remove expired entry
  51. return null;
  52. }
  53. } else {
  54. $this->logMessage("Cache miss for key: $key");
  55. return null;
  56. }
  57. }
  58. /**
  59. * Remove a cached item.
  60. *
  61. * @param string $key The key of the item to remove.
  62. * @return bool True on success, false if the key doesn't exist.
  63. */
  64. public function removeResult(string $key): bool {
  65. if (isset($this->cache[$key])) {
  66. unset($this->cache[$key]);
  67. $this->logMessage("Removed result for key: $key");
  68. return true;
  69. } else {
  70. $this->logMessage("Key not found for removal: $key");
  71. return false;
  72. }
  73. }
  74. /**
  75. * Log a message to the log file.
  76. *
  77. * @param string $message The message to log.
  78. */
  79. private function logMessage(string $message) {
  80. $timestamp = date('Y-m-d H:i:s');
  81. $logEntry = "[$timestamp] $message\n";
  82. file_put_contents($this->logFile, $logEntry, FILE_APPEND);
  83. }
  84. }
  85. // Example Usage:
  86. $cache = new MessageQueueCache();
  87. // Simulate a message queue operation
  88. $result = 'Data from message queue';
  89. // Cache the result
  90. if ($cache->cacheResult('my_queue_task', $result, 60)) { //Cache for 60 seconds
  91. // Use the cached result
  92. $retrievedResult = $cache->getResult('my_queue_task');
  93. echo "Retrieved result: " . $retrievedResult . "\n";
  94. } else {
  95. echo "Failed to cache result.\n";
  96. }
  97. //Try to get expired result
  98. sleep(61);
  99. $retrievedResult = $cache->getResult('my_queue_task');
  100. echo "Retrieved result after expiry: " . ($retrievedResult === null ? 'null' : $retrievedResult) . "\n";
  101. $cache->removeResult('my_queue_task');
  102. echo "Removed result.\n";
  103. ?>

Add your comment