-
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.
- Loading branch information
1 parent
f9bc2dc
commit 5f6867b
Showing
10 changed files
with
224 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
require "vendor/autoload.php"; | ||
|
||
use Momento\Auth\EnvMomentoTokenProvider; | ||
use Momento\Auth\EnvMomentoTokenProxyProvider; | ||
use Momento\Cache\SimpleCacheClient; | ||
use Momento\Config\Configurations\Laptop; | ||
use Monolog\Formatter\LineFormatter; | ||
use Monolog\Handler\StreamHandler; | ||
use Monolog\Logger; | ||
use Psr\Log\LoggerInterface; | ||
|
||
$CACHE_NAME = getenv("CACHE_NAME"); | ||
if (!$CACHE_NAME) { | ||
print "Error: Environment variable CACHE_NAME was not found.\n"; | ||
exit; | ||
} | ||
$ITEM_DEFAULT_TTL_SECONDS = 60; | ||
$KEY = "MyKey"; | ||
$VALUE = "MyValue"; | ||
|
||
// Setup | ||
$authProvider = new EnvMomentoTokenProxyProvider( | ||
"MOMENTO_AUTH_TOKEN", | ||
"momento-control:4443", | ||
"momento-cache:4444" | ||
); | ||
|
||
// Use your favorite PSR-3 logger. | ||
$logger = new Logger("example"); | ||
$streamHandler = new StreamHandler("php://stderr"); | ||
$formatter = new LineFormatter("%message%\n"); | ||
$streamHandler->setFormatter($formatter); | ||
$logger->pushHandler($streamHandler); | ||
|
||
// Or use the built-in minimal logger, equivalent to the above Monolog configuration. | ||
//$logger = \Momento\Utilities\LoggingHelper::getMinimalLogger(); | ||
// Or discard all log messages. | ||
//$logger = \Momento\Utilities\LoggingHelper::getNullLogger(); | ||
|
||
$configuration = Laptop::latest($logger); | ||
$client = new SimpleCacheClient($configuration, $authProvider, $ITEM_DEFAULT_TTL_SECONDS); | ||
|
||
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; | ||
} elseif ($response->asAlreadyExists()) { | ||
$logger->info("Cache " . $CACHE_NAME . " already exists.\n"); | ||
} | ||
|
||
// List cache | ||
$response = $client->listCaches(); | ||
if ($response->asSuccess()) { | ||
while (true) { | ||
$logger->info("SUCCESS: List caches: \n"); | ||
foreach ($response->asSuccess()->caches() as $cache) { | ||
$cacheName = $cache->name(); | ||
$logger->info("$cacheName\n"); | ||
} | ||
$nextToken = $response->asSuccess()->nextToken(); | ||
if (!$nextToken) { | ||
break; | ||
} | ||
$response = $client->listCaches($nextToken); | ||
} | ||
$logger->info("\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error listing cache: " . $response->asError()->message() . "\n"); | ||
exit; | ||
} | ||
|
||
// Set | ||
$logger->info("Setting key: $KEY to value: $VALUE\n"); | ||
$response = $client->set($CACHE_NAME, $KEY, $VALUE); | ||
if ($response->asSuccess()) { | ||
$logger->info("SUCCESS: - Set key: " . $KEY . " value: " . $VALUE . " cache: " . $CACHE_NAME . "\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error setting key: " . $response->asError()->message() . "\n"); | ||
exit; | ||
} | ||
|
||
// Get | ||
$logger->info("Getting value for key: $KEY\n"); | ||
$response = $client->get($CACHE_NAME, $KEY); | ||
if ($response->asHit()) { | ||
$logger->info("SUCCESS: - Get key: " . $KEY . " value: " . $response->asHit()->valueString() . " cache: " . $CACHE_NAME . "\n"); | ||
} elseif ($response->asMiss()) { | ||
$logger->info("Get operation was a MISS\n"); | ||
} elseif ($response->asError()) { | ||
$logger->info("Error getting cache: " . $response->asError()->message() . "\n"); | ||
exit; | ||
} | ||
|
||
printBanner("* Momento 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Momento\Auth; | ||
|
||
use Momento\Cache\Errors\InvalidArgumentError; | ||
use function Momento\Utilities\isNullOrEmpty; | ||
|
||
class EnvMomentoTokenProxyProvider implements ICredentialProvider | ||
{ | ||
|
||
private string $authToken; | ||
private string $controlProxyEndpoint; | ||
private string $cacheProxyEndpoint; | ||
private string $controlEndpoint; | ||
private string $cacheEndpoint; | ||
|
||
public function __construct( | ||
string $envVariableName, | ||
string $controlProxyEndpoint, | ||
string $cacheProxyEndpoint | ||
) | ||
{ | ||
$this->controlProxyEndpoint = $controlProxyEndpoint; | ||
$this->cacheProxyEndpoint = $cacheProxyEndpoint; | ||
$authToken = getenv($envVariableName); | ||
if ($authToken === false || isNullOrEmpty($authToken)) { | ||
throw new InvalidArgumentError("Environment variable $envVariableName is empty or null."); | ||
} | ||
$payload = AuthUtils::parseAuthToken($authToken); | ||
$this->authToken = $authToken; | ||
$this->controlEndpoint = $payload->cp; | ||
$this->cacheEndpoint = $payload->c; | ||
} | ||
|
||
public function getAuthToken(): string | ||
{ | ||
return $this->authToken; | ||
} | ||
|
||
public function getControlProxyEndpoint(): string|null | ||
{ | ||
return $this->controlProxyEndpoint; | ||
} | ||
|
||
public function getCacheProxyEndpoint(): string|null | ||
{ | ||
return $this->cacheProxyEndpoint; | ||
} | ||
|
||
public function getControlEndpoint(): string | ||
{ | ||
return $this->controlEndpoint; | ||
} | ||
|
||
public function getCacheEndpoint(): string | ||
{ | ||
return $this->cacheEndpoint; | ||
} | ||
} |
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
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