2929
3030final class FixedWindowThrottler extends AbstractWindowThrottler implements RetriableThrottlerInterface
3131{
32- const TIME_CACHE_KEY = ':time ' ;
33- const HITS_CACHE_KEY = ':hits ' ;
32+ const CACHE_KEY_TIME = ':time ' ;
33+ const CACHE_KEY_HITS = ':hits ' ;
3434
3535 /**
3636 * @var int|null
@@ -46,11 +46,11 @@ public function hit()
4646
4747 // Update the window start time if the previous window has passed, or no cached window exists
4848 try {
49- if (($ this ->timeProvider ->now () - $ this ->cache ->get ($ this ->key . self :: TIME_CACHE_KEY )) > $ this ->timeLimit ) {
50- $ this ->cache ->set ($ this ->key . self :: TIME_CACHE_KEY , $ this ->timeProvider ->now (), $ this ->cacheTtl );
49+ if (($ this ->timeProvider ->now () - $ this ->cache ->get ($ this ->getTimeCacheKey () )) > $ this ->timeLimit ) {
50+ $ this ->cache ->set ($ this ->getTimeCacheKey () , $ this ->timeProvider ->now (), $ this ->cacheTtl );
5151 }
5252 } catch (ItemNotFoundException $ exception ) {
53- $ this ->cache ->set ($ this ->key . self :: TIME_CACHE_KEY , $ this ->timeProvider ->now (), $ this ->cacheTtl );
53+ $ this ->cache ->set ($ this ->getTimeCacheKey () , $ this ->timeProvider ->now (), $ this ->cacheTtl );
5454 }
5555
5656 return $ this ;
@@ -62,7 +62,7 @@ public function hit()
6262 public function count ()
6363 {
6464 try {
65- if (($ this ->timeProvider ->now () - $ this ->cache ->get ($ this ->key . self :: TIME_CACHE_KEY )) > $ this ->timeLimit ) {
65+ if (($ this ->timeProvider ->now () - $ this ->cache ->get ($ this ->getTimeCacheKey () )) > $ this ->timeLimit ) {
6666 return 0 ;
6767 }
6868
@@ -78,7 +78,7 @@ public function count()
7878 public function clear ()
7979 {
8080 $ this ->setCachedHitCount (0 );
81- $ this ->cache ->set ($ this ->key . self :: TIME_CACHE_KEY , $ this ->timeProvider ->now (), $ this ->cacheTtl );
81+ $ this ->cache ->set ($ this ->getTimeCacheKey () , $ this ->timeProvider ->now (), $ this ->cacheTtl );
8282 }
8383
8484 /**
@@ -92,7 +92,9 @@ public function getRetryTimeout()
9292
9393 // Return the time until the current window ends
9494 // Try/catch for the ItemNotFoundException is not required, in that case $this->check() will return true
95- return 1e3 * ($ this ->timeLimit - $ this ->timeProvider ->now () + $ this ->cache ->get ($ this ->key .self ::TIME_CACHE_KEY ));
95+ $ cachedTime = $ this ->cache ->get ($ this ->getTimeCacheKey ());
96+
97+ return self ::SECOND_TO_MILLISECOND_MULTIPLIER * ($ this ->timeLimit - $ this ->timeProvider ->now () + $ cachedTime );
9698 }
9799
98100 /**
@@ -106,7 +108,7 @@ private function getCachedHitCount()
106108 return $ this ->hitCount ;
107109 }
108110
109- return $ this ->cache ->get ($ this ->key . self :: HITS_CACHE_KEY );
111+ return $ this ->cache ->get ($ this ->getHitsCacheKey () );
110112 }
111113
112114 /**
@@ -115,6 +117,22 @@ private function getCachedHitCount()
115117 private function setCachedHitCount ($ hitCount )
116118 {
117119 $ this ->hitCount = $ hitCount ;
118- $ this ->cache ->set ($ this ->key .self ::HITS_CACHE_KEY , $ hitCount , $ this ->cacheTtl );
120+ $ this ->cache ->set ($ this ->getHitsCacheKey (), $ hitCount , $ this ->cacheTtl );
121+ }
122+
123+ /**
124+ * @return string
125+ */
126+ private function getHitsCacheKey ()
127+ {
128+ return $ this ->key .self ::CACHE_KEY_HITS ;
129+ }
130+
131+ /**
132+ * @return string
133+ */
134+ private function getTimeCacheKey ()
135+ {
136+ return $ this ->key .self ::CACHE_KEY_TIME ;
119137 }
120138}
0 commit comments