-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpsr16-example.php
More file actions
51 lines (42 loc) · 1.51 KB
/
psr16-example.php
File metadata and controls
51 lines (42 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
require "vendor/autoload.php";
use Momento\Auth\CredentialProvider;
use Momento\Cache\Psr16CacheClient;
use Momento\Config\Configurations\Laptop;
use Momento\Logging\StderrLoggerFactory;
use Psr\Log\LoggerInterface;
$ITEM_DEFAULT_TTL_SECONDS = 60;
$KEY = "MyKey";
$VALUE = "MyValue";
// Setup
$authProvider = CredentialProvider::fromEnvironmentVariablesV2();
$configuration = Laptop::latest(new StderrLoggerFactory());
$client = new Psr16CacheClient($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 PSR-16 Example Start *", $logger);
// Set
$response = $client->set($KEY, $VALUE);
if (!$response) {
if ($error = $client->getLastError()) {
$logger->info("Set failed with error: {$error->getMessage()}\n");
exit;
}
$logger->info("Set failed without error\n");
}
$logger->info("Successfully set $KEY");
// Get
$response = $client->get($KEY);
if ($error = $client->getLastError()) {
$logger->info("Get failed with error: {$error->getMessage()}\n");
exit;
}
$logger->info("Successfully fetched key $KEY = $response\n");
printBanner("* Momento PSR-16 Example End *", $logger);