Skip to main content

A utility package for managing Redis keys.

Project description

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

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 is localhost.
  • REDIS_PORT: The port number on which the Redis server is listening. Default is 6379.
  • REDIS_DB: The Redis database number to use. Default is 0.
  • FERNET_KEY: Before using the application, it's essential to set up the FERNET_KEY. This key is used for encrypting and decrypting sensitive data.

Example Configuration using python-decouple

  1. Install python-decouple:

    pip install python-decouple
    
  2. Create a .env file in your project root and add your Redis configuration:

    REDIS_HOST=localhost
    REDIS_PORT=6379
    REDIS_DB=0
    FERNET_KEY=YourGeneratedKeyHere
    
  3. Use the configuration in your code:

    from my_redis_utils.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

python from my_redis_utils.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 my_redis_utils.redis_manager import RedisManager
from my_redis_utils.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.

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

redis_management-1.0.2.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

redis_management-1.0.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file redis_management-1.0.2.tar.gz.

File metadata

  • Download URL: redis_management-1.0.2.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for redis_management-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e20f3ceac6566158a6ea5c11c9babf8ec0535a2b2dcf0a7ceaf3e0bbdc4b084f
MD5 562cda19341ad5f19b44c05fc1834540
BLAKE2b-256 e9d9b2a76932859b099936228b96dd89d46f7a6c33de14ec2847198fdb27635c

See more details on using hashes here.

File details

Details for the file redis_management-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for redis_management-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b80eba03cf6f9470ea18a64a0b6267ad5a50abc439dad65dc65422a548ca41f4
MD5 c6e3969029638edb1156e215087c9b6b
BLAKE2b-256 aeb4a6e4eafa578366d79d0ed23425a2d56d0af4c810e56403622d7660914f83

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page