Skip to main content

a3redis is a simple wrapper around redis-py to make it easier to use.

Project description

a3redis

English | 简体中文

a3redis is a simple wrapper around redis-py to make it easier to use.

1. Introduction

  • When logically compatible, the Redis service type (standalone, sentinel, or cluster) can be switched by only modifying the configuration without changing the code.
  • You can configure multiple Redis services of different types at the same time; if no Redis service configuration is specified, redis://localhost:6379/0 will be used by default.
  • Provides class forms of commonly used data structures for ease of use.

2. Usage

Install

pip install a3redis

Examples

CONF = {
    "standalone": {
        "mode": "standalone",
        "init": {
            "host": "standalone-redis-master",
            "port": 6379,
            "db": 0,
            "password": "standalone",
            "decode_responses": True
        }
    },
    "sentinel": {
        "mode": "sentinel",
        "init": {
            "sentinels": [
                ["sentinel-redis-node-0.sentinel-redis-headless", 26379],
                ["sentinel-redis-node-1.sentinel-redis-headless", 26379],
                ["sentinel-redis-node-2.sentinel-redis-headless", 26379]
            ],
            "sentinel_kwargs": {
                "password": "sentinel"
            },
            "password": "sentinel",
            "db": 1,
            "decode_responses": True
        },
        "runtime": {
            "service_name": "my-master"
        }
    },
    "cluster": {
        "mode": "cluster",
        "init": {
            "host": "cluster-redis-cluster",
            "port": 6379,
            "password": "cluster",
            "decode_responses": True
        }
    }
}


from a3redis.bases import RedisClientFactory, RedisMode
from a3redis.structures import Hash


class User(Hash):
    main_key = 'user:{id}'
    _hkey_username = 'username'

    def get_username(self) -> str:
        return self.get_member_by_key(self._hkey_username)

    def set_username(self, username: str):
        self.set_member(self._hkey_username, username)


if __name__ == '__main__':
    RedisClientFactory.init_redis_clients(conf=CONF)

    localhost_anonymous_rdb = None
    standalone_rdb = RedisClientFactory.get_rdb(RedisMode.Standalone)
    sentinel_rdb = RedisClientFactory.get_rdb(RedisMode.Sentinel)
    cluster_rdb = RedisClientFactory.get_rdb(RedisMode.Cluster)
    
    for rdb in [localhost_anonymous_rdb, standalone_rdb, sentinel_rdb, cluster_rdb]:
        user = User(id="123", rdb=rdb)
        user.set_username("tom")
        user.get_username()
        user.delete()

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

a3redis-0.3.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

a3redis-0.3.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file a3redis-0.3.0.tar.gz.

File metadata

  • Download URL: a3redis-0.3.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for a3redis-0.3.0.tar.gz
Algorithm Hash digest
SHA256 76c03529139838cc0fac6bb6b5b448494376baf576c87d7d84aa31ffd7c85893
MD5 0e13fd2fcf556a6ffd5a96d3a4c0b905
BLAKE2b-256 ef082965675f817d9e0727233d042d24ed5d10dbfcf7e14f1ca781c595c3a9d8

See more details on using hashes here.

File details

Details for the file a3redis-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: a3redis-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for a3redis-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae8ee15ffff1076d9fe79b6d073e7f82a36e3feeb05c7d1b373fd354c1f58cff
MD5 d091b92e57382d6061c4a159943e5421
BLAKE2b-256 d87c4bfbefeb509104a3e88cfa4d81356a3cd7ffb894be83e639bbdda8349039

See more details on using hashes here.

Supported by

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