Momento wrapper for redis/redis-py
Project description
Momento Python Redis compatibility client
What and why?
This project provides a Momento-backed implementation of redis/redis-py The goal is to provide a drop-in replacement for redis/redis-py so that you can use the same code with either a Redis server or with the Momento Cache service!
Usage
To switch your existing redis/redis-py
application to use Momento, you only need to change the code where you construct
your client object:
With redis-py client
# Import the redis module
from redis import Redis
# Replace these values with your Redis server's details
_REDIS_HOST = 'my.redis-server.com';
_REDIS_PORT = 6379;
_REDIS_DB = 0
_REDIS_PASSWORD = 'mypasswd';
# Create a Redis client
redis_client = Redis(host=_REDIS_HOST, port=_REDIS_PORT, db=_REDIS_DB, password=_REDIS_PASSWORD)
With Momento's Redis compatibility client
import datetime
# Import the Momento redis compatibility client.
import momento
from momento_redis import MomentoRedis
_CACHE_NAME = "my-cache"
# Initialize Momento client.
redis_client = MomentoRedis(
momento.CacheClient(
momento.Configurations.Laptop.latest(),
momento.CredentialProvider.from_environment_variable("MOMENTO_AUTH_TOKEN"),
datetime.timedelta(seconds=60)
),
_CACHE_NAME
)
NOTE: The Momento redis/redis-py
implementation currently supports simple key/value pairs (GET
, SET
, DELETE
)
as well as INCR/INCRBY
and DECR/DECRBY
. We will continue to add support for additional Redis APIs in the future;
for more information see the current Redis API support section later in this doc.
Installation
The Momento Python Redis compatibility client is available on PyPi. You can install it via:
poetry add momento-redis
Examples
Prerequisites
To run these examples, you will need a Momento auth token. You can generate one using the Momento Console.
The examples will utilize the auth token via an environment variable MOMENTO_AUTH_TOKEN
that you set.
Basic example
In the examples/
directory, you will find a simple CLI app, basic.py
, that does some basic sets and
gets on strings. It uses the Momento Redis client by default, but you can also pass a '-r' flag on the command line
to use a Redis client instead to verify that the two clients are functioning identically. You may also pass a
'-h ' flag and/or a '-p ' flag to specify a specific host and port for the Redis client. By
default, localhost
and 6379
are used.
Here's an example run against Momento Cache:
cd examples/
export MOMENTO_AUTH_TOKEN=<your momento auth token goes here>
python basic.py
And the output should look like this:
Issuing a 'get' for 'key1', which we have not yet set.
result: None
Issuing a 'set' for 'key1', with value 'value1'.
result: True
Issuing another 'get' for 'key1'.
result: b'bar'
done
Running the script using Redis (python basic.py -r
) should produce identical output.
Current Redis API Support
This library supports the most popular Redis APIs, but does not yet support all Redis APIs. We currently support the most common APIs related to string values (GET, SET, DELETE, INCR, DECR). We will be adding support for additional APIs in the future. If there is a particular API that you need support for, please drop by our Discord or e-mail us at support@momentohq.com and let us know!
Type Checking
To allow the use of tools such as mypy
and in-IDE type checking to tell you if you're using any APIs that we
don't support yet, we provide our own MomentoRedisBase
abstract base class which explicitly lists out
the APIs we currently support. Simply use the class as a type annotation for your client:
from momento_redis import MomentoRedis, MomentoRedisBase
redis_client: MomentoRedisBase = MomentoRedis(...)
Once the client is typed using the abstract base class, static analysis tools will allow you to find calls to as yet unsupported APIs.
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
Built Distribution
File details
Details for the file momento_redis-0.1.2.tar.gz
.
File metadata
- Download URL: momento_redis-0.1.2.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.10.12 Linux/5.15.0-1042-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f61892e389bb40b4093b4839d81872df120aa17cc34c92d76b1dc73a92609d5b |
|
MD5 | 0a9716ee530af07a650675319d6f5877 |
|
BLAKE2b-256 | d60be4e049f783bba7926b5fe896cdb3bb78bf8d504deaa5b7ccf5121ee8841e |
File details
Details for the file momento_redis-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: momento_redis-0.1.2-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.10.12 Linux/5.15.0-1042-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 384256319fb0db10f8ba48d91d728107d1745535c81f81ce9ee86fe723ab2fd7 |
|
MD5 | 4a2fccd86954627e07c6fdd8bacd5c4b |
|
BLAKE2b-256 | e70f7084b6a81f6b3e294226a5618bd5e1ddafa5811be19f7f2e16e38b81c3e8 |