Make PHP Redis super easy with Redis Labs
Redis Labs' products and services enable running Redis datasets in a highly available and auto-scalable manner, with predictable top performance.
The Redis Labs Enterprise Cluster (RLEC) lets you install an enterprise grade Redis cluster in your environment of choice, whether an on-premises data-center or your preferred cloud platform. It gives you full control of your data and configuration - no clustering or sharding knowledge required!
Redis Cloud is a fully-managed cloud service for hosting and running Redis dataset without dealing with nodes, clusters, failure recovery or performance stabilization. Our technology does all that in a fully automated manner. Redis Cloud is available on all popular clouds and platforms-as-a-service.
Using Redis with PHP
In order to use Redis with PHP you will need a PHP Redis client. In following sections, we will demonstrate the use of Predis, a flexible and feature-complete Redis client library for PHP >= 5.3. Additional PHP clients for Redis can be found under the PHP section of the Redis Clients page.
Predis' installation instructions are given in the "How to Use Predis" section of its README file. The recommended method for installing Predis is to use Composer and install it from Packagist or the dedicated PEAR channel.
You can also download the latest Predis release from the GitHub repository.
Opening a Connection to Redis Using Predis
The following code creates a connection to Redis using Predis:
<?php require "predis/autoload.php"; Predis\Autoloader::register(); $redis = new Predis\Client(array( "scheme" => "tcp", "host" => "hostname", "port" => port, "password” => “password")); echo "Connected to Redis"; ?>
Unless you've installed Predis with Composer, you'll need to include the 2nd and 3rd lines of code to load and register the Predis client library. To adapt this example to your code, make sure that you replace the following values with those of your database:
- In line 8,
hostshould refer to your database's hostname or IP address
- In line 9,
portshould be your database's port
- In line 10,
passwordshould be your database's password
Persistent Connections with Predis
Predis supports the use ofing persistent connections, which are recommended practice to minimizeconnection management overhead. To enable persistent connections, use the
persistent connection attribute as shown in the following snippet:
$redis = new Predis\Client([ "scheme" => "tcp", "host" => "hostname", "port" => port, "password” => "password", "persistent" => "1"]);
Using SSL and Predis
As of v1.1.0, Predis provides native support for TLS/SSL connections. The above example can be modified to use TLS/SSL as follows:
$redis = new Predis\Client([ "scheme" => "tls", "ssl" => ["cafile" => "path_to_certfile", "verify_peer" => true], "host" => "hostname", "port" => port, "password” => “password"]);
Reading and Writing Data with Predis
Once connected to Redis, you can start reading and writing data. The following code snippet writes the value
bar to the Redis key
foo, reads it back, and prints it:
// open a connection to Redis ... $redis->set("foo", "bar"); $value = $redis->get("foo"); var_dump($value);
The output of the above code should be:
$ php predis_example.php Connected to Redis string(3) "bar"
For more information on using Redis Labs' products and services with PHP please see the Howto page.
Gartner Acknowledges Redis Labs as a Leader
In the 2015 Magic Quadrant for Operational Database Management Systems (ODBMS)49
Redis is an open-source, in-memory NoSQL database that was created in 2009 by Salvatore Sanfilippo - Redis Labs’ lead of open-source development. Redis is ranked the #1 NoSQL (and #2 database) in User Satisfaction and Market Presence by G2 Crowd, the top database technology on Docker by Datadog, the most popular NoSQL database in containers by DevOps.com and ClusterHQ, the #1 NoSQL among Top 10 Data Stores by Stackshare and both the fastest growing database since January 2013 and one of the top three NoSQL databases by DB-engines.
Redis Labs Enterprise Cluster (RLEC)
Create and manage scalable & highly-available redis databases on any infrastructure
Interact with Redis
Enter your Redis commands,
see how fast it responds!
Fully-managed Redis service
Highly available with auto-failover