cooldowns.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. if (!function_exists("apcu_fetch"))
  3. error_log("apcu is not installed! Please consider installing php-pecl-apcu for significant performance improvements");
  4. function load_cooldowns() {
  5. if (function_exists("apcu_fetch"))
  6. return apcu_exists("cooldowns") ? apcu_fetch("cooldowns") : array();
  7. return array();
  8. }
  9. function save_cooldowns($cooldowns) {
  10. if (function_exists("apcu_store"))
  11. apcu_store("cooldowns", $cooldowns);
  12. }
  13. function set_cooldown($instance, $timeout, $cooldowns) {
  14. $cooldowns[$instance] = time() + $timeout;
  15. save_cooldowns($cooldowns);
  16. return $cooldowns;
  17. }
  18. function has_cooldown($instance, $cooldowns) {
  19. return ($cooldowns[$instance] ?? 0) > time();
  20. }
  21. function has_cached_results($url) {
  22. if (function_exists("apcu_exists"))
  23. return apcu_exists("cached:$url");
  24. return false;
  25. }
  26. function store_cached_results($url, $results, $ttl = 0) {
  27. if (function_exists("apcu_store") && !empty($results))
  28. return apcu_store("cached:$url", $results, $ttl);
  29. }
  30. function fetch_cached_results($url) {
  31. if (function_exists("apcu_fetch"))
  32. return apcu_fetch("cached:$url");
  33. return array();
  34. }
  35. ?>