Install
PyPI
pip install upstash-redis
Usage
To be able to use upstash-redis, you need to create a database on
Upstash and grab UPSTASH_REDIS_REST_URL
and
UPSTASH_REDIS_REST_TOKEN
from the console.
from upstash_redis import Redis
redis = Redis(url="UPSTASH_REDIS_REST_URL", token="UPSTASH_REDIS_REST_TOKEN")
from upstash_redis.asyncio import Redis
redis = Redis(url="UPSTASH_REDIS_REST_URL", token="UPSTASH_REDIS_REST_TOKEN")
Or, if you want to automatically load the credentials from the environment:
from upstash_redis import Redis
redis = Redis.from_env()
from upstash_redis.asyncio import Redis
redis = 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:
from upstash_redis import Redis
redis = Redis.from_env()
def main():
redis.set("a", "b")
print(redis.get("a"))
from upstash_redis.asyncio import Redis
redis = Redis.from_env()
async def main():
await redis.set("a", "b")
print(await redis.get("a"))