-
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
feat: add sorted-set examples #241
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"require": { | ||
"momentohq/client-sdk-php": "1.11.1", | ||
"momentohq/client-sdk-php": "v1.14.0", | ||
"monolog/monolog": "^2.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
require "vendor/autoload.php"; | ||
|
||
use Momento\Auth\CredentialProvider; | ||
use Momento\Cache\CacheClient; | ||
use Momento\Config\Configurations\Laptop; | ||
use Momento\Logging\StderrLoggerFactory; | ||
use Momento\Requests\CollectionTtl; | ||
use Psr\Log\LoggerInterface; | ||
|
||
$CACHE_NAME = uniqid("php-sorted-set-example-"); | ||
$SET_NAME = "example-sorted-set"; | ||
$ITEM_DEFAULT_TTL_SECONDS = 60; | ||
|
||
// Setup | ||
$authProvider = CredentialProvider::fromEnvironmentVariable("MOMENTO_API_KEY"); | ||
$configuration = Laptop::latest(new StderrLoggerFactory()); | ||
$client = new CacheClient($configuration, $authProvider, $ITEM_DEFAULT_TTL_SECONDS); | ||
$logger = $configuration->getLoggerFactory()->getLogger("ex:"); | ||
|
||
function printBanner(string $message, LoggerInterface $logger): void | ||
{ | ||
$line = "******************************************************************"; | ||
$logger->info($line); | ||
$logger->info($message); | ||
$logger->info($line); | ||
} | ||
|
||
printBanner("* Momento Example Start *", $logger); | ||
|
||
// Ensure test cache exists | ||
$response = $client->createCache($CACHE_NAME); | ||
if ($response->asSuccess()) { | ||
$logger->info("Created cache " . $CACHE_NAME . "\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error creating cache: " . $response->asError()->message() . "\n"); | ||
exit(1); | ||
} elseif ($response->asAlreadyExists()) { | ||
$logger->info("Cache " . $CACHE_NAME . " already exists.\n"); | ||
} | ||
|
||
// The CollectionTtl object is used to control TTLs for collections as they | ||
// are updated. By default, it is configured to reset the TTL for the collection | ||
// to the client's default TTL each time the collection is updated. | ||
$collectionTtl = new CollectionTtl(); | ||
|
||
// add element to sorted set | ||
$logger->info("Adding element 'one' to sorted set $SET_NAME\n"); | ||
$response = $client->sortedSetPutElement($CACHE_NAME, $SET_NAME, "one", 1.0, $collectionTtl); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS: Added element to set\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error adding element to set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
// add multiple elements to sorted set | ||
$logger->info("Adding elements 'two', 'three', 'four', 'five' to sorted set $SET_NAME\n"); | ||
$response = $client->sortedSetPutElements($CACHE_NAME, $SET_NAME, [ | ||
"two" => 2.0, | ||
"three" => 3.0, | ||
"four" => 4.0, | ||
"five" => 5.0 | ||
], $collectionTtl); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS: Added elements to set\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error adding elements to set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
// fetch sorted set by rank (0 to -1 for all elements) in ascending order | ||
$logger->info("Fetching sorted set $SET_NAME\n"); | ||
$response = $client->sortedSetFetchByRank($CACHE_NAME, $SET_NAME, 0, -1); | ||
if ($response->asHit()) { | ||
$logger->info("SUCCESS: Sorted set $SET_NAME: " . implode(', ', $response->asHit()->valuesArray()) . "\n"); | ||
} elseif ($err = $response->asMiss()) { | ||
$logger->info("Sorted set $SET_NAME not found\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error fetching sorted set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
// fetch sorted set by score (0 to 2) in ascending order | ||
$logger->info("Fetching sorted set $SET_NAME by score\n"); | ||
$response = $client->sortedSetFetchByScore($CACHE_NAME, $SET_NAME, 0, 2); | ||
if ($response->asHit()) { | ||
$logger->info("SUCCESS: Sorted set $SET_NAME: " . implode(', ', $response->asHit()->valuesArray()) . "\n"); | ||
} elseif ($err = $response->asMiss()) { | ||
$logger->info("Sorted set $SET_NAME not found\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error fetching sorted set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
// increment score of element in sorted set | ||
$logger->info("Incrementing score of element 'one' in sorted set $SET_NAME by 1\n"); | ||
$response = $client->sortedSetIncrementScore($CACHE_NAME, $SET_NAME, "one", 1); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error incrementing score of element in set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
// get score of element in sorted set | ||
$logger->info("Getting score of element 'one' in sorted set $SET_NAME\n"); | ||
$response = $client->sortedSetGetScore($CACHE_NAME, $SET_NAME, "one"); | ||
if ($response->asHit()) { | ||
$logger->info("SUCCESS: Score of element 'one' in set $SET_NAME: " . $response->asHit()->score() . "\n"); | ||
} elseif ($err = $response->asMiss()) { | ||
$logger->info("Element 'one' not found in set $SET_NAME\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error getting score of element in set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
// get length of sorted set by score | ||
$logger->info("Getting length of sorted set $SET_NAME\n"); | ||
$response = $client->sortedSetLengthByScore($CACHE_NAME, $SET_NAME); | ||
if ($response->asHit()) { | ||
$logger->info("SUCCESS: Length of set $SET_NAME: " . $response->asHit()->length() . "\n"); | ||
} elseif ($err = $response->asMiss()) { | ||
$logger->info("Set $SET_NAME not found\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error getting length of set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
|
||
// Remove element from sorted set | ||
$logger->info("Removing element 'one' from $SET_NAME\n"); | ||
$response = $client->sortedSetRemoveElement($CACHE_NAME, $SET_NAME, "one"); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS: Removed element from set\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error removing element from set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
// Remove multiple elements from sorted set | ||
$logger->info("Removing elements 'two', 'three' from $SET_NAME\n"); | ||
$response = $client->sortedSetRemoveElements($CACHE_NAME, $SET_NAME, ["two", "three"]); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS: Removed elements from set\n"); | ||
} elseif ($err = $response->asError()) { | ||
$logger->info("Error removing elements from set: {$err->message()}\n"); | ||
exit(1); | ||
} | ||
|
||
// Delete test cache | ||
$logger->info("Deleting cache $CACHE_NAME\n"); | ||
$response = $client->deleteCache($CACHE_NAME); | ||
if ($response->asError()) { | ||
$logger->info("Error deleting cache: " . $response->asError()->message() . "\n"); | ||
exit(1); | ||
} | ||
|
||
printBanner("* Momento Example End *", $logger); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why not leave off the ranks for fetching all elements?
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 am looking into that. It was giving me error saying: