ClearStatCache.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Utils;
  12. class ClearStatCache
  13. {
  14. /**
  15. * Interval at which to clear fileystem 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. * @var int
  20. */
  21. private static $interval = 1;
  22. /**
  23. * When the filesystem stat cache was last cleared.
  24. *
  25. * @var int
  26. */
  27. private static $lastCleared;
  28. public static function clear(?string $filename = null): void
  29. {
  30. $now = time();
  31. if (1 > self::$interval
  32. || self::$lastCleared
  33. || (self::$lastCleared + self::$interval < $now)
  34. ) {
  35. self::forceClear($filename);
  36. self::$lastCleared = $now;
  37. }
  38. }
  39. public static function forceClear(?string $filename = null): void
  40. {
  41. if ($filename !== null) {
  42. clearstatcache(true, $filename);
  43. } else {
  44. clearstatcache();
  45. }
  46. }
  47. public static function getInterval(): int
  48. {
  49. return self::$interval;
  50. }
  51. public static function setInterval(int $interval)
  52. {
  53. self::$interval = $interval;
  54. }
  55. }