googlejupyterhub

Test Redis with a PHP client

You can test your Redis installation with a client like Predis, which is written in PHP. Find a complete list of Redis clients here.

NOTE: To use the example script below, you must have PHP installed on the server. You can check this by executing php -v. If you don’t have it available, you can easily install PHP on Debian with the command sudo apt-get install php5.

Begin by extracting the contents of the downloaded archive and creating a simple script named example.php.

$ cd predis-1.0
$ nano example.php

The script begins by including the class autoloader file and instantiating an object of the class:

require 'autoload.php';
$client = new Predis\Client(array(
  'host' => '127.0.0.1',
  'port' => 6379,
  'password' => 'PASSWORD'
));

Notice that it configures the client object by defining the Redis server host, port and password. Replace these values with actual values for your server.

You can now use the object’s set() and get() methods to add or remove values from the cache. In this example, the set() method stores the value ‘cowabunga’ using the key ‘foo’. The key can then be used with the get() method to retrieve the original value whenever needed.

$client->set('foo', 'cowabunga');
$response = $client->get('foo');

Here’s the complete code for the example.php script:

<?php
require 'autoload.php';
$client = new Predis\Client(array(
  'host' => '127.0.0.1',
  'port' => 6379,
  'password' => 'PASSWORD'
));
$client->set('foo', 'cowabunga');
$response = $client->get('foo');
echo $response;
?>

Save the file and run it.

$ php example.php

The script will connect to your Redis server, save the value to the key ‘foo’, then retrieve and display it.

Last modification December 21, 2022