-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: adding storage client * chore: add doc comments * chore: add storage client example script * chore: hack in support for bytes type * chore: clean up error message * chore: add toString for storage get response * chore: fix type specifier * chore: support multiple storage data clients * chore: regenerate protos * chore: mostly finished storage client * chore: 'not found' handling * chore: remove refs to deleted 'put' func and deprecated not found error * chore: better handling of not found errors * chore: PR feedback cleanup * chore: split out grpc config and transport strategies for storage * chore: return nil for type and found on error
- Loading branch information
1 parent
65abdd8
commit d6bcc98
Showing
86 changed files
with
2,757 additions
and
4,654 deletions.
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
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.4.0", | ||
"momentohq/client-sdk-php": "1.9.1", | ||
"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,125 @@ | ||
<?php | ||
declare(strict_types=1); | ||
require "vendor/autoload.php"; | ||
|
||
use Momento\Auth\CredentialProvider; | ||
use Momento\Config\Configurations\StorageLaptop; | ||
use Momento\Logging\StderrLoggerFactory; | ||
use Psr\Log\LoggerInterface; | ||
use Momento\Storage\PreviewStorageClient; | ||
use Momento\Storage\StorageOperationTypes\StorageValueType; | ||
|
||
$STORE_NAME = uniqid("php-storage-example-"); | ||
$VALUES = [ | ||
"str"=> "StringValue", | ||
"int" => 123, | ||
"double" => 123.456, | ||
"fakeout" => "123" | ||
]; | ||
|
||
// Setup | ||
$authProvider = CredentialProvider::fromEnvironmentVariable("MOMENTO_AUTH_TOKEN"); | ||
$configuration = StorageLaptop::latest(new StderrLoggerFactory()); | ||
$client = new PreviewStorageClient($configuration, $authProvider); | ||
$logger = $configuration->getLoggerFactory()->getLogger("ex:"); | ||
|
||
function printBanner(string $message, LoggerInterface $logger): void | ||
{ | ||
$line = "**************************************************************************"; | ||
$logger->info($line); | ||
$logger->info($message); | ||
$logger->info($line); | ||
} | ||
|
||
// Used to tear down temporary store after failure or completion of script | ||
function deleteStore(string $storeName, LoggerInterface $logger, PreviewStorageClient $client): void | ||
{ | ||
$logger->info("Deleting store $storeName\n"); | ||
$response = $client->deleteStore($storeName); | ||
if ($response->asError()) { | ||
$logger->info("Error deleting store: " . $response->asError()->message() . "\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
printBanner("* Momento Storage Example Start *", $logger); | ||
|
||
// Ensure test store exists | ||
$response = $client->createStore($STORE_NAME); | ||
if ($response->asSuccess()) { | ||
$logger->info("Created store " . $STORE_NAME . "\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error creating store: " . $response->asError()->message() . "\n"); | ||
exit(1); | ||
} elseif ($response->asAlreadyExists()) { | ||
$logger->info("Store " . $STORE_NAME . " already exists.\n"); | ||
} | ||
|
||
// List stores | ||
$response = $client->listStores(); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS: List stores: \n"); | ||
foreach ($response->asSuccess()->stores() as $store) { | ||
$storeName = $store->name(); | ||
$logger->info("$storeName\n"); | ||
} | ||
$logger->info("\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error listing store: " . $response->asError()->message() . "\n"); | ||
deleteStore($STORE_NAME, $logger, $client); | ||
exit(1); | ||
} | ||
|
||
// Set | ||
foreach ($VALUES as $key => $value) { | ||
$logger->info("Setting key: '$key' to value: '$value', type = " . get_class($value) . "\n"); | ||
$response = $client->set($STORE_NAME, $key, $value); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error setting key: " . $response->asError()->message() . "\n"); | ||
deleteStore($STORE_NAME, $logger, $client); | ||
exit(1); | ||
} | ||
} | ||
|
||
// Get | ||
foreach ($VALUES as $key => $value) { | ||
$logger->info("Getting value for key: '$key'\n"); | ||
$response = $client->get($STORE_NAME, $key); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS\n"); | ||
$valueType = $response->asSuccess()->type(); | ||
if ($valueType == StorageValueType::STRING) { | ||
print("Got string value: " . $response->asSuccess()->tryGetString() . "\n"); | ||
} elseif ($valueType == StorageValueType::INTEGER) { | ||
print("Got integer value: " . $response->asSuccess()->tryGetInteger() . "\n"); | ||
} elseif ($valueType == StorageValueType::DOUBLE) { | ||
print("Got double value: " . $response->asSuccess()->tryGetDouble() . "\n"); | ||
} elseif ($valueType == StorageValueType::BYTES) { | ||
// This case is not expected in this example as PHP doesn't have a native byte type | ||
print("Got bytes value: " . $response->asSuccess()->tryGetBytes() . "\n"); | ||
} | ||
} elseif ($response->asError()) { | ||
$logger->info("Error getting key: " . $response->asError()->message() . "\n"); | ||
deleteStore($STORE_NAME, $logger, $client); | ||
exit(1); | ||
} | ||
} | ||
|
||
// Delete | ||
foreach (array_keys($VALUES) as $key) { | ||
$logger->info("Deleting key: '$key'\n"); | ||
$response = $client->delete($STORE_NAME, $key); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error deleting key: " . $response->asError()->message() . "\n"); | ||
exit(1); | ||
} | ||
} | ||
|
||
// Delete store | ||
deleteStore($STORE_NAME, $logger, $client); | ||
|
||
printBanner("* Momento Storage Example End *", $logger); |
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
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
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
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
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,54 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Momento\Cache\Internal; | ||
|
||
use Momento\Config\IConfiguration; | ||
use Momento\Config\IStorageConfiguration; | ||
use Momento\Storage\Internal\StorageDataClient; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class IdleStorageDataClientWrapper implements LoggerAwareInterface { | ||
|
||
private StorageDataClient $client; | ||
private LoggerInterface $logger; | ||
private object $clientFactory; | ||
private ?int $maxIdleMillis; | ||
private int $lastAccessTime; | ||
|
||
public function __construct(object $clientFactory, IStorageConfiguration $configuration) { | ||
$this->clientFactory = $clientFactory; | ||
$this->client = ($clientFactory->callback)(); | ||
$this->logger = $configuration->getLoggerFactory()->getLogger(get_class($this)); | ||
$this->maxIdleMillis = $configuration->getTransportStrategy()->getMaxIdleMillis(); | ||
$this->lastAccessTime = $this->getMilliseconds(); | ||
} | ||
|
||
public function setLogger(LoggerInterface $logger): void | ||
{ | ||
$this->logger = $logger; | ||
} | ||
|
||
public function getClient(): StorageDataClient { | ||
if ($this->maxIdleMillis === null) { | ||
return $this->client; | ||
} | ||
$this->logger->debug("Checking to see if client has been idle for more than {$this->maxIdleMillis}"); | ||
if ($this->getMilliseconds() - $this->lastAccessTime > $this->maxIdleMillis) { | ||
$this->logger->debug("Client has been idle for more than {$this->maxIdleMillis}; reconnecting"); | ||
$this->client->close(); | ||
$this->client = ($this->clientFactory->callback)(); | ||
} | ||
$this->lastAccessTime = $this->getMilliseconds(); | ||
return $this->client; | ||
} | ||
|
||
public function close(): void { | ||
$this->client->close(); | ||
} | ||
|
||
private function getMilliseconds(): int { | ||
return (int)(gettimeofday(true) * 1000); | ||
} | ||
} |
Oops, something went wrong.