A Python class for interacting with Redis using asyncio and aioredis.
Project description
AIORedisORM
A Python class for interacting with Redis using asyncio and aioredis.
Installation
You can install AIORedisORM using pip:
pip install aioredisorm
Example Usage
Here is an example that demonstrates the usage of the AIORedisORM class:
import asyncio
from aioredisorm import AIORedisORM
async def main():
# Add prefix to beging of each key
redis_client = AIORedisORM(key_prefix='my_prefix')
await redis_client.connect()
# Set a value
await redis_client.set_value('my_key', 'my_value', ex=12)
# Get a value
result = await redis_client.get_value('my_key')
print(result) # Output: b'my_value'
# Set a hash
await redis_client.set_hash('my_hash', {'key1': 'value1', 'key2': 'value2', 'key3': 13})
# Set a hash with expiration
await redis_client.set_hash('my_hash', {'key1': 'value1', 'key2': 'value2', 'key3': 13}, ex=5)
# Get a hash
hash_result = await redis_client.get_hash('my_hash')
print(hash_result) # Output: {b'key1': b'value1', b'key2': b'value2', b'key3': b'123'}
await asyncio.sleep(5) # Wait for the expiration to pass
hash_result = await redis_client.get_hash('my_hash')
print(hash_result) # Output: {}
# Decode the bytes to a string if needed
result = result.decode('utf-8')
print(result) # Output: 'my_value'
# Set a set
await redis_client.set_set('my_set', 'value1', 'value2', 'value3')
# Get a set
set_result = await redis_client.get_set('my_set')
print("set_result", set_result) # Output: {b'value1', b'value2', b'value3'}
# Transaction example
commands = [
('set', 'key1', 'value1'),
('set', 'key2', 'value2')
]
results = await redis_client.execute_transaction(commands)
print(results) # Output: [(True, True)]
# Set a list
await redis_client.set_list('my_list', 'value1', 'value2', 'value3')
# Get a list
list_result = await redis_client.get_list('my_list')
print(list_result) # Output: [b'value1', b'value2', b'value3']
# Get the expiration time of a key
ttl, pttl = await redis_client.get_key_expiration('key1')
print(f"TTL of 'my_key': {ttl} seconds")
print(f"PTTL of 'my_key': {pttl} milliseconds")
# Close the connection
await redis_client.close()
# Run the async example
asyncio.run(main())
Make sure to import the AIORedisORM class and replace 'my_prefix' with your desired key prefix.
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
aioredisorm-0.1.3.tar.gz
(3.8 kB
view details)
Built Distribution
File details
Details for the file aioredisorm-0.1.3.tar.gz
.
File metadata
- Download URL: aioredisorm-0.1.3.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.6 Linux/5.19.0-46-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 720dde55158995e737f58689f0f90cc8dbabe2bf7bc6c00555279623824e4005 |
|
MD5 | a6c9cac8fb84a157e08f5c9848fc589e |
|
BLAKE2b-256 | 95fa2bfcba5ff45b0ab2c81f875025532910331755fbd3af80d990d3f049bc55 |
Provenance
File details
Details for the file aioredisorm-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: aioredisorm-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.6 Linux/5.19.0-46-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5903e8ea68fbda813f0ab0ec9abd6028c22481989901f039375b3548e9a49837 |
|
MD5 | 5683a5030bd79a597d2dcea1258ee1ae |
|
BLAKE2b-256 | 8efba0c157a93b8c4bf42e79cf03ce80de4bbc0076494a1ae945a928ee6d5887 |