Table Of Contentredis-py Documentation
Release 2.7.2
Andy McCurdy, Mahdi Yusuf
September10,2015
Contents
1 Redis 3
2 StrictRedis 5
3 Connections 19
4 Utils 23
5 Exceptions 25
6 Indicesandtables 27
i
ii
redis-pyDocumentation,Release2.7.2
Contents:
Contents 1
redis-pyDocumentation,Release2.7.2
2 Contents
1
CHAPTER
Redis
classredis.Redis(host=’localhost’, port=6379, db=0, password=None,
socket_timeout=None, socket_connect_timeout=None, socket_keepalive=None,
socket_keepalive_options=None, connection_pool=None, unix_socket_path=None,
encoding=’utf-8’, encoding_errors=’strict’, charset=None, errors=None, de-
code_responses=False, retry_on_timeout=False, ssl=False, ssl_keyfile=None,
ssl_certfile=None,ssl_cert_reqs=None,ssl_ca_certs=None)
Provides backwards compatibility with older versions of redis-py that changed arguments to some commands
tobemorePythonic,sane,orbyaccident.
lrem(name,value,num=0)
Removethefirstnumoccurrencesofelementsequaltovaluefromtheliststoredatname.
Thenumargumentinfluencestheoperationinthefollowingways: num>0: Removeelementsequal
to value moving from head to tail. num < 0: Remove elements equal to value moving from tail to
head. num=0: Removeallelementsequaltovalue.
pipeline(transaction=True,shard_hint=None)
Return a new pipeline object that can queue multiple commands for later execution. transaction
indicateswhetherallcommandsshouldbeexecutedatomically. Apartfrommakingagroupofoperations
atomic,pipelinesareusefulforreducingtheback-and-forthoverheadbetweentheclientandserver.
setex(name,value,time)
Setthevalueofkeynametovaluethatexpiresintimeseconds.timecanberepresentedbyaninteger
oraPythontimedeltaobject.
zadd(name,*args,**kwargs)
NOTE:TheorderofargumentsdiffersfromthatoftheofficialZADDcommand. Forbackwardscompata-
bility,thismethodacceptsargumentsintheformofname1,score1,name2,score2,whiletheofficialRedis
documentsexpectsscore1,name1,score2,name2.
Ifyou’relookingtousethestandardsyntax,considerusingtheStrictRedisclass. SeetheAPIReference
sectionofthedocsformoreinformation.
Setanynumberofelement-name,scorepairstothekeyname. Pairscanbespecifiedintwoways:
As*args,intheformof: name1,score1,name2,score2,... oras**kwargs,intheformof: name1=score1,
name2=score2,...
The following example would add four values to the ‘my-key’ key: redis.zadd(‘my-key’, ‘name1’, 1.1,
‘name2’,2.2,name3=3.3,name4=4.4)
3
redis-pyDocumentation,Release2.7.2
4 Chapter1. Redis
2
CHAPTER
StrictRedis
classredis.StrictRedis(host=’localhost’, port=6379, db=0, password=None, socket_timeout=None,
socket_connect_timeout=None, socket_keepalive=None,
socket_keepalive_options=None, connection_pool=None,
unix_socket_path=None, encoding=’utf-8’, encoding_errors=’strict’,
charset=None, errors=None, decode_responses=False,
retry_on_timeout=False, ssl=False, ssl_keyfile=None, ssl_certfile=None,
ssl_cert_reqs=None,ssl_ca_certs=None)
ImplementationoftheRedisprotocol.
This abstract class provides a Python interface to all Redis commands and an implementation of the Redis
protocol.
ConnectionandPipelinederivefromthis,implementinghowthecommandsaresentandreceivedtotheRedis
server
append(key,value)
Appends the string value to the value at key. If key doesn’t already exist, create it with a value of
value. Returnsthenewlengthofthevalueatkey.
bgrewriteaof()
TelltheRedisservertorewritetheAOFfilefromdatainmemory.
bgsave()
Tell the Redis server to save its data to disk. Unlike save(), this method is asynchronous and returns
immediately.
bitcount(key,start=None,end=None)
Returns the count of set bits in the value of key. Optional start and end paramaters indicate which
bytestoconsider
bitop(operation,dest,*keys)
Performabitwiseoperationusingoperationbetweenkeysandstoretheresultindest.
bitpos(key,bit,start=None,end=None)
Returnthepositionofthefirstbitsetto1or0inastring. startandenddifinessearchrange. Therange
is interpreted as a range of bytes and not a range of bits, so start=0 and end=2 means to look at the first
threebytes.
blpop(keys,timeout=0)
LPOPavalueoffofthefirstnon-emptylistnamedinthekeyslist.
IfnoneofthelistsinkeyshasavaluetoLPOP,thenblockfortimeoutseconds,oruntilavaluegets
pushedontooneofthelists.
Iftimeoutis0,thenblockindefinitely.
5
redis-pyDocumentation,Release2.7.2
brpop(keys,timeout=0)
RPOPavalueoffofthefirstnon-emptylistnamedinthekeyslist.
IfnoneofthelistsinkeyshasavaluetoLPOP,thenblockfortimeoutseconds,oruntilavaluegets
pushedontooneofthelists.
Iftimeoutis0,thenblockindefinitely.
brpoplpush(src,dst,timeout=0)
Popavalueoffthetailofsrc,pushitontheheadofdstandthenreturnit.
This command blocks until a value is in src or until timeout seconds elapse, whichever is first. A
timeoutvalueof0blocksforever.
client_getname()
Returnsthecurrentconnectionname
client_kill(address)
Disconnectstheclientataddress(ip:port)
client_list()
Returnsalistofcurrentlyconnectedclients
client_setname(name)
Setsthecurrentconnectionname
config_get(pattern=’*’)
Returnadictionaryofconfigurationbasedonthepattern
config_resetstat()
Resetruntimestatistics
config_rewrite()
Rewriteconfigfilewiththeminimalchangetoreflectrunningconfig
config_set(name,value)
Setconfigitemnamewithvalue
dbsize()
Returnsthenumberofkeysinthecurrentdatabase
debug_object(key)
Returnsversionspecificmetainformationaboutagivenkey
decr(name,amount=1)
Decrementsthevalueofkeybyamount. Ifnokeyexists,thevaluewillbeinitializedas0-amount
delete(*names)
Deleteoneormorekeysspecifiedbynames
dump(name)
Returnaserializedversionofthevaluestoredatthespecifiedkey. Ifkeydoesnotexistanilbulkreplyis
returned.
echo(value)
Echothestringbackfromtheserver
eval(script,numkeys,*keys_and_args)
ExecutetheLuascript,specifyingthenumkeysthescriptwilltouchandthekeynamesandargument
valuesinkeys_and_args. Returnstheresultofthescript.
In practice, use the object returned by register_script. This function exists purely for Redis API
completion.
6 Chapter2. StrictRedis
Description:redis-py Documentation, Release 2.7.2 brpop(keys, timeout=0). RPOP a value off of the first non-empty list named in the keys list. If none of the lists in