Redis Management
A utility package for managing Redis keys in Python. This package provides an easy-to-use interface for interacting with Redis, including setting, getting, and managing expiration of keys.
Features
- Manage public keys in Redis
- Set and get values (including JSON and boolean)
- Create and validate OTP codes
- Set and get expiration times for keys
- Check if a key exists and delete it
Requirements
- Python 3.6+
- Redis server
- redis
- Optional: python-decouple for configuration
Installation
You can install this package directly from GitHub:
pip install git+https://github.com/AAbbasRR/redis-manager.git
Alternatively, clone the repository and install manually:
git clone https://github.com/AAbbasRR/redis-manager.git
cd my_redis_utils
pip install .
Configuration
By default, the package uses localhost and the default Redis port (6379). You can configure the Redis connection
using python-decouple or by providing the parameters directly.
Configuration Keys
REDIS_HOST: The hostname or IP address of the Redis server. Default islocalhost.REDIS_PORT: The port number on which the Redis server is listening. Default is6379.REDIS_DB: The Redis database number to use. Default is0.FERNET_KEY: Before using the application, it's essential to set up theFERNET_KEY. This key is used for encrypting and decrypting sensitive data.
Example Configuration using python-decouple
-
Install
python-decouple:pip install python-decouple
-
Create a
.envfile in your project root and add your Redis configuration:REDIS_HOST=localhost REDIS_PORT=6379 REDIS_DB=0 FERNET_KEY=YourGeneratedKeyHere
-
Use the configuration in your code:
from redis_management.redis_manager import RedisManager redis_manager = RedisManager(identifier="user123", key="session_token")
Generating the FERNET_KEY
To generate a FERNET_KEY, you can use the following command in your terminal:
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
Usage
Example Code
from redis_management.redis_manager import RedisManager
Initialize RedisManager with identifier and key
redis_manager = RedisManager(identifier="user123", key="session_token")
Set a value
redis_manager.set_value("some_value")
Get a value
value = redis_manager.get_value()
print(value)
Set a JSON value
redis_manager.set_json_value({"key": "value"})
Get a JSON value
json_value = redis_manager.get_json_value()
print(json_value)
Create and set OTP key
otp_code = redis_manager.create_and_set_otp_key()
print(otp_code)
Validate OTP code
is_valid = redis_manager.validate(otp_code)
print(is_valid)
Check if key exists
exists = redis_manager.exists()
print(exists)
Delete the key
redis_manager.delete()
Exception Handling
The package provides custom exceptions for better error handling:
RedisException: Base exception for Redis errors.KeyNotFoundException: Raised when a key is not found in Redis.InvalidKeyException: Raised when an invalid key is provided.
Example:
from redis_management.redis_manager import RedisManager
from redis_management.exceptions import KeyNotFoundException
try:
redis_manager = RedisManager(identifier="user123", key="non_existent_key")
value = redis_manager.get_value()
except KeyNotFoundException:
print("Key not found in Redis.")
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
Author
Abbas Rahimzadeh - arahimzadeh79@gmail.com
Acknowledgments
Special thanks to the open-source community for their valuable contributions and resources.
Release files for redis-management 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| redis_management-1.1.0.tar.gz | 6.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| redis_management-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.7 kB
Release files / redis_management-1.1.0.tar.gz
| Download URL | redis_management-1.1.0.tar.gz |
|---|---|
| Size | 6.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc034bf5fdfab59aa0517875fd347339f03661fb2674fd75a9d55aa5201fdc02
|
|
BLAKE2b-256 checksum How to use checksums |
25a2220110e36d3720b92d3756e91173a6fe9d600513cbbc4b1a7686cbd3cd41
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / redis_management-1.1.0-py3-none-any.whl
| Download URL | redis_management-1.1.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
54a35a2254d44076759fd70c0769ead22f4525e017f856af391eab0069e1c267
|
|
BLAKE2b-256 checksum How to use checksums |
2f0afe254fac4be39d304d080b6993507d2eea82986127b629345d925b2313a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|