Package: redis-cli
Redis Python ClientAnother 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 officialredis-py, for the principle of this client is focusing on init, the interface you actually work withISclassRedisitself from redis-py, without wrapping, which will compatible with multiple versions of redis-py, including these in the future.Installationuse pip$pipinstallredis-cliuse 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$pipinstall-rrequirements.txtUsageBasic example>>>importredis_cli
>>>redis_cli.init_from_url("redis://localhost:6379")>>>
>>>fromredis_cliimportget_redis
>>>get_redis().set('foo','bar')True
>>>get_redis().get('foo')b'bar'InitializationTIPS: 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 instanceimportredis_cliimportredis# from Redis instancer=redis.Redis(host='localhost',port=6379,db=0)redis_cli.init_from_redis(r)# from Sentinel instances=redis.Sentinel([('localhost',26379)],socket_timeout=0.1)redis_cli.init_from_sentinel(s,'mymaster')from urlScheme 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 paramreadonly(default false).importredis_cli# from redis/rediss/unix urlredis_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 urlredis_cli.init_from_url('redis-sentinel://username:password@host1:1,host2,host3:3/mymaster/0?readonly=true')with env variablesThis could be useful when deploy apps in kubernetes environment.NOTICE:passwordfrom url has the highest priority, then from envREDISCLI_AUTH.exportREDISCLI_URL='redis-sentinel://host:26379/mymaster/0'exportREDISCLI_AUTH='complicated#pass'importredis_cli# above env REDISCLI_URL and REDISCLI_AUTH will take overredis_cli.init_from_url('redis://:password@localhost:6379/0')kubernetes exampleCreate a redis auth config secret:apiVersion:v1kind:Secretmetadata:name:redis-auth-conf-secrettype:Opaquedata:REDISCLI_URL:"redis-sentinel://host:26379/mymaster/0"# base64REDISCLI_AUTH:"complicated#pass"# base64Mount environment variable in deployment config:# ...containers:-name:your container# ...envFrom:-secretRef:name:redis-auth-conf-secret# ...Then init redis at app startup:importredis_cliredis_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 beREDISCLI_URLandREDISCLI_AUTHinredis-auth-conf-secret.Operationget_redis()returns shared Redis instance Based on how you init redis_cli, could be normal Redis, master Redis or slave Redis of sentinel.fromredis_cliimportget_redisr=get_redis()r.set('foo','bar')r.get('foo')r.delete('foo')Referenceshttps://github.com/redis/redis-pyhttps://github.com/exponea/redis-sentinel-url/blob/master/redis_sentinel_url.pyhttps://github.com/lettuce-io/lettuce-core/wiki/Redis-URI-and-connection-detailshttps://www.oreilly.com/library/view/python-cookbook/0596001673/ch05s23.htmlhttps://huangzhw.github.io/2019/03/23/how-redis-py-sentinel-work