1. <?php
  2. /**
  3. * Invalidates cache for log streams in staging environments.
  4. *
  5. * @param string $environment The environment (e.g., 'staging').
  6. * @return bool True on success, false on failure.
  7. */
  8. function invalidateStagingLogCache(string $environment): bool
  9. {
  10. // Sanity check: Ensure environment is valid
  11. if ($environment !== 'staging') {
  12. error_log("Invalid environment provided: " . $environment);
  13. return false;
  14. }
  15. // Define the cache key for log streams. Adjust as needed.
  16. $cacheKey = 'staging_log_streams';
  17. // Attempt to invalidate the cache. Replace with your actual cache invalidation logic.
  18. try {
  19. // Example using a simple array cache (replace with your cache system)
  20. if (isset($_キャッシュ[$cacheKey])) {
  21. unset($_キャッシュ[$cacheKey]);
  22. error_log("Invalidated cache for log streams: " . $cacheKey);
  23. } else {
  24. error_log("Cache for log streams not found: " . $cacheKey);
  25. return true; //Consider it successful if the cache wasn't there.
  26. }
  27. return true;
  28. } catch (Exception $e) {
  29. error_log("Error invalidating cache: " . $e->getMessage());
  30. return false;
  31. }
  32. }
  33. // Example usage (replace with your actual environment detection)
  34. if ($_SERVER['SERVER_NAME'] === 'staging.example.com') {
  35. if (invalidateStagingLogCache('staging')) {
  36. echo "Staging log cache invalidated successfully.\n";
  37. } else {
  38. echo "Failed to invalidate staging log cache.\n";
  39. }
  40. } else {
  41. echo "Not running in staging environment.\n";
  42. }
  43. ?>

Add your comment