Skip to main content

A Redis Python Client

Project description

Redis Python Client

Another redis python client :) redis-cli-py provides friendly access to redis (on both normal python apps and kubernetes apps), separating initialization and keys operation with borg pattern.
You will have full features of official redis-py, for the principle of this client is focusing on init, the interface you actually work with IS class Redis itself from redis-py, without wrapping, which will compatible with multiple versions of redis-py, including these in the future.

Installation

use pip

$ pip install redis-cli

use git repository

# this is requirements.txt
# git+https://gitee.com/will4j/redis-cli-py.git@main#egg=redis-cli
git+https://github.com/will4j/redis-cli-py.git@main#egg=redis-cli 
$ pip install -r requirements.txt

Usage

Basic example

>>> import redis_cli
>>> redis_cli.init_from_url("redis://localhost:6379")
>>>
>>> from redis_cli import get_redis
>>> get_redis().set('foo', 'bar')
True
>>> get_redis().get('foo')
b'bar'

Initialization

TIPS: Both Redis and Sentinel actually use connectionpool internel, so do not bother with connectionpool.
NOTICE: You can init redis_cli multiple times, but only one shared Redis instance will exists.

from existing redis instance

import redis_cli
import redis

# from Redis instance
r = redis.Redis(host='localhost', port=6379, db=0)
redis_cli.init_from_redis(r)

# from Sentinel instance
s = redis.Sentinel([('localhost', 26379)], socket_timeout=0.1)
redis_cli.init_from_sentinel(s, 'mymaster')

from url

Scheme redis/rediss/unix will delegate to redis.from_url.
Scheme redis+sentinel will be parsed, return master Redis (which can both read & write) or slave Redis (which is readonly),according to url param readonly (default false).

import redis_cli

# from redis/rediss/unix url
redis_cli.init_from_url('redis://:password@localhost:6379/0')
redis_cli.init_from_url('rediss://localhost:6379/0')
redis_cli.init_from_url('unix://path/to/socket.sock?db=0')

# from sentinel url
redis_cli.init_from_url('redis-sentinel://username:password@host1:1,host2,host3:3/mymaster/0?readonly=true')

with env variables

This could be useful when deploy apps in kubernetes environment.
NOTICE: password from url has the highest priority, then from env REDISCLI_AUTH.

export REDISCLI_URL='redis-sentinel://host:26379/mymaster/0'
export REDISCLI_AUTH='complicated#pass'
import redis_cli
# above env REDISCLI_URL and REDISCLI_AUTH will take over
redis_cli.init_from_url('redis://:password@localhost:6379/0')

kubernetes example

Create a redis auth config secret:

apiVersion: v1
kind: Secret
metadata:
  name: redis-auth-conf-secret
type: Opaque
data:
  REDISCLI_URL: "redis-sentinel://host:26379/mymaster/0" # base64
  REDISCLI_AUTH: "complicated#pass" # base64

Mount environment variable in deployment config:

# ...
containers:
  - name: your container
    # ...
    envFrom:
      - secretRef:
          name: redis-auth-conf-secret
# ...

Then init redis at app startup:

import redis_cli
redis_cli.init_from_url('redis://:password@localhost:6379/0')

The redis url in your code could be dev url or whatever, the final redis auth config will be REDISCLI_URL and REDISCLI_AUTH in redis-auth-conf-secret.

Operation

get_redis() returns shared Redis instance Based on how you init redis_cli, could be normal Redis, master Redis or slave Redis of sentinel.

from redis_cli import get_redis

r = get_redis()
r.set('foo', 'bar')
r.get('foo')
r.delete('foo')

References

  1. https://github.com/redis/redis-py
  2. https://github.com/exponea/redis-sentinel-url/blob/master/redis_sentinel_url.py
  3. https://github.com/lettuce-io/lettuce-core/wiki/Redis-URI-and-connection-details
  4. https://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s23.html
  5. https://huangzhw.github.io/2019/03/23/how-redis-py-sentinel-work

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-cli-1.0.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

redis_cli-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file redis-cli-1.0.1.tar.gz.

File metadata

  • Download URL: redis-cli-1.0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for redis-cli-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6be46d8e6ae638fec27cb425d499b7f25b1592402c74d5d17f5b85b5e7f988a0
MD5 73d01babbeaa2fc87093268844cbb3a2
BLAKE2b-256 857a464077bce2a14cd8399776ae0c694e7c240b58ffe4b231413013f2fb48fe

See more details on using hashes here.

File details

Details for the file redis_cli-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: redis_cli-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for redis_cli-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e9f6af4a8b7591d8bfd1e23be89bdd9666ce824df881748fb02b88dd30575dc8
MD5 3b38f6abe24b90f015cf2f6992246a2b
BLAKE2b-256 7b2933cbdafefd22613cc003f837b6f0295462b2e91696180f06c5210f576a1b

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