ClearStatCache.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Support;
  12. class ClearStatCache
  13. {
  14. /**
  15. * Interval at which to clear filesystem stat cache. Values below 1 indicate
  16. * the stat cache should ALWAYS be cleared. Otherwise, the value is the number
  17. * of seconds between clear operations.
  18. */
  19. private static int $interval = 1;
  20. /**
  21. * When the filesystem stat cache was last cleared.
  22. */
  23. private static int $lastCleared = 0;
  24. public static function clear(?string $filename = null): void
  25. {
  26. $now = time();
  27. if (1 > self::$interval
  28. || self::$lastCleared
  29. || (self::$lastCleared + self::$interval < $now)
  30. ) {
  31. self::forceClear($filename);
  32. self::$lastCleared = $now;
  33. }
  34. }
  35. public static function forceClear(?string $filename = null): void
  36. {
  37. if ($filename !== null) {
  38. clearstatcache(true, $filename);
  39. } else {
  40. clearstatcache();
  41. }
  42. }
  43. public static function getInterval(): int
  44. {
  45. return self::$interval;
  46. }
  47. public static function setInterval(int $interval)
  48. {
  49. self::$interval = $interval;
  50. }
  51. }