RateLimiter.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Illuminate\Cache;
  3. use Illuminate\Support\InteractsWithTime;
  4. use Illuminate\Contracts\Cache\Repository as Cache;
  5. class RateLimiter
  6. {
  7. use InteractsWithTime;
  8. /**
  9. * The cache store implementation.
  10. *
  11. * @var \Illuminate\Contracts\Cache\Repository
  12. */
  13. protected $cache;
  14. /**
  15. * Create a new rate limiter instance.
  16. *
  17. * @param \Illuminate\Contracts\Cache\Repository $cache
  18. * @return void
  19. */
  20. public function __construct(Cache $cache)
  21. {
  22. $this->cache = $cache;
  23. }
  24. /**
  25. * Determine if the given key has been "accessed" too many times.
  26. *
  27. * @param string $key
  28. * @param int $maxAttempts
  29. * @return bool
  30. */
  31. public function tooManyAttempts($key, $maxAttempts)
  32. {
  33. if ($this->attempts($key) >= $maxAttempts) {
  34. if ($this->cache->has($key.':timer')) {
  35. return true;
  36. }
  37. $this->resetAttempts($key);
  38. }
  39. return false;
  40. }
  41. /**
  42. * Increment the counter for a given key for a given decay time.
  43. *
  44. * @param string $key
  45. * @param float|int $decayMinutes
  46. * @return int
  47. */
  48. public function hit($key, $decayMinutes = 1)
  49. {
  50. $this->cache->add(
  51. $key.':timer', $this->availableAt($decayMinutes * 60), $decayMinutes
  52. );
  53. $added = $this->cache->add($key, 0, $decayMinutes);
  54. $hits = (int) $this->cache->increment($key);
  55. if (! $added && $hits == 1) {
  56. $this->cache->put($key, 1, $decayMinutes);
  57. }
  58. return $hits;
  59. }
  60. /**
  61. * Get the number of attempts for the given key.
  62. *
  63. * @param string $key
  64. * @return mixed
  65. */
  66. public function attempts($key)
  67. {
  68. return $this->cache->get($key, 0);
  69. }
  70. /**
  71. * Reset the number of attempts for the given key.
  72. *
  73. * @param string $key
  74. * @return mixed
  75. */
  76. public function resetAttempts($key)
  77. {
  78. return $this->cache->forget($key);
  79. }
  80. /**
  81. * Get the number of retries left for the given key.
  82. *
  83. * @param string $key
  84. * @param int $maxAttempts
  85. * @return int
  86. */
  87. public function retriesLeft($key, $maxAttempts)
  88. {
  89. $attempts = $this->attempts($key);
  90. return $maxAttempts - $attempts;
  91. }
  92. /**
  93. * Clear the hits and lockout timer for the given key.
  94. *
  95. * @param string $key
  96. * @return void
  97. */
  98. public function clear($key)
  99. {
  100. $this->resetAttempts($key);
  101. $this->cache->forget($key.':timer');
  102. }
  103. /**
  104. * Get the number of seconds until the "key" is accessible again.
  105. *
  106. * @param string $key
  107. * @return int
  108. */
  109. public function availableIn($key)
  110. {
  111. return $this->cache->get($key.':timer') - $this->currentTime();
  112. }
  113. }