Skip to main content

Connect with PHP

Learn how to connect to an Aiven for Caching service using PHP and the predis library, complete with code examples and setup instructions.

Variables

Replace the following placeholders in the code sample with actual values from your service overview page:

VariableDescription
SERVICE_URIURI for the Aiven for Caching service connection

Prerequisites

To install the predis library, run the following command:

composer require predis/predis

Code

Create a file named index.php and insert the code below, substituting the placeholder with your Aiven for Caching URI:

<?php

require 'vendor/autoload.php';
Predis\Autoloader::register();

$redis_uri = 'REDIS_URI';

$client = new Predis\Client($redis_uri);

$client->set('key', 'hello world');
$value = $client->get('key');

echo "The value of key is: {$value}";

This code creates a key named key with the value hello world without an expiration. It then retrieves this key from the caching service and outputs its value.

Execute the script with:

php index.php

Successful execution results in the following output:

The value of key is: hello world