From 74d7ba4f396264109563c5aaadc778c9feef8a13 Mon Sep 17 00:00:00 2001 From: Pete Gautier Date: Fri, 13 Oct 2023 11:57:58 -0700 Subject: [PATCH] feat: add flush cache operation --- src/Cache/CacheClient.php | 19 ++++++ .../CacheOperationTypes.php | 59 +++++++++++++++++++ src/Cache/Internal/ScsControlClient.php | 20 +++++++ 3 files changed, 98 insertions(+) diff --git a/src/Cache/CacheClient.php b/src/Cache/CacheClient.php index 54ac88b1..7878765f 100644 --- a/src/Cache/CacheClient.php +++ b/src/Cache/CacheClient.php @@ -13,6 +13,7 @@ use Momento\Cache\CacheOperationTypes\DictionaryRemoveFieldsResponse; use Momento\Cache\CacheOperationTypes\DictionarySetFieldResponse; use Momento\Cache\CacheOperationTypes\DictionarySetFieldsResponse; +use Momento\Cache\CacheOperationTypes\FlushCacheResponse; use Momento\Cache\CacheOperationTypes\ResponseFuture; use Momento\Cache\CacheOperationTypes\GetResponse; use Momento\Cache\CacheOperationTypes\IncrementResponse; @@ -160,6 +161,24 @@ public function deleteCache(string $cacheName): DeleteCacheResponse return $this->controlClient->deleteCache($cacheName); } + /** + * Flush a cache. + * + * @param string $cacheName Name of the cache to delete. + * @return FlushCacheResponse Represents the result of the flush cache operation. This result is + * resolved to a type-safe object of one of the following types:
+ * * FlushCacheSuccess
+ * * FlushCacheError
+ * Pattern matching can be to operate on the appropriate subtype:
+ * if ($error = $response->asError()) {
+ *   // handle error condition
+ * }
+ */ + public function flushCache(string $cacheName): FlushCacheResponse + { + return $this->controlClient->flushCache($cacheName); + } + /** * Set the value in cache with a given time to live (TTL) seconds. * diff --git a/src/Cache/CacheOperationTypes/CacheOperationTypes.php b/src/Cache/CacheOperationTypes/CacheOperationTypes.php index c448b509..4bf5737a 100644 --- a/src/Cache/CacheOperationTypes/CacheOperationTypes.php +++ b/src/Cache/CacheOperationTypes/CacheOperationTypes.php @@ -458,6 +458,65 @@ class ListCachesError extends ListCachesResponse use ErrorBody; } +/** + * Parent response type for a flush cache request. The + * response object is resolved to a type-safe object of one of + * the following subtypes: + * + * * FlushCacheSuccess + * * FlushCacheError + * + * Pattern matching can be used to operate on the appropriate subtype. + * For example: + * + * if ($success = $response->asSuccess()) { + * // handle success if needed + * } elseif ($error = $response->asError()) + * // handle error as appropriate + * } + * + */ +abstract class FlushCacheResponse extends ResponseBase +{ + /** + * @return FlushCacheSuccess|null Returns the success subtype if the request was successful and null otherwise. + */ + public function asSuccess(): ?FlushCacheSuccess + { + if ($this->isSuccess()) { + return $this; + } + return null; + } + + /** + * @return FlushCacheError|null Returns the error subtype if the request returned an error and null otherwise. + */ + public function asError(): ?FlushCacheError + { + if ($this->isError()) { + return $this; + } + return null; + } + +} + +/** + * Indicates that the request that generated it was successful. + */ +class FlushCacheSuccess extends FlushCacheResponse +{ +} + +/** + * Contains information about an error returned from the request. + */ +class FlushCacheError extends FlushCacheResponse +{ + use ErrorBody; +} + /** * Parent response type for a set request. The * response object is resolved to a type-safe object of one of diff --git a/src/Cache/Internal/ScsControlClient.php b/src/Cache/Internal/ScsControlClient.php index 370d49e2..9fe8f2c2 100644 --- a/src/Cache/Internal/ScsControlClient.php +++ b/src/Cache/Internal/ScsControlClient.php @@ -5,6 +5,7 @@ use Control_client\_CreateCacheRequest; use Control_client\_DeleteCacheRequest; +use Control_client\_FlushCacheRequest; use Control_client\_ListCachesRequest; use Grpc\UnaryCall; use Momento\Auth\ICredentialProvider; @@ -15,6 +16,9 @@ use Momento\Cache\CacheOperationTypes\DeleteCacheResponse; use Momento\Cache\CacheOperationTypes\DeleteCacheError; use Momento\Cache\CacheOperationTypes\DeleteCacheSuccess; +use Momento\Cache\CacheOperationTypes\FlushCacheError; +use Momento\Cache\CacheOperationTypes\FlushCacheResponse; +use Momento\Cache\CacheOperationTypes\FlushCacheSuccess; use Momento\Cache\CacheOperationTypes\ListCachesResponse; use Momento\Cache\CacheOperationTypes\ListCachesError; use Momento\Cache\CacheOperationTypes\ListCachesSuccess; @@ -108,4 +112,20 @@ public function listCaches(?string $nextToken = null): ListCachesResponse return new ListCachesSuccess($response); } + public function flushCache(string $cacheName): FlushCacheResponse + { + try { + validateCacheName($cacheName); + $request = new _FlushCacheRequest(); + $request->setCacheName($cacheName); + $call = $this->grpcManager->client->FlushCache($request); + $this->processCall($call); + } catch (SdkError $e) { + return new FlushCacheError($e); + } catch (\Exception $e) { + return new FlushCacheError(new UnknownError($e->getMessage())); + } + return new FlushCacheSuccess(); + } + }