# 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](https://github.com/redis/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
```bash
$ pip install redis-cli
```
### use git repository
```text
# 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
```
```bash
$ pip install -r requirements.txt
```
## Usage
### Basic example
```bash
>>> 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
```python
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).
```python
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`.
```bash
export REDISCLI_URL='redis-sentinel://host:26379/mymaster/0'
export REDISCLI_AUTH='complicated#pass'
```
```python
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:
```yaml
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:
```yaml
# ...
containers:
- name: your container
# ...
envFrom:
- secretRef:
name: redis-auth-conf-secret
# ...
```
Then init redis at app startup:
```python
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.
```python
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
1. https://github.com/exponea/redis-sentinel-url/blob/master/redis_sentinel_url.py
1. https://github.com/lettuce-io/lettuce-core/wiki/Redis-URI-and-connection-details
1. https://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s23.html
1. https://huangzhw.github.io/2019/03/23/how-redis-py-sentinel-work
Raw data
{
"_id": null,
"home_page": "",
"name": "redis-cli",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "Redis,Python,client",
"author": "",
"author_email": "William Wang <williamw0825@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/85/7a/464077bce2a14cd8399776ae0c694e7c240b58ffe4b231413013f2fb48fe/redis-cli-1.0.1.tar.gz",
"platform": null,
"description": "# Redis Python Client\nAnother 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. \nYou will have full features of official [redis-py](https://github.com/redis/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.\n\n## Installation\n### use pip\n```bash\n$ pip install redis-cli\n```\n### use git repository\n```text\n# this is requirements.txt\n# git+https://gitee.com/will4j/redis-cli-py.git@main#egg=redis-cli\ngit+https://github.com/will4j/redis-cli-py.git@main#egg=redis-cli \n```\n```bash\n$ pip install -r requirements.txt\n```\n\n## Usage\n### Basic example\n```bash\n>>> import redis_cli\n>>> redis_cli.init_from_url(\"redis://localhost:6379\")\n>>>\n>>> from redis_cli import get_redis\n>>> get_redis().set('foo', 'bar')\nTrue\n>>> get_redis().get('foo')\nb'bar'\n```\n\n### Initialization\n**TIPS**: Both Redis and Sentinel actually use connectionpool internel, so do not bother with connectionpool. \n**NOTICE**: You can init redis_cli multiple times, but only one shared Redis instance will exists.\n#### from existing redis instance\n```python\nimport redis_cli\nimport redis\n\n# from Redis instance\nr = redis.Redis(host='localhost', port=6379, db=0)\nredis_cli.init_from_redis(r)\n\n# from Sentinel instance\ns = redis.Sentinel([('localhost', 26379)], socket_timeout=0.1)\nredis_cli.init_from_sentinel(s, 'mymaster')\n```\n#### from url\nScheme redis/rediss/unix will delegate to redis.from_url. \nScheme 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).\n```python\nimport redis_cli\n\n# from redis/rediss/unix url\nredis_cli.init_from_url('redis://:password@localhost:6379/0')\nredis_cli.init_from_url('rediss://localhost:6379/0')\nredis_cli.init_from_url('unix://path/to/socket.sock?db=0')\n\n# from sentinel url\nredis_cli.init_from_url('redis-sentinel://username:password@host1:1,host2,host3:3/mymaster/0?readonly=true')\n```\n\n#### with env variables\nThis could be useful when deploy apps in kubernetes environment. \n**NOTICE**: `password` from url has the highest priority, then from env `REDISCLI_AUTH`. \n```bash\nexport REDISCLI_URL='redis-sentinel://host:26379/mymaster/0'\nexport REDISCLI_AUTH='complicated#pass'\n```\n```python\nimport redis_cli\n# above env REDISCLI_URL and REDISCLI_AUTH will take over\nredis_cli.init_from_url('redis://:password@localhost:6379/0')\n```\n\n#### kubernetes example\nCreate a redis auth config secret:\n```yaml\napiVersion: v1\nkind: Secret\nmetadata:\n name: redis-auth-conf-secret\ntype: Opaque\ndata:\n REDISCLI_URL: \"redis-sentinel://host:26379/mymaster/0\" # base64\n REDISCLI_AUTH: \"complicated#pass\" # base64\n```\nMount environment variable in deployment config:\n```yaml\n# ...\ncontainers:\n - name: your container\n # ...\n envFrom:\n - secretRef:\n name: redis-auth-conf-secret\n# ...\n```\nThen init redis at app startup:\n```python\nimport redis_cli\nredis_cli.init_from_url('redis://:password@localhost:6379/0')\n```\nThe 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`.\n\n### Operation\n`get_redis()` returns shared Redis instance Based on how you init redis_cli, could be normal Redis, master Redis or slave Redis of sentinel.\n\n```python\nfrom redis_cli import get_redis\n\nr = get_redis()\nr.set('foo', 'bar')\nr.get('foo')\nr.delete('foo')\n```\n\n## References\n1. https://github.com/redis/redis-py\n1. https://github.com/exponea/redis-sentinel-url/blob/master/redis_sentinel_url.py\n1. https://github.com/lettuce-io/lettuce-core/wiki/Redis-URI-and-connection-details\n1. https://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s23.html\n1. https://huangzhw.github.io/2019/03/23/how-redis-py-sentinel-work\n",
"bugtrack_url": null,
"license": "",
"summary": "A Redis Python Client",
"version": "1.0.1",
"project_urls": {
"Bug Tracker": "https://github.com/will4j/redis-cli-py/issues",
"Homepage": "https://github.com/will4j/redis-cli-py"
},
"split_keywords": [
"redis",
"python",
"client"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7b2933cbdafefd22613cc003f837b6f0295462b2e91696180f06c5210f576a1b",
"md5": "3b38f6abe24b90f015cf2f6992246a2b",
"sha256": "e9f6af4a8b7591d8bfd1e23be89bdd9666ce824df881748fb02b88dd30575dc8"
},
"downloads": -1,
"filename": "redis_cli-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3b38f6abe24b90f015cf2f6992246a2b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 5851,
"upload_time": "2023-10-02T05:58:32",
"upload_time_iso_8601": "2023-10-02T05:58:32.924732Z",
"url": "https://files.pythonhosted.org/packages/7b/29/33cbdafefd22613cc003f837b6f0295462b2e91696180f06c5210f576a1b/redis_cli-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "857a464077bce2a14cd8399776ae0c694e7c240b58ffe4b231413013f2fb48fe",
"md5": "73d01babbeaa2fc87093268844cbb3a2",
"sha256": "6be46d8e6ae638fec27cb425d499b7f25b1592402c74d5d17f5b85b5e7f988a0"
},
"downloads": -1,
"filename": "redis-cli-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "73d01babbeaa2fc87093268844cbb3a2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 5958,
"upload_time": "2023-10-02T05:58:36",
"upload_time_iso_8601": "2023-10-02T05:58:36.407756Z",
"url": "https://files.pythonhosted.org/packages/85/7a/464077bce2a14cd8399776ae0c694e7c240b58ffe4b231413013f2fb48fe/redis-cli-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-02 05:58:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "will4j",
"github_project": "redis-cli-py",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "coverage",
"specs": []
},
{
"name": "pytest",
"specs": []
},
{
"name": "redis",
"specs": []
}
],
"lcname": "redis-cli"
}