Skip to main content

Coverage Status Lint & Test Release to PyPI

Asynchronous Redis Cache

This package offers several data types to ease working with a Redis cache in an asynchronous workflow. The package is currently in development and it's not recommended to start using it in production at this point.

Installation

Prerequisites

To use async-rediscache, make sure that redis is installed and running on your system. Alternatively, you could use fakeredis as a back-end for testing purposes and local development.

Install using pip

To install async-rediscache run the following command:

pip install async-rediscache

Alternatively, to install async-rediscache with fakeredis run:

pip install async-rediscache[fakeredis]

Basic use

Creating a RedisSession

To use a RedisCache, you first have to create a RedisSession instance that manages the connection pool to Redis. You can create the RedisSession at any point but make sure to call the connect method from an asynchronous context (see this explanation for why).

import async_rediscache

async def main():
    session = async_rediscache.RedisSession()
    await session.connect()

    # Do something interesting

    await session.close()

Creating a RedisSession with a network connection

async def main():
    connection = {"address": "redis://127.0.0.1:6379"}
    async_rediscache.RedisSession(**connection)

RedisCache

A RedisCache is the most basic data type provided by async-rediscache. It works like a dictionary in that you can associate keys with values. To prevent key collisions, each RedisCache instance should use a unique namespace identifier that will be prepended to the key when storing the pair to Redis.

Creating a RedisCache instance

When creating a RedisCache instance, it's important to make sure that it has a unique namespace. This can be done directly by passing a namespace keyword argument to the constructor:

import async_rediscache

birthday_cache = async_rediscache.RedisCache(namespace="birthday")

Alternatively, if you assign a class attribute to a RedisCache instance, a namespace will be automatically generated using the name of the owner class and the name of attribute assigned to the cache:

import async_rediscache

class Channel:
    topics = async_rediscache.RedisCache()  # The namespace be set to `"Channel.topics"`

Note: There is nothing preventing you from reusing the same namespace, although you should be aware this could lead to key collisions (i.e., one cache could interfere with the values another cache has stored).

Using a RedisCache instance

Using a RedisCache is straightforward: Just call and await the methods you want to use and it should just work. There's no need to pass a RedisSession around as the session is fetched internally by the RedisCache. Obviously, one restriction is that you have to make sure that the RedisSession is still open and connected when trying to use a RedisCache.

Here are some usage examples:

import async_rediscache

async def main():
    session = async_rediscache.RedisSession()
    await session.connect()

    cache = async_rediscache.RedisCache(namespace="python")

    # Simple key/value manipulation
    await cache.set("Guido", "van Rossum")
    print(await cache.get("Guido"))  # Would print `van Rossum`

    # A contains check works as well
    print(await cache.contains("Guido"))  # True
    print(await cache.contains("Kyle"))  # False

    # You can iterate over all key, value pairs as well:
    item_view = await cache.items()
    for key, value in item_view:
        print(key, value)

    # Other options:
    number_of_pairs = await cache.length()
    pairs_in_dict = await cache.to_dict()
    popped_item = await cache.pop("Raymond", "Default value")
    await cache.update({"Brett": 10, "Barry": False})
    await cache.delete("Barry")
    await cache.increment("Brett", 1)  # Increment Brett's int by 1
    await cache.clear()

    await session.close()

RedisQueue

A RedisQueue implements the same interface as a queue.SimpleQueue object, except that all the methods are coroutines. Creating an instance works the same as with a RedisCache.

Release files for async-rediscache 0.2.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 async-rediscache 0.2.0
File Size Uploaded
async-rediscache-0.2.0.tar.gz 17.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for async-rediscache 0.2.0
File Interpreter ABI Platform
async_rediscache-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.7 kB

Release files / async-rediscache-0.2.0.tar.gz

Download URL async-rediscache-0.2.0.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c1fd95fe530211b999748ebff96e2e9b629f2664957f9b36916b898e42fc57c4
BLAKE2b-256 checksum
How to use checksums
2fdce54bb3e4aa4c02d19952c0db4b9d59fb41f8000ffc069deed887f725aad3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

Release files / async_rediscache-0.2.0-py3-none-any.whl

Download URL async_rediscache-0.2.0-py3-none-any.whl
Size 18.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
710676211b407399c9ad94afa66fa04c22a936be11ba6f227e6c74cfa140ce78
BLAKE2b-256 checksum
How to use checksums
fd884390a44e59f5d77abc49cb002d4e964f6c527ee91bcd945f0d07f66d6436
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6
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