Skip to content

Async Redis Client is a simple and easy-to-use module to interact with Redis databases

License

Notifications You must be signed in to change notification settings

deepmancer/aredis-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧰 Async Redis Client

Redis PyPI Python License

aredis-client is your go-to Python package for seamless asynchronous Redis interactions, powered by redis-py. With its singleton-based connection pooling, it ensures efficient, thread-safe operations, making your Redis experience faster and easier.


Source Code Website
github.com/deepmancer/aredis-client deepmancer.github.io/aredis-client

✨ Features

  • 💼 Full Compatibility: Works effortlessly with the redis-py library.
  • Asynchronous Operations: Async Redis connections for improved performance.
  • 🛠️ Singleton Pattern: Efficiently manage Redis connections using a singleton design.
  • 🔄 Context Manager Support: Easily manage Redis sessions.
  • 🔧 Simple Configuration: Configure effortlessly with RedisConfig.

📦 Installation

Get started quickly by installing aredis-client with pip:

pip install git+https://github.com/deepmancer/aredis-client.git

📝 Usage Guide

🔧 Configuration

Start by creating a configuration object with RedisConfig:

from aredis_client import RedisConfig

config = RedisConfig(
    host='localhost',
    port=6379,
    db=0,
)

🏗️ Creating an AsyncRedis Instance

Next, create an instance of AsyncRedis using the configuration:

from aredis_client import AsyncRedis

async def main():
    redis_client = await AsyncRedis.create(config=config)
    print(redis_client.url)

⚙️ Managing Redis Sessions

Interact with the Redis server using the context manager from get_or_create_session:

from aredis_client import AsyncRedis

async def main():
    redis_client = await AsyncRedis.create(config=config)

    async with redis_client.get_or_create_session() as session:
        # Interact with your Redis server
        await session.set('key', 'value')
        value = await session.get('key')
        print(value)

    await redis_client.disconnect()

🔍 Example Usage

Here's a complete example to demonstrate the power of aredis-client:

import asyncio
from aredis_client import AsyncRedis, RedisConfig

async def main():
    config = RedisConfig(
        host='localhost',
        port=6379,
        db=0,
    )
    client = await AsyncRedis.create(config=config)

    async with client.get_or_create_session() as session:
        await session.set('key', 'value')
        value = await session.get('key')
        print(f'The value for "key" is {value}')
        
        keys = ['key1', 'key2', 'key3']
        pipeline = session.pipeline()
        for key in keys:
            pipeline.delete(key)
        await pipeline.execute()
        
    await client.disconnect()

if __name__ == "__main__":
    asyncio.run(main())

🛡️ Error Handling

Stay safe with custom exceptions to handle Redis-related errors:

  • RedisConnectionError
  • RedisSessionCreationError

🛑 Disconnecting

Ensure a clean disconnect from the Redis server:

await redis_client.disconnect()

📄 License

This project is licensed under the Apache License 2.0. See the LICENSE file for full details.


Get started with aredis-client today and take your Redis operations much easier! 🚀

Releases

No releases published

Packages

No packages published

Languages