55use Sammyjo20 \Saloon \Constants \Saloon ;
66use Sammyjo20 \Saloon \Http \SaloonRequest ;
77use Sammyjo20 \Saloon \Http \SaloonResponse ;
8- use Sammyjo20 \SaloonCachePlugin \Interfaces \CacheDriver ;
8+ use Sammyjo20 \SaloonCachePlugin \Interfaces \CacheDriverInterface ;
99use Sammyjo20 \SaloonCachePlugin \Http \Middleware \ExplicitCacheMiddleware ;
1010
1111trait AlwaysCacheResponses
@@ -27,6 +27,13 @@ trait AlwaysCacheResponses
2727 */
2828 protected bool $ cachingEnabled = true ;
2929
30+ /**
31+ * Should the existing cache be invalidated?
32+ *
33+ * @var bool
34+ */
35+ protected bool $ invalidateCache = false ;
36+
3037 /**
3138 * Boot the Saloon plugin
3239 *
@@ -50,7 +57,7 @@ public function bootAlwaysCacheResponses(SaloonRequest $request): void
5057
5158 // Run the custom cache middleware.
5259
53- $ request ->addHandler ('saloonCache ' , new ExplicitCacheMiddleware ($ request ));
60+ $ request ->addHandler ('saloonCache ' , new ExplicitCacheMiddleware ($ request, $ this -> invalidateCache ));
5461
5562 // We should also intercept the response and set the "cached" property to true.
5663
@@ -75,7 +82,7 @@ public function bootAlwaysCacheResponses(SaloonRequest $request): void
7582 * @throws \JsonException
7683 * @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException
7784 */
78- protected function cacheKey (SaloonRequest $ request , array $ headers, bool $ hashKey = true ): string
85+ protected function cacheKey (SaloonRequest $ request , array $ headers ): string
7986 {
8087 $ requestUrl = $ request ->getFullRequestUrl ();
8188 $ className = get_class ($ request );
@@ -121,12 +128,24 @@ public function disableCaching(): self
121128 return $ this ;
122129 }
123130
131+ /**
132+ * Invalidate the current cache and refresh the cache.
133+ *
134+ * @return $this
135+ */
136+ public function invalidateCache (): self
137+ {
138+ $ this ->invalidateCache = true ;
139+
140+ return $ this ;
141+ }
142+
124143 /**
125144 * Return an instance of the cache driver that should be used.
126145 *
127146 * @return mixed
128147 */
129- abstract public function cacheDriver (): CacheDriver ;
148+ abstract public function cacheDriver (): CacheDriverInterface ;
130149
131150 /**
132151 * Define the cache TTL (Time-to-live) in seconds.
0 commit comments