Package: redis-sdk
Khulnasoft Redis Python SDKredis-sdk 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@khulnasoft/redisandredis-py,
the goal of this SDK is to provide a simple way to use Redis over theKhulnasoft REST API.The SDK is currently compatible with Python 3.8 and above.Khulnasoft Redis Python SDKQuick StartInstallPyPIUsageBITFIELD and BITFIELD_ROCustom commandsEncodingRetry mechanismContributingPreparing the environmentRunning testsQuick StartInstallPyPIpipinstallredis-sdkUsageTo be able to use redis-sdk, you need to create a database onKhulnasoftand grabREDIS_SDK_REST_URLandREDIS_SDK_REST_TOKENfrom the console.# for sync clientfromredis_sdkimportRedisredis=Redis(url="REDIS_SDK_REST_URL",token="REDIS_SDK_REST_TOKEN")# for async clientfromredis_sdk.asyncioimportRedisredis=Redis(url="REDIS_SDK_REST_URL",token="REDIS_SDK_REST_TOKEN")Or, if you want to automatically load the credentials from the environment:# for sync usefromredis_sdkimportRedisredis=Redis.from_env()# for async usefromredis_sdk.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:fromredis_sdkimportRedisredis=Redis.from_env()defmain():redis.set("a","b")print(redis.get("a"))# or for async context:fromredis_sdk.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 Khulnasoft 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 mechanismredis-sdk 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 onKhulnasoft.Running testsTo run all the tests, make sure the poetry virtual environment activated with all
the necessary dependencies. Set theREDIS_SDK_REST_URLandREDIS_SDK_REST_TOKENenvironment variables and run:poetryrunpytest