Valkey is an in-memory data store that offers a flexible data structure, high performance, and on-demand scalability. A Vultr Managed Database for Valkey offers high availability, high throughput, and low latency for mission-critical applications. This guide explains how to use a Vultr Managed Database for Valkey in Python to create highly available applications.
Before you begin:
To integrate a Vultr Managed Database for Valkey to your Python application, install the Python redis module and set up a sample project directory to store the application files.
Upgrade the Python pip package manager.
Install the Python redis driver to connect to the Valkey database.
Create a new project directory.
Navigate to the new project directory.
To use your Vultr Managed Database for Valkey connection in your Python application files, create a central Python module that connects to the database. You can then reuse the module by declaring the database module using the import clause in your application. Follow the steps below to create the Valkey connection module
Using a text editor such as Vim, create a new redis_gateway.py file
Add the following code to the file. Replace the vultr-prod-abcd.vultrdb.com, 16752, and example-password values with your correct Vultr Managed Database for Valkey details
Save and close the file
The above Python application code declares a RedisGateway class with an __init__(self) method. This method executes every time you create an instance of the RedisGateway class. Then, it connects to the Valkey database using the self.r_client = redis.Redis(...) function.
To import the Valkey database module to other Python source code files, use the following declarations:
In Valkey, a string is a sequence of bytes and it's the most used data type. You can use the string data type to store session IDs, user passwords, product information, and static HTML content. To create a key in a Valkey server, use the set keyword. To retrieve a key value, use the get keyword. In this section, create a Python application to implement Valkey strings as described below.
Create a new redis_strings.py file
Add the following contents to the file
Save and close the file.
The above application file imports the redis_gateway module you created earlier, then:
r_key: Defines the name of the string key you're setting in the Valkey databaser_value: Defines the Valkey key valuer_client.set(r_key, r_value): Sets the key in the Valkey databaseif r_client.exists(r_key): Verifies whether the key exists in the Valkey database server before retrieving it to avoid run time errorsr_client.get(r_key).decode("utf-8"): Retrieves the key from the Valkey databaseRun the Valkey strings application file
Output:
As displayed in the above output, Python correctly connects to the Valkey database and sets the value example-password for the string key john_doe.
A Valkey list is an ordered collection of strings used to implement queueing mechanisms. Valkey allows you to add elements to the head or tail of a list using the lpush and rpush commands. Create a new Python application to test the Valkey Lists functionality as described below.
Create a new redis_lists.py file
Add the following contents to the file
Save and close the file.
Below is what the above application code does:
r_list variable represents the key of your sample Valkey list.sample_job_1 = {...} and sample_job_1 = {...} are sample list values.r_client.lpush(r_list, r_list_value_1, r_list_value_2) function inserts the two sample jobs into the Valkey listwhile(r_client.llen(r_list) != 0): loop queries the Valkey server and iterates through the r_list variable to print the list valuesRun the redis_lists.py application file
Output:
Valkey hashes are record types that map keys to value pairs. Implement Valkey hashes in the form of company data in a Python application as described below.
Create a new redis_hash.py file
Add the following contents to the file
Save and close the file.
In the above application:
The r_hash variable represents the hash key in the Valkey database.
The r_client.hset(r_hash, "company_name", "XYZ COMPANY") function sets a hash in the Redis server. This function is the same as:
r_client.hgetall(r_hash): Retrieves all key-value pairs for a given hash
r_client.hget(r_hash, "company_name"): Retrieves the value of a given key in the hash
Run the application
Output:
Sorted sets represent a collection of strings arranged by an associated score. An element can only appear once in a sorted set. Valkey offers the zadd and zrange functions for adding and retrieving sorted sets values. Implement these functions in a Python application as described below.
Create a new redis_sorted_set.py file
Add the following contents to the file
Save and close the file.
In the above application:
r_sorted_set: Declares a key for the sorted setdatabase_scores = {...}: Contains four elements with a matching scorer_client.zadd(r_sorted_set, database_scores): Adds the set elements to the Valkey serverr_client.zrange(r_sorted_set, 0, 3): Returns all sorted set values from the databaseRun the application
Output:
In this guide, you have implemented different data types using a Vultr Managed Database for Valkey with Python. Based on your application use case, implement the relevant Valkey data type to cache and store data in your database.
0 Comments
Be the first to comment and share your perspective with the community.