ledis-py’s documentation!¶
API Reference¶
-
class
ledis.
Ledis
(host='localhost', port=6380, db=0, socket_timeout=None, connection_pool=None, charset='utf-8', errors='strict', decode_responses=False, unix_socket_path=None)[source]¶ Implementation of the Redis protocol.
This abstract class provides a Python interface to all LedisDB commands and an implementation of the Redis protocol.
Connection and Pipeline derive from this, implementing how the commands are sent and received to the Ledis server
-
bexpireat
(name, when)[source]¶ - Set an expire flag on key name for time seconds. time can be represented by
- an integer or a Python timedelta object.
-
bmsetbit
(name, *args)[source]¶ Set any number of offset, value pairs to the key
name
. Pairs can be specified in the following way:offset1, value1, offset2, value2, ...
-
bopt
(operation, dest, *keys)[source]¶ Perform a bitwise operation using
operation
betweenkeys
and store the result indest
.operation
is one of and, or, xor, not.
-
decr
(name, amount=1)[source]¶ Decrements the value of
key
byamount
. If no key exists, the value will be initialized as 0 -amount
-
decrby
(name, amount=1)[source]¶ Decrements the value of
key
byamount
. If no key exists, the value will be initialized as 0 -amount
-
expire
(name, time)[source]¶ Set an expire flag on key
name
fortime
seconds.time
can be represented by an integer or a Python timedelta object.
-
expireat
(name, when)[source]¶ Set an expire flag on key
name
.when
can be represented as an integer indicating unix time or a Python datetime object.
-
classmethod
from_url
(url, db=None, **kwargs)[source]¶ Return a Ledis client object configured from the given URL.
For example:
ledis://localhost:6380/0 unix:///path/to/socket.sock?db=0
There are several ways to specify a database number. The parse function will return the first specified option:
- A
db
querystring option, e.g. ledis://localhost?db=0 - If using the ledis:// scheme, the path argument of the url, e.g. ledis://localhost/0
- The
db
argument to this function.
If none of these options are specified, db=0 is used.
Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class’s initializer. In the case of conflicting arguments, querystring arguments always win.
- A
-
getset
(name, value)[source]¶ Set the value at key
name
tovalue
if key doesn’t exist Return the value at keyname
atomically
-
hexpire
(name, time)[source]¶ Set an expire flag on key name for time milliseconds. time can be represented by an integer or a Python timedelta object.
-
hexpireat
(name, when)[source]¶ Set an expire flag on key name. when can be represented as an integer representing unix time in milliseconds (unix time * 1000) or a Python datetime object.
-
hmset
(name, mapping)[source]¶ Sets each key in the
mapping
dict to its corresponding value in the hashname
-
hset
(name, key, value)[source]¶ Set
key
tovalue
within hashname
Returns 1 if HSET created a new field, otherwise 0
-
incr
(name, amount=1)[source]¶ Increments the value of
key
byamount
. If no key exists, the value will be initialized asamount
-
incrby
(name, amount=1)[source]¶ Increments the value of
key
byamount
. If no key exists, the value will be initialized asamount
-
lexpire
(name, time)[source]¶ Set an expire flag on key
name
fortime
seconds.time
can be represented by an integer or a Python timedelta object.
-
lexpireat
(name, when)[source]¶ Set an expire flag on key
name
.when
can be represented as an integer indicating unix time or a Python datetime object.
-
lindex
(name, index)[source]¶ Return the item from list
name
at positionindex
Negative indexes are supported and will return an item at the end of the list
-
lrange
(name, start, end)[source]¶ Return a slice of the list
name
between positionstart
andend
start
andend
can be negative numbers just like Python slicing notation
-
mset
(*args, **kwargs)[source]¶ Sets key/values based on a mapping. Mapping can be supplied as a single dictionary argument or as kwargs.
-
parse_response
(connection, command_name, **options)[source]¶ Parses a response from the Ledis server
-
sdiffstore
(dest, keys, *args)[source]¶ Store the difference of sets specified by
keys
into a new set nameddest
. Returns the number of keys in the new set.
-
sexpire
(name, time)[source]¶ Set an expire flag on key name for time milliseconds. time can be represented by an integer or a Python timedelta object.
-
sexpireat
(name, when)[source]¶ Set an expire flag on key name. when can be represented as an integer representing unix time in milliseconds (unix time * 1000) or a Python datetime object.
-
sinterstore
(dest, keys, *args)[source]¶ Store the intersection of sets specified by
keys
into a new set nameddest
. Returns the number of keys in the new set.
-
sunionstore
(dest, keys, *args)[source]¶ Store the union of sets specified by
keys
into a new set nameddest
. Returns the number of keys in the new set.
-
zadd
(name, *args, **kwargs)[source]¶ Set any number of score, element-name pairs to the key
name
. Pairs can be specified in two ways:As *args, in the form of: score1, name1, score2, name2, ... or as **kwargs, in the form of: name1=score1, name2=score2, ...
The following example would add four values to the ‘my-key’ key: ledis.zadd(‘my-key’, 1.1, ‘name1’, 2.2, ‘name2’, name3=3.3, name4=4.4)
-
zcount
(name, min, max)[source]¶ Return the number of elements in the sorted set at key
name
with a score betweenmin
andmax
. The min and max arguments have the same semantic as described for ZRANGEBYSCORE.
-
zexpireat
(name, when)[source]¶ - Set an expire flag on key name for time seconds. time can be represented by
- an integer or a Python timedelta object.
-
zrange
(name, start, end, desc=False, withscores=False)[source]¶ Return a range of values from sorted set
name
betweenstart
andend
sorted in ascending order.start
andend
can be negative, indicating the end of the range.desc
a boolean indicating whether to sort the results descendinglywithscores
indicates to return the scores along with the values. The return type is a list of (value, score) pairs
-
zrangebyscore
(name, min, max, start=None, num=None, withscores=False)[source]¶ Return a range of values from the sorted set
name
with scores betweenmin
andmax
.If
start
andnum
are specified, then return a slice of the range.withscores
indicates to return the scores along with the values. The return type is a list of (value, score) pairs
-
zremrangebyrank
(name, min, max)[source]¶ Remove all elements in the sorted set
name
with ranks betweenmin
andmax
. Values are 0-based, ordered from smallest score to largest. Values can be negative indicating the highest scores. Returns the number of elements removed
-
zremrangebyscore
(name, min, max)[source]¶ Remove all elements in the sorted set
name
with scores betweenmin
andmax
. Returns the number of elements removed.
-
zrevrange
(name, start, num, withscores=False)[source]¶ Return a range of values from sorted set
name
betweenstart
andnum
sorted in descending order.start
andnum
can be negative, indicating the end of the range.withscores
indicates to return the scores along with the values The return type is a list of (value, score) pairs
-
zrevrangebyscore
(name, min, max, start=None, num=None, withscores=False)[source]¶ Return a range of values from the sorted set
name
with scores betweenmin
andmax
in descending order.If
start
andnum
are specified, then return a slice of the range.withscores
indicates to return the scores along with the values. The return type is a list of (value, score) pairs
-
-
class
ledis.
ConnectionPool
(connection_class=<class 'ledis.connection.Connection'>, max_connections=None, **connection_kwargs)[source]¶ Generic connection pool
-
classmethod
from_url
(url, db=None, **kwargs)[source]¶ Return a connection pool configured from the given URL.
For example:
ledis://localhost:6380/0 unix:///path/to/socket.sock?db=0
- Three URL schemes are supported:
- ledis:// creates a normal TCP socket connection unix:// creates a Unix Domain Socket connection
There are several ways to specify a database number. The parse function will return the first specified option:
- A
db
querystring option, e.g. ledis://localhost?db=0 - If using the ledis:// scheme, the path argument of the url, e.g. ledis://localhost/0
- The
db
argument to this function.
If none of these options are specified, db=0 is used.
Any additional querystring arguments and keyword arguments will be passed along to the ConnectionPool class’s initializer. In the case of conflicting arguments, querystring arguments always win.
-
classmethod
-
class
ledis.
BlockingConnectionPool
(max_connections=50, timeout=20, connection_class=None, queue_class=None, **connection_kwargs)[source]¶ Thread-safe blocking connection pool:
>>> from ledis.client import Ledis >>> client = Ledis(connection_pool=BlockingConnectionPool())
It performs the same function as the default
:py:class: ~ledis.connection.ConnectionPool
implementation, in that, it maintains a pool of reusable connections that can be shared by multiple ledis clients (safely across threads if required).The difference is that, in the event that a client tries to get a connection from the pool when all of connections are in use, rather than raising a
:py:class: ~ledis.exceptions.ConnectionError
(as the default:py:class: ~ledis.connection.ConnectionPool
implementation does), it makes the client wait (“blocks”) for a specified number of seconds until a connection becomes available.Use
max_connections
to increase / decrease the pool size:>>> pool = BlockingConnectionPool(max_connections=10)
Use
timeout
to tell it either how many seconds to wait for a connection to become available, or to block forever:# Block forever. >>> pool = BlockingConnectionPool(timeout=None)
# Raise a
ConnectionError
after five seconds if a connection is # not available. >>> pool = BlockingConnectionPool(timeout=5)-
get_connection
(command_name, *keys, **options)[source]¶ Get a connection, blocking for
self.timeout
until a connection is available from the pool.If the connection returned is
None
then creates a new connection. Because we use a last-in first-out queue, the existing connections (having been returned to the pool after the initialNone
values were added) will be returned beforeNone
values. This means we only create new connections when we need to, i.e.: the actual number of connections will only increase in response to demand.
-