Skip to main content

Serverless Redis Sdk from Upstash

Project description

Upstash Redis - python edition

upstash-redis is a connectionless, HTTP-based Redis client for python, designed to be used in serverless and serverful environments such as:

  • AWS Lambda
  • Vercel Serverless
  • Google Cloud Functions
  • and other environments where HTTP is preferred over TCP.

Inspired by other Redis clients like @upstash/redis and redis-py, the goal of this SDK is to provide a simple way to use Redis in HTTP-based environments.

The SDK is currently compatible with python 3.8 and above.

Quick Start

Install

PyPi

pip install upstash-redis

Usage

To be able to use upstash-redis, you need to create a database on Upstash and grab UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN from the console.

# In this method, you can also give specific parameters for the Redis instance instance, such as rest_retries and so on

# for sync client
from upstash_redis import Redis
redis = Redis(url="UPSTASH_REDIS_REST_URL", token="UPSTASH_REDIS_REST_TOKEN")

# for async client
from upstash_redis.asyncio import Redis

redis = Redis(url="UPSTASH_REDIS_REST_URL", token="UPSTASH_REDIS_REST_TOKEN")

Or, if you want to automatically load the credentials from the environment:

# for sync use
from upstash_redis import Redis
redis = Redis.from_env()

# for async use
from upstash_redis.asyncio import Redis
redis = Redis.from_env()

If you are in a serverless environment that allows it, it's recommended to initialise the client outside the request handler to be reused while your function is still hot.

Running commands might look like this:

from upstash_redis import Redis
redis = Redis.from_env()

def main():
  redis.set("a", "b")
  print(redis.get("a"))

# or for async context:

from upstash_redis.asyncio import Redis
redis = Redis.from_env()

async def main():
  async with redis:
    await redis.set("a", "b")
    print(await redis.get("a"))

BITFIELD and BITFIELD_RO

One particular case is represented by these two chained commands, which are available as functions that return an instance of the BITFIELD and, respectively, BITFIELD_RO classes. Use the execute function to run the commands.

    redis.bitfield("test_key")
    .incrby(encoding="i8", offset=100, increment=100)
    .overflow("SAT")
    .incrby(encoding="i8", offset=100, increment=100)
    .execute()

    redis.bitfield_ro("test_key_2")
    .get(encoding="u8", offset=0)
    .get(encoding="u8", offset="#1")
    .execute()

Custom commands

If you want to run a command that hasn't been implemented, you can use the run function of your client instance and pass the command as list

redis.run(command=["XLEN", "test_stream"])

If you want to have more control over your session management or need to use multiple custom values, use the sync_execute function directly.

Encoding

Although Redis can store invalid JSON data, there might be problems with the deserialization. To avoid this, the Upstash REST proxy is capable of encoding the data as base64 on the server and then sending it to the client to be decoded.

For very large data, this can add a few milliseconds in latency. So, if you're sure that your data is valid JSON, you can set rest_encoding to False.

Retry mechanism

upstash-redis has a fallback mechanism in case of network or API issues. By default, if a request fails it'll retry once, 3 seconds after the error. If you want to customize that, set rest_retries and rest_retry_interval (in seconds).

Formatting returns

The SDK relies on the Upstash REST proxy, which returns the RESP2 responses of the given commands. By default, we apply formatting to some of them to provide a better developer experience. If you want the commands to output the raw return, set format_return to False.

You can also opt out of individual commands:

redis.format_return = False

print(redis.copy(source="source_string", destination="destination_string"))

redis.format_return = True

Contributing

Preparing the environment

This project uses Poetry for packaging and dependency management. Make sure you are able to create the poetry shell with relevant dependencies.

You will also need a database on Upstash.

Running tests

To run all the tests, make sure the poetry virtual environment activated with all the necessary dependencies. Set the UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN environment variables and run:

poetry run pytest

Project details


Download files

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

Source Distribution

upstash_redis-0.14.5.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

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

upstash_redis-0.14.5-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file upstash_redis-0.14.5.tar.gz.

File metadata

  • Download URL: upstash_redis-0.14.5.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.17 Linux/5.15.0-1040-azure

File hashes

Hashes for upstash_redis-0.14.5.tar.gz
Algorithm Hash digest
SHA256 f27de1ea239516e411b65778085009e0dc6ec7adb4a7268840021d1e87072e67
MD5 9c5a9ea1ec44ee2fb2ca7ea5284e792b
BLAKE2b-256 b86f960b202c9c0785d6894e7a049079f90d44a4d91d4290eeb0e05608b17101

See more details on using hashes here.

File details

Details for the file upstash_redis-0.14.5-py3-none-any.whl.

File metadata

  • Download URL: upstash_redis-0.14.5-py3-none-any.whl
  • Upload date:
  • Size: 29.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.8.17 Linux/5.15.0-1040-azure

File hashes

Hashes for upstash_redis-0.14.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7f6d30fe490f6d960d594cc4fc9220d6855e8f91c53d4a1b6f7c9092b0f4cfe8
MD5 4a15d9f29138767e85d2c24bb998b6da
BLAKE2b-256 b2d8cab83b7761f52f1202978c1291f80b8618f511c2acf263e3e9f231555e83

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