Redis + PHP – how to use.

Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries and more… Detailed information about Redis on official web site.

1. Installing Redis

The suggested way of installing Redis is compiling it from sources as Redis has no dependencies other than a working GCC compiler and libc. Follow this link for more information about installation process. As I use Gentoo I’ve installed it as easy as it can be, just: emerge dev-db/redis

2. Configuring.

Open /etc/redis.conf file in your favorite editor and check following configuration items:

daemonize yes

By default Redis does not run as a daemon. Use ‘yes’ if you need it. Note that Redis will write a pid file in /var/run/ when daemonized.

port 6379

Accept connections on the specified port, default is 6379. If port 0 is specified Redis will not listen on a TCP socket.


By default Redis listens for connections from all the network interfaces available on the server. It is possible to listen to just one or multiple interfaces using the “bind” configuration directive, followed by one or more IP addresses. I recommend to use if you are not planning remotely connect to Redis server.

dbfilename dump.rdb

The filename where to dump the DB

dir /var/lib/redis/

The working directory. The DB will be written inside this directory, with the filename specified above using the ‘dbfilename’ configuration directive.

At this point Redis is installed and configured, let’s see what we can do with Redis and PHP. Let’s write simple application called “URL Shortener”

3. Simple application.

Redis supports data structures such as strings, hashes, lists, sets, sorted sets with range and more…

In our simple application we will use hashes. Hashes in Redis are a useful tool to represent objects with many fields. They are set up to store vast amount of fields in a small amount of space. A hash can store more than 4 billion field-value pairs.

Some common Hash commands are:

  • HMSET: Sets up multiple hash values
  • HSET: Sets the hash field with a string value
  • HGET: Retrieves the value of a hash field
  • HMGET: Retrieves all of the values for given hash fields
  • HGETALL: Retrieves all of the values for in a hash
  • HLEN: Retrieves the number on elements in a hash

In our application we will use just two commands: HSET, HGET

First we need a PHP library, a list of most known library is described on official site.

I’ve used this one:

It’s very simple PHP library and good for beginners. Of cource you can use more powerfull library: in your projects.

Let’s look at class methods I’ve used:

  • $redis->cmd() – Set’s list of Redis command. You can call cmd() command many times and then call set() or get() to run it on the server.
  • $redis->set() – Send list of commands to the server and run it. Returns execution status.
  • $redis->get() – Send list of commands to the server and run it. Returns command results.

Let’s look at main application class:

The main function is add($link) – generate unique ID and add link into the database.

First, we need to generate unique link identifier

$id = $this->getUniqID();

and then add it into Redis database to “links” key:

$res = $this->redis->cmd(‘HSET’, ‘links’, $id, $link)->set();

HSET – Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten.
Return value: Integer, specifically:

  • 1 if field is a new field in the hash and value was set.
  • 0 if field already exists in the hash and the value was updated.

HSET links <unique ID>
returns: 1

Next function of links.php class is get($id) – get link by ID from the database.

$res = $this->redis->cmd(‘HGET’, ‘links’, $id)->get();

HGET – Returns the value associated with field in the hash stored at key.
Return value: Bulk string reply: the value associated with field, or nil(null) when field is not present in the hash or key does not exist.
HGET links <unique ID>

Another two functions: count() and delLinkByID() implemented but not used yet.

Full source of code can be retrieved from GitHub. Just clone and play with it.

Please share this article with your friends in social networks and anywhere else.