Tutorials

Getting Started with Redis in PHP

Introduction

Redis created by Salvatore Sanfilippo is an open-source, in-memory data structure server with advanced key-value cache and store, often referred to as a NoSQL database. It is also referred to as a data structure server since it can store strings, hashes, lists, sets, sorted sets, and more.

The essence of a key-value store is the ability to store some data, called a value inside a key. This data can later be retrieved only if we know the exact key used to store it.

Salvatore Sanfilippo (creator of Redis) said Redis can be used to replace an RDBMS database. Now, although nothing is impossible, I think it would be a bad idea because using a key-value store for things, like a full-text search, might be painful. Especially, when you consider ACID compliance and syncing data in a key-value store: painful.

Below are just a few uses of Redis, though there are many more than this.

  • Caching can be used in the same manner as memcached.
  • Leaderboards or related problems.
  • Counting stuff.
  • Real-time analysis.
  • Deletion and filtering.
  • Show latest item listings on your home page.

This article’s aim is not to show you the syntax of Redis (you can learn about Redis’s syntax here), in this article, we will learn how to use Redis in PHP.

Redis is pretty easy to install and the instructions, included, are for both Windows and Linux users.

To install Redis on Linux, is pretty simple, but you’ll need TCL installed if you don’t have TCL installed. You can simply run:

  1. sudo apt-get install tcl

To install Redis:

  1. wget http://download.redis.io/releases/redis-2.8.19.tar.gz
  2. tar xzf redis-2.8.19.tar.gz
  3. cd redis-2.8.19
  4. make

Note: 2.8.19 should be replaced with the latest stable version of Redis.

All Redis binaries are saved in the src Folder. To start the server:

  1. src/redis-server

Redis installation on Windows is very easy, just visit this link, download a package, and install.

Install Predis a Redis Client for PHP

Predis is a Redis Client for PHP. It is well written and has a lot of support from the community. To use Predis just clone the repository into your working directory:

  1. git clone git://github.com/nrk/predis.git

First, we’ll require the Redis Autoloader and register it. Then we’ll wrap the client in a try-catch block. The connection setting for connecting to Redis on a local server is different from connecting to a remote server.

getMessage()); }

Now that we have successfully connected to the Redis server, let’s start using Redis.

Redis supports a range of datatypes and you might wonder what a NoSQL key-value store has to do with datatypes? Well, these datatypes help developers store data in a meaningful way and can make data retrieval faster. Here are some of the datatypes supported by Redis:

  • String: Similar to Strings in PHP.
  • List: Similar to a single dimensional array in PHP. You can push, pop, shift and unshift, the elements that are placed in order or insertion FIFO (first in, first out).
  • Hash: Maps between string fields and string values. They are the perfect data type to represent objects (e.g.: A User with a number of fields like name, surname, and so forth).
  • Set: Similar to list, except that it has no order and each element may appear only once.
  • Sorted Set: Similar to Redis Sets with a unique feature of values stored in set. The difference is that each member of a Sorted Set is associated with score, used to order the set from the smallest score to the largest.

Others are bitmaps and hyperloglogs, but they will not be discussed in this article, as they are pretty dense.

In Redis, the most important commands are SET, GET and EXISTS. These commands are used to store, check, and retrieve data from a Redis server. Just like the commands, the Predis class can be used to perform Redis operations by methods with the same name as commands. For example:

set(‘;message’;, ‘;Hello world’;); $value = $redis->get(‘message’); print($value); echo ($redis->exists(‘message’)) ? “Oui” : “please populate the message key”;

INCR and DECR are commands used to either decrease or increase a value.

set(“counter”, 0); $redis->incr(“counter”); $redis->incr(“counter”); $redis->decr(“counter”);

We can also increase the values of the counter key by larger integer values or we can decrease the value of the counter key with the INCRBY and DECRBY commands.

set(“counter”, 0); $redis->incrby(“counter”, 15); $redis->incrby(“counter”, 5); $redis->decrby(“counter”, 10);

There are a few basic Redis commands for working with lists and they are:

  • LPUSH: adds an element to the beginning of a list
  • RPUSH: add an element to the end of a list
  • LPOP: removes the first element from a list and returns it
  • RPOP: removes the last element from a list and returns it
  • LLEN: gets the length of a list
  • LRANGE: gets a range of elements from a list

Simple List Usage:

rpush(“languages”, “french”); $redis->rpush(“languages”, “arabic”); $redis->lpush(“languages”, “english”); $redis->lpush(“languages”, “swedish”); $redis->lpop(“languages”); $redis->rpop(“languages”); $redis->llen(“languages”); $redis->lrange(“languages”, 0, -1); $redis->lrange(“languages”, 0, 1);

A hash in Redis is a map between one string field and string values, like a one-to-many relationship. The commands associated with hashes in Redis are:

  • HSET: sets a key-value on the hash
  • HGET: gets a key-value on the hash
  • HGETALL: gets all key-values from the hash
  • HMSET: mass assigns several key-values to a hash
  • HDEL: deletes a key from the object
  • HINCRBY: increments a key-value from the hash with a given value.

hset($key, ‘;age’;, 44); $redis->hset($key, ‘;country’;, ‘;finland’;); $redis->hset($key, ‘occupation’, ‘software engineer’); $redis->hset($key, ‘reknown’, ‘linux kernel’); $redis->hset($key, ‘to delete’, ‘i will be deleted’); $redis->get($key, ‘age’); $redis->get($key, ‘country’)); $redis->del($key, ‘to delete’); $redis->hincrby($key, ‘age’, 20); $redis->hmset($key, [ ‘age’ => 44, ‘country’ => ‘finland’, ‘occupation’ => ‘software engineer’, ‘reknown’ => ‘linux kernel’, ]); $data = $redis->hgetall($key); print_r($data);

The list of commands associated with sets includes:

  • SADD: adds a N number of values to the key
  • SREM: removes N number of values from a key
  • SISMEMBER: if a value exists
  • SMEMBERS: lists of values in the set.

sadd($key, ‘;china’;); $redis->sadd($key, [‘england’, ‘france’, ‘germany’]); $redis->sadd($key, ‘china’); $redis->srem($key, [‘england’, ‘china’]); $redis->sismember($key, ‘england’); $redis->smembers($key);

Since Redis is an in-memory data store, you would probably not store data forever. Therefore, this brings us to EXPIRE, EXPIREAT, TTL, PERSIST:

  • EXPIRE: sets an expiration time, in seconds, for the key after which it is deleted
  • EXPIREAT: sets an expiration time using UNIX timestamps for the key after which it is deleted
  • TTL: gets the remaining time left for a key expiration
  • PERSIST: makes a key last forever by removing the expiration timer from the key.

$key = “expire in 1 hour”; $redis->expire($key, 3600); $redis->expireat($key, time() + 3600); sleep(600); $redis->ttl($key); $redis->persist($key);

The commands listed in this article are just a handful of many existing Redis commands (see more redis commands).

Future of Redis

Redis is a better replacement for memcached, as it is faster, scales better (supports master-slave replication), supports datatypes that many (Facebook, Twitter, Instagram) have dropped memcached for Redis. Redis is open source and many brilliant programmers from the open-source community have contributed patches.

Other sources

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button