Features
BITFIELD and BITFIELD_RO
One particular case is represented by these two chained commands, which are
available as functions that return an instance of the BITFIELD
and,
respectively, BITFIELD_RO
classes. Use the execute
function to run the
commands.
redis.bitfield("test_key") \
.incrby(encoding="i8", offset=100, increment=100) \
.overflow("SAT") \
.incrby(encoding="i8", offset=100, increment=100) \
.execute()
redis.bitfield_ro("test_key_2") \
.get(encoding="u8", offset=0) \
.get(encoding="u8", offset="#1") \
.execute()
Custom commands
If you want to run a command that hasn’t been implemented, you can use the
execute
function of your client instance and pass the command as a list
.
redis.execute(["XLEN", "test_stream"])
Encoding
Although Redis can store invalid JSON data, there might be problems with the deserialization. To avoid this, the Upstash REST proxy is capable of encoding the data as base64 on the server and then sending it to the client to be decoded.
For very large data, this can add a few milliseconds in latency. So, if you’re
sure that your data is valid JSON, you can set rest_encoding
to None
.
Retry mechanism
upstash-redis has a fallback mechanism in case of network or API issues. By
default, if a request fails it’ll retry once, 3 seconds after the error. If you
want to customize that, set rest_retries
and rest_retry_interval
(in
seconds).
Pipelines & Transactions
If you want to submit commands in batches to reduce the number of roundtrips, you can utilize pipelining or transactions. The difference between pipelines and transactions is that transactions are atomic: no other command is executed during that transaction. In pipelines there is no such guarantee.
To use a pipeline, simply call the pipeline
method:
pipeline = redis.pipeline()
pipeline.set("foo", 1)
pipeline.incr("foo")
pipeline.get("foo")
result = pipeline.exec()
print(result)
# prints [True, 2, '2']
For transaction, use mutli
:
pipeline = redis.multi()
pipeline.set("foo", 1)
pipeline.incr("foo")
pipeline.get("foo")
result = pipeline.exec()
print(result)
# prints [True, 2, '2']
You can also chain the commands:
pipeline = redis.pipeline()
pipeline.set("foo", 1).incr("foo").get("foo")
result = pipeline.exec()
print(result)
# prints [True, 2, '2']
Was this page helpful?