Package: upstash-redis
Upstash Redis Python SDKupstash-redis is a connectionless, HTTP-based Redis client for Python, designed to be used in serverless and serverful environments such as:AWS LambdaVercel ServerlessGoogle Cloud Functionsand other environments where HTTP is preferred over TCP.Inspired by other Redis clients like@upstash/redisandredis-py,
the goal of this SDK is to provide a simple way to use Redis over theUpstash REST API.The SDK is currently compatible with Python 3.8 and above.Upstash Redis Python SDKQuick StartInstallPyPIUsageBITFIELD and BITFIELD_ROCustom commandsEncodingRetry mechanismContributingPreparing the environmentRunning testsQuick StartInstallPyPIpipinstallupstash-redisUsageTo be able to use upstash-redis, you need to create a database onUpstashand grabUPSTASH_REDIS_REST_URLandUPSTASH_REDIS_REST_TOKENfrom the console.# for sync clientfromupstash_redisimportRedisredis=Redis(url="UPSTASH_REDIS_REST_URL",token="UPSTASH_REDIS_REST_TOKEN")# for async clientfromupstash_redis.asyncioimportRedisredis=Redis(url="UPSTASH_REDIS_REST_URL",token="UPSTASH_REDIS_REST_TOKEN")Or, if you want to automatically load the credentials from the environment:# for sync usefromupstash_redisimportRedisredis=Redis.from_env()# for async usefromupstash_redis.asyncioimportRedisredis=Redis.from_env()If you are in a serverless environment that allows it, it's recommended to initialise the client outside the request handler
to be reused while your function is still hot.Running commands might look like this:fromupstash_redisimportRedisredis=Redis.from_env()defmain():redis.set("a","b")print(redis.get("a"))# or for async context:fromupstash_redis.asyncioimportRedisredis=Redis.from_env()asyncdefmain():awaitredis.set("a","b")print(awaitredis.get("a"))BITFIELD and BITFIELD_ROOne particular case is represented by these two chained commands, which are available as functions that return an instance of
theBITFIELDand, respectively,BITFIELD_ROclasses. Use theexecutefunction 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 commandsIf you want to run a command that hasn't been implemented, you can use theexecutefunction of your client instance
and pass the command as alist.redis.execute(command=["XLEN","test_stream"])EncodingAlthough 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 setrest_encodingtoNone.Retry mechanismupstash-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, setrest_retriesandrest_retry_interval(in seconds).ContributingPreparing the environmentThis project usesPoetryfor packaging and dependency management. Make sure you are able to create the poetry shell with relevant dependencies.You will also need a database onUpstash.Running testsTo run all the tests, make sure the poetry virtual environment activated with all
the necessary dependencies. Set theUPSTASH_REDIS_REST_URLandUPSTASH_REDIS_REST_TOKENenvironment variables and run:poetryrunpytest