Skip to main content

SDK for Momento

Project description

logo

project status project stability

Momento Python Client Library

Python 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!

Getting Started :running:

Requirements

  • Python 3.7 or above is required
  • A Momento Auth Token is required, you can generate one using the Momento CLI

Examples

Ready to dive right in? Just check out the examples directory for complete, working examples of how to use the SDK.

Installation

The Momento SDK is available on PyPi. To install via pip:

pip install momento

Usage

The examples below require an environment variable named MOMENTO_AUTH_TOKEN which must be set to a valid Momento authentication token.

Python 3.10 introduced the match statement, which allows for structural pattern matching on objects. If you are running python 3.10 or greater, here is a quickstart you can use in your own project:

from datetime import timedelta

from momento import CacheClient, Configurations, CredentialProvider
from momento.responses import CacheGet, CacheSet, CreateCache

if __name__ == "__main__":
    cache_name = "default-cache"
    with CacheClient(
        configuration=Configurations.Laptop.v1(),
        credential_provider=CredentialProvider.from_environment_variable("MOMENTO_AUTH_TOKEN"),
        default_ttl=timedelta(seconds=60),
    ) as cache_client:
        create_cache_response = cache_client.create_cache(cache_name)
        match create_cache_response:
            case CreateCache.CacheAlreadyExists():
                print(f"Cache with name: {cache_name} already exists.")
            case CreateCache.Error() as error:
                raise error.inner_exception

        print("Setting Key: foo to Value: FOO")
        set_response = cache_client.set(cache_name, "foo", "FOO")
        match set_response:
            case CacheSet.Error() as error:
                raise error.inner_exception

        print("Getting Key: foo")
        get_response = cache_client.get(cache_name, "foo")
        match get_response:
            case CacheGet.Hit() as hit:
                print(f"Look up resulted in a hit: {hit}")
                print(f"Looked up Value: {hit.value_string!r}")
            case CacheGet.Miss():
                print("Look up resulted in a: miss. This is unexpected.")
            case CacheGet.Error() as error:
                raise error.inner_exception

The above code uses structural pattern matching, a feature introduced in Python 3.10. Using a Python version less than 3.10? No problem. Here is the same example compatible across all versions of Python:

from datetime import timedelta
from momento import CacheClient, Configurations, CredentialProvider
from momento.responses import CacheGet, CacheSet, CreateCache

if __name__ == "__main__":
    cache_name = 'default-cache'
    with CacheClient(configuration=Configurations.Laptop.v1(),
                     credential_provider=CredentialProvider.from_environment_variable('MOMENTO_AUTH_TOKEN'),
                     default_ttl=timedelta(seconds=60)
                     ) as cache_client:
        create_cache_response = cache_client.create_cache(cache_name)
        if isinstance(create_cache_response, CreateCache.CacheAlreadyExists):
            print(f"Cache with name: {cache_name} already exists.")
        elif isinstance(create_cache_response, CreateCache.Error):
            raise create_cache_response.inner_exception

        print("Setting Key: foo to Value: FOO")
        set_response = cache_client.set(cache_name, 'foo', 'FOO')
        if isinstance(set_response, CacheSet.Error):
            raise set_response.inner_exception

        print("Getting Key: foo")
        get_response = cache_client.get(cache_name, 'foo')
        if isinstance(get_response, CacheGet.Hit):
            print(f"Look up resulted in a hit: {get_response.value_string}")
            print(f"Looked up Value: {get_response.value_string}")
        elif isinstance(get_response, CacheGet.Miss):
            print("Look up resulted in a: miss. This is unexpected.")
        elif isinstance(get_response, CacheGet.Error):
            raise get_response.inner_exception

Logging

To avoid cluttering DEBUG logging with per-method logs the Momento SDK adds a TRACE logging level. This will only happen if the TRACE level does not already exist.

To enable TRACE level logging you can call logging.basicConfig() before making any log statements:

import logging

logging.basicConfig(level='TRACE')

Error Handling

Coming Soon!

Tuning

Coming Soon!


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

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

momento-1.3.1.tar.gz (61.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

momento-1.3.1-py3-none-any.whl (98.2 kB view details)

Uploaded Python 3

File details

Details for the file momento-1.3.1.tar.gz.

File metadata

  • Download URL: momento-1.3.1.tar.gz
  • Upload date:
  • Size: 61.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.11 Linux/5.15.0-1037-azure

File hashes

Hashes for momento-1.3.1.tar.gz
Algorithm Hash digest
SHA256 6f917228b069a3624ed43fc80cdb3ccd8508e9b27224810c1b3ee49ea7ec6e1a
MD5 3e3baeeec46caf797b8a41087acfa4b9
BLAKE2b-256 08917e2bf41a36375e4bb30094cc4e4b80dedc425e9183d150418ed865f7cb35

See more details on using hashes here.

File details

Details for the file momento-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: momento-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 98.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.11 Linux/5.15.0-1037-azure

File hashes

Hashes for momento-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad0dc35dd8b28ab5060ed5ea8688702868c1b84650b019118dc0dff4f2a1f1ef
MD5 d079770f2274699c105c997cb4a77491
BLAKE2b-256 0579a117e19b1b13a2289e1e1d0a69d79cbadf66141111aa56f5c7e4b4d4d845

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page