1. <?php
  2. /**
  3. * Caches web form results for internal tooling.
  4. *
  5. * This function takes form data and stores the results in a cache
  6. * for later retrieval. This avoids redundant processing of
  7. * the same form data, improving performance.
  8. *
  9. * @param array $formData The form data as an associative array.
  10. * @param string $cacheKey A unique key for the cache entry.
  11. * @param int $cacheExpiryTime Time to live for the cache entry in seconds. Defaults to 3600 (1 hour).
  12. * @param callable $resultCallback A function that takes the form data and returns the result.
  13. * @return array|null The result of the resultCallback or null if the data is not cached.
  14. */
  15. function cacheFormResults(array $formData, string $cacheKey, int $cacheExpiryTime = 3600, callable $resultCallback): ?array
  16. {
  17. // Define the cache store (using a simple array for demonstration. Consider using a more robust cache system like Redis or Memcached in production).
  18. static $cache = [];
  19. // Check if the data is already in the cache.
  20. if (isset($cache[$cacheKey])) {
  21. // Data found in cache, and it hasn't expired.
  22. $cachedResult = $cache[$cacheKey];
  23. // Check if the cache entry has expired.
  24. if ($cachedResult['expiry'] > time()) {
  25. // Cache entry is valid.
  26. return $cachedResult['result'];
  27. } else {
  28. // Cache entry has expired. Remove it.
  29. unset($cache[$cacheKey]);
  30. }
  31. }
  32. // If data not found in cache or expired, calculate the result.
  33. $result = $resultCallback($formData);
  34. // Store the result in the cache with an expiry time.
  35. $cache[$cacheKey] = [
  36. 'result' => $result,
  37. 'expiry' => time() + $cacheExpiryTime,
  38. ];
  39. return $result;
  40. }
  41. /**
  42. * Example usage: Simulates a form submission and retrieves the result.
  43. */
  44. function processForm(array $formData): array
  45. {
  46. // Simulate some form processing.
  47. sleep(1); // Simulate a time-consuming operation
  48. // Example: Check if a required field is present.
  49. if (empty($formData['name'])) {
  50. return ['error' => 'Name is required'];
  51. }
  52. // Simulate some calculation.
  53. $result = [
  54. 'message' => 'Form processed successfully!',
  55. 'data' => $formData,
  56. ];
  57. return $result;
  58. }
  59. // Example usage:
  60. $form_data = [
  61. 'name' => 'John Doe',
  62. 'email' => 'john.doe@example.com',
  63. 'age' => 30,
  64. ];
  65. $cache_key = 'form_result_' . md5(json_encode($form_data)); //Generate a unique key based on the form data
  66. $result = cacheFormResults($form_data, $cache_key, 60, 'processForm'); //Cache for 60 seconds
  67. if ($result !== null) {
  68. echo "Result: " . json_encode($result) . "\n";
  69. } else {
  70. echo "No result found in cache.\n";
  71. }
  72. //Demonstrate retrieving the cached result
  73. $form_data2 = [
  74. 'name' => 'John Doe',
  75. 'email' => 'john.doe@example.com',
  76. 'age' => 30,
  77. ];
  78. $cache_key2 = 'form_result_' . md5(json_encode($form_data2)); //Generate a unique key based on the form data
  79. $result2 = cacheFormResults($form_data2, $cache_key2, 60, 'processForm'); //Retrieve the result from the cache
  80. if ($result2 !== null) {
  81. echo "Result from cache: " . json_encode($result2) . "\n";
  82. } else {
  83. echo "No result found in cache.\n";
  84. }
  85. ?>

Add your comment