-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: make $refreshTtl
optional and re-order arguments for list push and dictionary increment
#77
Changes from 6 commits
6cc0d26
2512025
1d5d4ae
9be7921
2e8af3a
d0dea01
4f987c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,8 @@ | |
use Momento\Cache\Errors\SdkError; | ||
use Momento\Cache\Errors\UnknownError; | ||
use Momento\Config\IConfiguration; | ||
use Momento\Requests\CollectionTtl; | ||
use Momento\Requests\CollectionTtlFactory; | ||
use Momento\Utilities\_ErrorConverter; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
@@ -175,6 +177,14 @@ private function ttlToMillis(?int $ttl = null): int | |
return $ttl * 1000; | ||
} | ||
|
||
private function returnCollectionTtl(?CollectionTtl $ttl): CollectionTtl | ||
{ | ||
if (!$ttl) { | ||
return CollectionTtl::fromCacheTtl(); | ||
} | ||
return $ttl; | ||
} | ||
|
||
private function processCall(UnaryCall $call): mixed | ||
{ | ||
[$response, $status] = $call->wait(); | ||
|
@@ -271,18 +281,19 @@ public function listFetch(string $cacheName, string $listName): CacheListFetchRe | |
} | ||
|
||
public function listPushFront( | ||
string $cacheName, string $listName, string $value, bool $refreshTtl, ?int $truncateBackToSize = null, ?int $ttlSeconds = null | ||
string $cacheName, string $listName, string $value, ?int $truncateBackToSize = null, CollectionTtl $ttl = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above: please make all the CollectionTtl parameters in this file explicitly nullable |
||
): CacheListPushFrontResponse | ||
{ | ||
try { | ||
$collectionTtl = $this->returnCollectionTtl($ttl); | ||
validateCacheName($cacheName); | ||
validateListName($listName); | ||
validateTruncateSize($truncateBackToSize); | ||
$ttlMillis = $this->ttlToMillis($ttlSeconds); | ||
$ttlMillis = $this->ttlToMillis($collectionTtl->getTtl()); | ||
$listPushFrontRequest = new _ListPushFrontRequest(); | ||
$listPushFrontRequest->setListName($listName); | ||
$listPushFrontRequest->setValue($value); | ||
$listPushFrontRequest->setRefreshTtl($refreshTtl); | ||
$listPushFrontRequest->setRefreshTtl($collectionTtl->getRefreshTtl()); | ||
$listPushFrontRequest->setTtlMilliseconds($ttlMillis); | ||
if (!is_null($truncateBackToSize)) { | ||
$listPushFrontRequest->setTruncateBackToSize($truncateBackToSize); | ||
|
@@ -300,18 +311,19 @@ public function listPushFront( | |
} | ||
|
||
public function listPushBack( | ||
string $cacheName, string $listName, string $value, bool $refreshTtl, ?int $truncateFrontToSize = null, ?int $ttlSeconds = null | ||
string $cacheName, string $listName, string $value, ?int $truncateFrontToSize = null, CollectionTtl $ttl = null | ||
): CacheListPushBackResponse | ||
{ | ||
try { | ||
$collectionTtl = $this->returnCollectionTtl($ttl); | ||
validateCacheName($cacheName); | ||
validateListName($listName); | ||
validateTruncateSize($truncateFrontToSize); | ||
$ttlMillis = $this->ttlToMillis($ttlSeconds); | ||
$ttlMillis = $this->ttlToMillis($collectionTtl->getTtl()); | ||
$listPushBackRequest = new _ListPushBackRequest(); | ||
$listPushBackRequest->setListName($listName); | ||
$listPushBackRequest->setValue($value); | ||
$listPushBackRequest->setRefreshTtl($refreshTtl); | ||
$listPushBackRequest->setRefreshTtl($collectionTtl->getRefreshTtl()); | ||
$listPushBackRequest->setTtlMilliseconds($ttlMillis); | ||
if (!is_null($truncateFrontToSize)) { | ||
$listPushBackRequest->setTruncateFrontToSize($truncateFrontToSize); | ||
|
@@ -442,19 +454,20 @@ public function listErase(string $cacheName, string $listName, ?int $beginIndex | |
return new CacheListEraseResponseSuccess(); | ||
} | ||
|
||
public function dictionarySetField(string $cacheName, string $dictionaryName, string $field, string $value, bool $refreshTtl, ?int $ttlSeconds = null): CacheDictionarySetFieldResponse | ||
public function dictionarySetField(string $cacheName, string $dictionaryName, string $field, string $value, CollectionTtl $ttl = null): CacheDictionarySetFieldResponse | ||
{ | ||
try { | ||
$collectionTtl = $this->returnCollectionTtl($ttl); | ||
validateCacheName($cacheName); | ||
validateDictionaryName($dictionaryName); | ||
validateFieldName($field); | ||
validateValueName($value); | ||
$ttlMillis = $this->ttlToMillis($ttlSeconds); | ||
$ttlMillis = $this->ttlToMillis($collectionTtl->getTtl()); | ||
validateTtl($ttlMillis); | ||
$dictionarySetFieldRequest = new _DictionarySetRequest(); | ||
$dictionarySetFieldRequest->setDictionaryName($dictionaryName); | ||
$dictionarySetFieldRequest->setItems([$this->toSingletonFieldValuePair($field, $value)]); | ||
$dictionarySetFieldRequest->setRefreshTtl($refreshTtl); | ||
$dictionarySetFieldRequest->setRefreshTtl($collectionTtl->getRefreshTtl()); | ||
$dictionarySetFieldRequest->setTtlMilliseconds($ttlMillis); | ||
$call = $this->grpcManager->client->DictionarySet($dictionarySetFieldRequest, ["cache" => [$cacheName]], ["timeout" => $this->timeout]); | ||
$this->processCall($call); | ||
|
@@ -541,14 +554,15 @@ public function dictionaryFetch(string $cacheName, string $dictionaryName): Cach | |
return new CacheDictionaryFetchResponseMiss(); | ||
} | ||
|
||
public function dictionarySetFields(string $cacheName, string $dictionaryName, array $items, bool $refreshTtl, ?int $ttlSeconds = null): CacheDictionarySetFieldsResponse | ||
public function dictionarySetFields(string $cacheName, string $dictionaryName, array $items, CollectionTtl $ttl = null): CacheDictionarySetFieldsResponse | ||
{ | ||
try { | ||
$collectionTtl = $this->returnCollectionTtl($ttl); | ||
validateCacheName($cacheName); | ||
validateDictionaryName($dictionaryName); | ||
validateItems($items); | ||
validateFieldsKeys($items); | ||
$ttlMillis = $this->ttlToMillis($ttlSeconds); | ||
$ttlMillis = $this->ttlToMillis($collectionTtl->getTtl()); | ||
$protoItems = []; | ||
foreach ($items as $field => $value) { | ||
$fieldValuePair = new _DictionaryFieldValuePair(); | ||
|
@@ -558,7 +572,7 @@ public function dictionarySetFields(string $cacheName, string $dictionaryName, a | |
} | ||
$dictionarySetFieldsRequest = new _DictionarySetRequest(); | ||
$dictionarySetFieldsRequest->setDictionaryName($dictionaryName); | ||
$dictionarySetFieldsRequest->setRefreshTtl($refreshTtl); | ||
$dictionarySetFieldsRequest->setRefreshTtl($collectionTtl->getRefreshTtl()); | ||
$dictionarySetFieldsRequest->setItems($protoItems); | ||
$dictionarySetFieldsRequest->setTtlMilliseconds($ttlMillis); | ||
$call = $this->grpcManager->client->DictionarySet($dictionarySetFieldsRequest, ["cache" => [$cacheName]], ["timeout" => $this->timeout]); | ||
|
@@ -595,21 +609,22 @@ public function dictionaryGetFields(string $cacheName, string $dictionaryName, a | |
} | ||
|
||
public function dictionaryIncrement( | ||
string $cacheName, string $dictionaryName, string $field, bool $refreshTtl, int $amount = 1, ?int $ttlSeconds = null | ||
string $cacheName, string $dictionaryName, string $field, int $amount = 1, CollectionTtl $ttl = null | ||
): CacheDictionaryIncrementResponse | ||
{ | ||
try { | ||
$collectionTtl = $this->returnCollectionTtl($ttl); | ||
validateCacheName($cacheName); | ||
validateDictionaryName($dictionaryName); | ||
validateFieldName($field); | ||
$ttlMillis = $this->ttlToMillis($ttlSeconds); | ||
$ttlMillis = $this->ttlToMillis($collectionTtl->getTtl()); | ||
validateTtl($ttlMillis); | ||
$dictionaryIncrementRequest = new _DictionaryIncrementRequest(); | ||
$dictionaryIncrementRequest | ||
->setDictionaryName($dictionaryName) | ||
->setField($field) | ||
->setAmount($amount) | ||
->setRefreshTtl($refreshTtl) | ||
->setRefreshTtl($collectionTtl->getRefreshTtl()) | ||
->setTtlMilliseconds($ttlMillis); | ||
$call = $this->grpcManager->client->DictionaryIncrement( | ||
$dictionaryIncrementRequest, ["cache" => [$cacheName]], ["timeout" => $this->timeout] | ||
|
@@ -669,17 +684,18 @@ public function dictionaryRemoveFields(string $cacheName, string $dictionaryName | |
return new CacheDictionaryRemoveFieldsResponseSuccess(); | ||
} | ||
|
||
public function setAddElement(string $cacheName, string $setName, string $element, bool $refreshTt, ?int $ttlSeconds = null): CacheSetAddElementResponse | ||
public function setAddElement(string $cacheName, string $setName, string $element, CollectionTtl $ttl = null): CacheSetAddElementResponse | ||
{ | ||
try { | ||
$collectionTtl = $this->returnCollectionTtl($ttl); | ||
validateCacheName($cacheName); | ||
validateSetName($setName); | ||
validateElement($element); | ||
$ttlMillis = $this->ttlToMillis($ttlSeconds); | ||
$ttlMillis = $this->ttlToMillis($collectionTtl->getTtl()); | ||
validateTtl($ttlMillis); | ||
$setAddElementRequest = new _SetUnionRequest(); | ||
$setAddElementRequest->setSetName($setName); | ||
$setAddElementRequest->setRefreshTtl($refreshTt); | ||
$setAddElementRequest->setRefreshTtl($collectionTtl->getRefreshTtl()); | ||
$setAddElementRequest->setTtlMilliseconds($ttlMillis); | ||
$setAddElementRequest->setElements([$element]); | ||
$call = $this->grpcManager->client->SetUnion($setAddElementRequest, ["cache" => [$cacheName]], ["timeout" => $this->timeout]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Momento\Requests; | ||
|
||
class CollectionTtl | ||
{ | ||
private ?int $ttlSeconds; | ||
private ?bool $refreshTtl; | ||
|
||
public function __construct(?int $ttlSeconds = null, ?bool $refreshTtl = true) | ||
{ | ||
$this->ttlSeconds = $ttlSeconds; | ||
$this->refreshTtl = $refreshTtl; | ||
} | ||
|
||
public static function fromCacheTtl(): CollectionTtl | ||
{ | ||
return new CollectionTtl(null, true); | ||
} | ||
|
||
public static function of(int $ttl): CollectionTtl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'd prefer this parameter to match the constructor and be called |
||
{ | ||
return new CollectionTtl($ttl); | ||
} | ||
|
||
public function getTtl(): int|null | ||
{ | ||
return $this->ttlSeconds; | ||
} | ||
|
||
public function getRefreshTtl(): bool|null | ||
{ | ||
return $this->refreshTtl; | ||
} | ||
|
||
public function withRefreshTtlOnUpdates(): CollectionTtl | ||
{ | ||
return new CollectionTtl($this->ttlSeconds, $this->refreshTtl); | ||
} | ||
|
||
public function withNoRefreshTtlOnUpdates(): CollectionTtl | ||
{ | ||
return new CollectionTtl($this->ttlSeconds, false); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer the
CollectionTtl
parameter be explicitly nullable in all of these functions:It looks like the null assignment does that implicitly, but I think making it explicit and consistent with the rest of our nullable parameters will make it more readable and maintainable.