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.


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

SERVICE_URIURI for the Aiven for Caching service connection


To install the predis library, run the following command:

composer require predis/predis


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


require 'vendor/autoload.php';

$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