Skip to content

Commit

Permalink
feat: adding list API (#16)
Browse files Browse the repository at this point in the history
* feat: adding list API

* adding a missing timeout

* testing improvement

* add method for handling ttl validation and conversion

* README appeasement

Co-authored-by: Pete Gautier <pete@momentohq.com>
  • Loading branch information
pgautier404 and pgautier404 authored Oct 13, 2022
1 parent 8979539 commit d147005
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Japanese: [日本語](README.ja.md)

**IDE Notes**: You'll most likely want to use an IDE that supports PHP development, such as [PhpStorm](https://www.jetbrains.com/phpstorm/) or [Microsoft Visual Studio Code](https://code.visualstudio.com/).

### Examples

Check out full working code in [the examples directory](examples/) of this repository!

### Installation

Install composer [as described on the composer website](https://getcomposer.org/doc/00-intro.md).
Expand Down
145 changes: 132 additions & 13 deletions src/Cache/CacheOperationTypes/CacheOperationTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@
namespace Momento\Cache\CacheOperationTypes;

use Cache_client\_GetResponse;
use Cache_client\_ListFetchResponse;
use Control_client\_ListCachesResponse;
use Cache_client\_SetResponse;
use Momento\Cache\Errors\MomentoErrorCode;
use Momento\Cache\Errors\SdkError;

class CacheInfo
{
private string $name;

public function __construct($grpcListedCache) {
$this->name = $grpcListedCache->getCacheName();
}

public function name() : string {
return $this->name;
}
}

trait ErrorBody {
private SdkError $innerException;
private MomentoErrorCode $errorCode;
Expand Down Expand Up @@ -188,19 +202,6 @@ class ListCachesResponseError extends ListCachesResponse
use ErrorBody;
}

class CacheInfo
{
private string $name;

public function __construct($grpcListedCache) {
$this->name = $grpcListedCache->getCacheName();
}

public function name() : string {
return $this->name;
}
}

abstract class CacheSetResponse extends ResponseBase
{
public function asSuccess(): CacheSetResponseSuccess|null
Expand Down Expand Up @@ -322,3 +323,121 @@ class CacheDeleteResponseError extends CacheDeleteResponse
{
use ErrorBody;
}

abstract class CacheListFetchResponse extends ResponseBase
{
public function asHit() : CacheListFetchResponseHit|null
{
if ($this->isHit())
{
return $this;
}
return null;
}

public function asMiss() : CacheListFetchResponseMiss|null
{
if ($this->isMiss())
{
return $this;
}
return null;
}

public function asError() : CacheListFetchResponseError|null
{
if ($this->isError())
{
return $this;
}
return null;
}
}

class CacheListFetchResponseHit extends CacheListFetchResponse
{

private array $values = [];

public function __construct(_ListFetchResponse $response)
{
parent::__construct();
if ($response->getFound())
{
foreach ($response->getFound()->getValues() as $value)
{
$this->values[] = $value;
}
}
}

public function values() : array
{
return $this->values;
}

}

class CacheListFetchResponseMiss extends CacheListFetchResponse { }

class CacheListFetchResponseError extends CacheListFetchResponse
{
use ErrorBody;
}

abstract class CacheListPushFrontResponse extends ResponseBase
{
public function asSuccess() : CacheListPushFrontResponseSuccess|null
{
if ($this->isSuccess())
{
return $this;
}
return null;
}

public function asError() : CacheListPushFrontResponseError|null
{
if ($this->isError())
{
return $this;
}
return null;
}
}

class CacheListPushFrontResponseSuccess extends CacheListPushFrontResponse { }

class CacheListPushFrontResponseError extends CacheListPushFrontResponse
{
use ErrorBody;
}

abstract class CacheListPushBackResponse extends ResponseBase
{
public function asSuccess() : CacheListPushBackResponseSuccess|null
{
if ($this->isSuccess())
{
return $this;
}
return null;
}

public function asError() : CacheListPushBackResponseError|null
{
if ($this->isError())
{
return $this;
}
return null;
}
}

class CacheListPushBackResponseSuccess extends CacheListPushBackResponse { }

class CacheListPushBackResponseError extends CacheListPushBackResponse
{
use ErrorBody;
}

22 changes: 22 additions & 0 deletions src/Cache/SimpleCacheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Momento\Cache;

use Momento\Auth\ICredentialProvider;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheListPushBackResponse;
use Momento\Cache\CacheOperationTypes\CacheListPushFrontResponse;

class SimpleCacheClient
{
Expand Down Expand Up @@ -55,4 +58,23 @@ public function delete(string $cacheName, string $key) : CacheOperationTypes\Cac
{
return $this->dataClient->delete($cacheName, $key);
}

public function listFetch(string $cacheName, string $listName) : CacheListFetchResponse
{
return $this->dataClient->listFetch($cacheName, $listName);
}

public function listPushFront(
string $cacheName, string $listName, string$value, bool $refreshTtl, ?int $ttlSeconds=null, ?int $truncateBackToSize=null
) : CacheListPushFrontResponse
{
return $this->dataClient->listPushFront($cacheName, $listName, $value, $refreshTtl, $truncateBackToSize, $ttlSeconds);
}

public function listPushBack(
string $cacheName, string $listName, string$value, bool $refreshTtl, ?int $ttlSeconds=null, ?int $truncateFrontToSize=null
) : CacheListPushBackResponse
{
return $this->dataClient->listPushBack($cacheName, $listName, $value, $refreshTtl, $truncateFrontToSize, $ttlSeconds);
}
}
111 changes: 105 additions & 6 deletions src/Cache/_ScsDataClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@

use Cache_client\_DeleteRequest;
use Cache_client\_GetRequest;
use Cache_client\_ListFetchRequest;
use Cache_client\_ListPushBackRequest;
use Cache_client\_ListPushFrontRequest;
use Cache_client\_SetRequest;

use Cache_client\ECacheResult;
use Grpc\UnaryCall;
use Momento\Cache\CacheOperationTypes\CacheDeleteResponse;
use Momento\Cache\CacheOperationTypes\CacheDeleteResponseError;
use Momento\Cache\CacheOperationTypes\CacheDeleteResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheGetResponse;
use Momento\Cache\CacheOperationTypes\CacheGetResponseError;
use Momento\Cache\CacheOperationTypes\CacheGetResponseHit;
use Momento\Cache\CacheOperationTypes\CacheGetResponseMiss;
use Momento\Cache\CacheOperationTypes\CacheGetStatus;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponseError;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponseHit;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponseMiss;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheListPushBackResponse;
use Momento\Cache\CacheOperationTypes\CacheListPushBackResponseError;
use Momento\Cache\CacheOperationTypes\CacheListPushBackResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheListPushFrontResponse;
use Momento\Cache\CacheOperationTypes\CacheListPushFrontResponseError;
use Momento\Cache\CacheOperationTypes\CacheListPushFrontResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheSetResponse;
use Momento\Cache\CacheOperationTypes\CacheSetResponseError;
use Momento\Cache\CacheOperationTypes\CacheSetResponseSuccess;
Expand All @@ -38,7 +52,6 @@ class _ScsDataClient
private static int $TIMEOUT_MULTIPLIER = 1000000;
private int $deadline_seconds;
private int $defaultTtlSeconds;
private string $endpoint;
private _DataGrpcManager $grpcManager;

public function __construct(string $authToken, string $endpoint, int $defaultTtlSeconds, ?int $operationTimeoutMs)
Expand All @@ -48,7 +61,15 @@ public function __construct(string $authToken, string $endpoint, int $defaultTtl
$this->defaultTtlSeconds = $defaultTtlSeconds;
$this->deadline_seconds = $operationTimeoutMs ? $operationTimeoutMs / 1000.0 : self::$DEFAULT_DEADLINE_SECONDS;
$this->grpcManager = new _DataGrpcManager($authToken, $endpoint);
$this->endpoint = $endpoint;
}

private function ttlToMillis(?int $ttl=null) : int
{
if (!$ttl) {
$ttl = $this->defaultTtlSeconds;
}
validateTtl($ttl);
return $ttl * 1000;
}

private function processCall(UnaryCall $call) : mixed
Expand All @@ -64,12 +85,11 @@ public function set(string $cacheName, string $key, string $value, int $ttlSecon
{
try {
validateCacheName($cacheName);
$itemTtlSeconds = $ttlSeconds ? $ttlSeconds : $this->defaultTtlSeconds;
validateTtl($itemTtlSeconds);
$ttlMillis = $this->ttlToMillis($ttlSeconds);
$setRequest = new _SetRequest();
$setRequest->setCacheKey($key);
$setRequest->setCacheBody($value);
$setRequest->setTtlMilliseconds($itemTtlSeconds * 1000);
$setRequest->setTtlMilliseconds($ttlMillis);
$call = $this->grpcManager->client->Set(
$setRequest, ["cache"=>[$cacheName]], ["timeout"=>$this->deadline_seconds * self::$TIMEOUT_MULTIPLIER]
);
Expand Down Expand Up @@ -120,9 +140,88 @@ public function delete(string $cacheName, string $key) : CacheDeleteResponse
} catch (SdkError $e) {
return new CacheDeleteResponseError($e);
} catch (\Exception $e){
return new CacheGetResponseError(new UnknownError($e->getMessage()));
return new CacheDeleteResponseError(new UnknownError($e->getMessage()));
}
return new CacheDeleteResponseSuccess();
}

public function listFetch(string $cacheName, string $listName) : CacheListFetchResponse
{
try {
validateCacheName($cacheName);
validateListName($listName);
$listFetchRequest = new _ListFetchRequest();
$listFetchRequest->setListName($listName);
$call = $this->grpcManager->client->ListFetch(
$listFetchRequest, ["cache"=>[$cacheName]], ["timeout" => $this->deadline_seconds * self::$TIMEOUT_MULTIPLIER]
);
$response = $this->processCall($call);
} catch (SdkError $e) {
return new CacheListFetchResponseError($e);
} catch (\Exception $e){
return new CacheListFetchResponseError(new UnknownError($e->getMessage()));
}
if (!$response->hasFound())
{
return new CacheListFetchResponseMiss();
}
return new CacheListFetchResponseHit($response);
}

public function listPushFront(
string $cacheName, string $listName, string $value, bool $refreshTtl, ?int $truncateBackToSize=null, ?int $ttlSeconds=null
) : CacheListPushFrontResponse
{
try {
validateCacheName($cacheName);
validateListName($listName);
$ttlMillis = $this->ttlToMillis($ttlSeconds);
$listPushFrontRequest = new _ListPushFrontRequest();
$listPushFrontRequest->setListName($listName);
$listPushFrontRequest->setValue($value);
$listPushFrontRequest->setRefreshTtl($refreshTtl);
$listPushFrontRequest->setTtlMilliseconds($ttlMillis);
if (!is_null($truncateBackToSize)) {
$listPushFrontRequest->setTruncateTailToSize($truncateBackToSize);
}
$call = $this->grpcManager->client->ListPushFront(
$listPushFrontRequest, ["cache" => [$cacheName]], ["timeout" => $this->deadline_seconds * self::$TIMEOUT_MULTIPLIER]
);
$response = $this->processCall($call);
} catch (SdkError $e) {
return new CacheListPushFrontResponseError($e);
} catch (\Exception $e) {
return new CacheListPushFrontResponseError(new UnknownError($e->getMessage()));
}
return new CacheListPushFrontResponseSuccess($response);
}

public function listPushBack(
string $cacheName, string $listName, string $value, bool $refreshTtl, ?int $truncateFrontToSize=null, ?int $ttlSeconds=null
) : CacheListPushBackResponse
{
try {
validateCacheName($cacheName);
validateListName($listName);
$ttlMillis = $this->ttlToMillis($ttlSeconds);
$listPushBackRequest = new _ListPushBackRequest();
$listPushBackRequest->setListName($listName);
$listPushBackRequest->setValue($value);
$listPushBackRequest->setRefreshTtl($refreshTtl);
$listPushBackRequest->setTtlMilliseconds($ttlMillis);
if (!is_null($truncateFrontToSize)) {
$listPushBackRequest->setTruncateHeadToSize($truncateFrontToSize);
}
$call = $this->grpcManager->client->ListPushBack(
$listPushBackRequest, ["cache" => [$cacheName]], ["timeout" => $this->deadline_seconds * self::$TIMEOUT_MULTIPLIER]
);
$this->processCall($call);
} catch (SdkError $e) {
return new CacheListPushBackResponseError($e);
} catch (\Exception $e) {
return new CacheListPushBackResponseError(new UnknownError($e->getMessage()));
}
return new CacheListPushBackResponseSuccess();
}

}
Loading

0 comments on commit d147005

Please sign in to comment.