Skip to content

kvcache/client-sdk-php

 
 

Repository files navigation

logo

project status project stability

Momento PHP Client Library

⚠️ Alpha SDK ⚠️

This is an official Momento SDK, but the API is in an alpha stage and may be subject to backward-incompatible changes. For more info, click on the alpha badge above.

PHP client SDK for Momento Serverless Cache: a fast, simple, pay-as-you-go caching solution without any of the operational overhead required by traditional caching solutions!

Japanese: 日本語

Getting Started 🏃

Requirements

  • A Momento Auth Token is required, you can generate one using the Momento CLI
  • At least PHP 7
  • The grpc PHP extension. See the gRPC docs section on installing the extension.

IDE Notes: You'll most likely want to use an IDE that supports PHP development, such as PhpStorm or Microsoft Visual Studio Code.

Examples

Check out full working code in the examples directory of this repository!

Installation

Install composer as described on the composer website.

Add our repository to your composer.json file and our SDK as a dependency:

{
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/momentohq/client-sdk-php"
    }
  ],
  "require": {
    "momentohq/client-sdk-php": "dev-main"
  }
}

Run composer update to install the necessary prerequisites.

Usage

Check out full working code in the examples directory of this repository!

Here is an example to get you started:

<?php

require "vendor/autoload.php";

use Momento\Cache\SimpleCacheClient;

$MOMENTO_AUTH_TOKEN = getenv("MOMENTO_AUTH_TOKEN");
$CACHE_NAME = "cache";
$ITEM_DEFAULT_TTL_SECONDS = 60;
$KEY = "MyKey";
$VALUE = "MyValue";

function printBanner(string $message) : void {
    $line = "******************************************************************";
    print "$line\n$message\n$line\n";
}

function createCache(SimpleCacheClient $client, string $cacheName) : void {
    try {
        $client->createCache($cacheName);
    } catch (\Momento\Cache\Errors\AlreadyExistsError $e) {}
}

function listCaches(SimpleCacheClient $client) : void {
    $result = $client->listCaches();
    while (true) {
        foreach ($result->caches() as $cache) {
            print "- {$cache->name()}\n";
        }
        $nextToken = $result->nextToken();
        if (!$nextToken) {
            break;
        }
        $result = $client->listCaches($nextToken);
    }
}

printBanner("*                      Momento Example Start                     *");
$client = new SimpleCacheClient($MOMENTO_AUTH_TOKEN, $ITEM_DEFAULT_TTL_SECONDS);
createCache($client, $CACHE_NAME);
listCaches($client);
print "Setting key $KEY to value $VALUE\n";
$client->set($CACHE_NAME, $KEY, $VALUE);
$response = $client->get($CACHE_NAME, $KEY);
print "Look up status is: {$response->status()}\n";
print "Look up value is: {$response->value()}\n";
printBanner("*                       Momento Example End                      *");

Error Handling

Coming soon!

Tuning

Coming soon!


For more info, visit our website at https://gomomento.com!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%