Skip to content

Commit

Permalink
feat: add flush cache operation
Browse files Browse the repository at this point in the history
  • Loading branch information
pgautier404 committed Oct 13, 2023
1 parent 30106c6 commit 74d7ba4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Cache/CacheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:<br>
* * FlushCacheSuccess<br>
* * FlushCacheError<br>
* Pattern matching can be to operate on the appropriate subtype:<br>
* <code>if ($error = $response->asError()) {<br>
* &nbsp;&nbsp;// handle error condition<br>
* }</code>
*/
public function flushCache(string $cacheName): FlushCacheResponse
{
return $this->controlClient->flushCache($cacheName);
}

/**
* Set the value in cache with a given time to live (TTL) seconds.
*
Expand Down
59 changes: 59 additions & 0 deletions src/Cache/CacheOperationTypes/CacheOperationTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
* <code>
* if ($success = $response->asSuccess()) {
* // handle success if needed
* } elseif ($error = $response->asError())
* // handle error as appropriate
* }
* </code>
*/
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
Expand Down
20 changes: 20 additions & 0 deletions src/Cache/Internal/ScsControlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

}

0 comments on commit 74d7ba4

Please sign in to comment.