Skip to main content

Khulnasoft Redis Python SDK

redis-sdk 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 @khulnasoft/redis and redis-py, the goal of this SDK is to provide a simple way to use Redis over the Khulnasoft REST API.

The SDK is currently compatible with Python 3.8 and above.

Quick Start

Install

PyPI

pip install redis-sdk

Usage

To be able to use redis-sdk, you need to create a database on Khulnasoft and grab REDIS_SDK_REST_URL and REDIS_SDK_REST_TOKEN from the console.

# for sync client
from redis_sdk import Redis

redis = Redis(url="REDIS_SDK_REST_URL", token="REDIS_SDK_REST_TOKEN")

# for async client
from redis_sdk.asyncio import Redis

redis = Redis(url="REDIS_SDK_REST_URL", token="REDIS_SDK_REST_TOKEN")

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

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

# for async use
from redis_sdk.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 redis_sdk import Redis

redis = Redis.from_env()

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

# or for async context:

from redis_sdk.asyncio import Redis

redis = Redis.from_env()

async def main():  
  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 execute function of your client instance and pass the command as a list.

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

Encoding

Although Redis can store invalid JSON data, there might be problems with the deserialization. To avoid this, the Khulnasoft 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 None.

Retry mechanism

redis-sdk 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).

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 Khulnasoft.

Running tests

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

poetry run pytest

Metadata

Release files for redis-sdk 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for redis-sdk 1.0.0
File Size Uploaded
redis_sdk-1.0.0.tar.gz 29.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for redis-sdk 1.0.0
File Interpreter ABI Platform
redis_sdk-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 61.5 kB

Release files / redis_sdk-1.0.0.tar.gz

Download URL redis_sdk-1.0.0.tar.gz
Size 29.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b668087136bf053b799b26968eeb7ba4e1bb58a0438dac2a7be7ef96435ce50e
BLAKE2b-256 checksum
How to use checksums
be37a5e7161cb4f0211139d7f7b3fecdd3f6b13c621ec91668c0e4b55608776c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.6 Darwin/21.6.0

Release files / redis_sdk-1.0.0-py3-none-any.whl

Download URL redis_sdk-1.0.0-py3-none-any.whl
Size 31.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fefb4c54a9362f678a41d071d4bda5e2e9b7a337f9155f9ec782110a4bd8ac4b
BLAKE2b-256 checksum
How to use checksums
808a48a46a0c3851bdee7fb03c4d7fb4dfc434e896acd1e29f2a6b61b0291942
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.11.6 Darwin/21.6.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page