Vultr Managed Database for Valkey is a highly available in-memory data store with flexible data structures such as strings, hashes, lists, and sets. You can use the database with PHP to build highly available applications with fast response rates as compared to disk-based databases.
This guide explains how to use a Vultr Managed Database for Valkey with PHP to interact with different data types in your web application.
Before you begin:
PHP is a server-side object-oriented programming language. To run PHP files, prepare the development server with the necessary packages and configurations. To integrate with a Vultr Managed Database for Valkey, install the php-redis extension and set up the web server configurations to read PHP files on the server.
Verify the installed PHP version on your server
Your output should look like the one below:
Install the php-redis extension on your server
Enable the Apache PHP module on your server depending on your PHP version
Restart the Apache web server to save changes
Navigate to the Apache virtual host files directory
View your virtual host configuration file. By default, 000-default.conf
Keep note of the configured DocumentRoot value. Usually, /var/www/html
Allow the HTTP port 80 through your server firewall to access your PHP files
Using a text editor such as Nano, create a new info.php file in your web root directory
Add the following contents to the file to test your PHP and Valkey module configuration
Save and close the file
Visit your server IP address and load the info.php file
Verify that a PHP Valkey installed prompt displays on the web page
You have prepared your development server to read and serve PHP scripts. Integrate your Vultr Managed Database for Valkey to enhance your PHP application functions and sync with the database.
To organize your PHP code, create a Redis® PHP database class to declare your database information. Within your PHP applications, you can import the class to use your Valkey database connection. Create a new connection class as described in the steps below.
Navigate to your web root directory to store PHP files
Create a new RedisGateway.php file
Add the following contents to the file. Replace the example values with your actual Vultr Managed Database for Valkey details in the $host, $port, $user, and $pass variables.
Save and close the file.
Below is what the above PHP application methods and functions do:
__construct(): Initializes a connection to the Valkey server when you create an instance of the RedisGateway class using the PHP new keyword$redis = new Redis();: imports the Redis® PHP driver$redis->connect("tls://" . $host, $port);: Connects to the Vultr Managed Database for Valkey$redis->auth([$user, $pass]);: Defines the Valkey database connection details$this->redis_client = $redis;: Populates the $redis_client public property with the Valkey database connection detailsTo import the above PHP class, use the following declarations in your application
A string is a collection of bytes. You can use the Valkey string data type to store text, binary arrays, and serialized objects. When using Valkey as a cache, you can use the string data type to store values such as login tokens, user profile information, and frequently visited HTML pages. Implement Valkey Strings in your application as described below.
Create a new redis_strings.php file
Add the following contents to the file
Save and close the file.
Below is what the above PHP application functions do:
require_once('RedisGateway.php');: Imports the custom RedisGateway class to use the Valkey connection information$redis_client->set($redis_key, $key_value);: Sets a new key in the Valkey database$redis_key: Creates a new Valkey key with the value john_doe$key_value: Sets the Valkey key value to example-password$redis_client->expire($redis_key, 3600);: Sets the Valkey key expiration time in seconds. 3600 sets the expiration time to 1 hour$redis_client->get($redis_key);: Retrieves the Valkey key value from the databaseecho $redis_value: Prints the Valkey key value from the databaseUsing the curl utility, run the PHP application using your localhost IP Address
Output:
As displayed in the above output, the PHP application prints the Valkey key-value example-password from the database.
A hash is a collection of field-value pairs. Unlike strings, a Valkey hash allows you to store multiple field values under one key. For instance, you can use a hash to save user session data in a Valkey database in a single key. Implement Valkey hashes in your PHP application as described below
Create a new redis_hashes.php file
Add the following contents to the file
Save and close the file.
In the above application code:
$redis_hash_key: Defines the Valkey hash key$username: Defines the key for each element in the Valkey hash$password: Stores the value for each element in the key$redis_client->hset($redis_hash_key, $username, $password);: Creates and populates the Valkey hash with dataecho $redis_client->hget($redis_hash_key, $username);: Retrieves the value of a given key from the hashRun the redis_hashes.php application file
Output:
A sorted set is a collection of unique Valkey strings ordered by an associated score. For example, in a fitness PHP application, you can use the Valkey set data type to store and analyze walking distances for the application users. Implement Valkey sorted sets as described in the following PHP application.
Create a new redis_sorted_sets.php file
Add the following contents to the file
Save and close the file.
In the above application:
$redis_set_key: Defines the name of the sorted Valkey set$redis_client->zadd(...);: Adds a string and the associated score to the Valkey database$redis_client->zrange($redis_set_key, 0, 2, true): Retrieves the sorted sets values from the Valkey databaseRun the redis_sorted_sets.php application file
Output:
In Valkey, a list is an ordered sequence of strings for implementing stacks and queue-based applications. Valkey allows you to add values to the left or right side of a list using the lpush and rpush directives. lPop and rPop retrieve and remove elements from a list respectively. Implement Valkey lists as described in the following example PHP application
Create a new redis_lists.php file
Add the following contents to the file
Save and close the file.
In the above PHP application:
$list_key: Declares the Valkey key name$redis_client->lrange($list_key, 0, 2);: Retrieves all the list values from the Valkey server$redis_client->llen($list_key): Finds the length of a list or the total elements in the listwhile ($redis_client->llen($list_key) > 0) {...}: loops through the Valkey list and runs the lPop('test_list') function to display and remove elements from the listRun the redis_lists.php application file
Output:
In this article, you have integrated a Vultr Managed Database for Valkey with PHP and implemented several data types to work with the stored database values. By implementing Valkey in your PHP application, you can speed up your application processes as compared to relational databases.
0 Comments
Be the first to comment and share your perspective with the community.