Redis_Db Class

The Redis_Db class allows you to interact with a Redis key-value store.

Configuration

For every Redis database connection there must be a config in redis array in app/config/<environment>/db.php. Configuration options and explanation are listed below. Every config requires to contain a hostname and a port.

Param Type Default Description
hostname string
'127.0.0.1'
Hostname
port integer
6379
Port to use in the connection
timeout mixed
null
Connection timeout
database integer
0
Database zero-based index
password mixed
null
Optional password if the database requires authentication

forge($name = 'default', $config = array())

Create a new named redis instance, optionally with a custom configuration

Static Yes
Parameters
Param Default Description
$name
'default'
The name of the new redis instance, as defined the application config db.php file.
$config
array()
custom configuration, which will override the values defined in the db.php file.
Returns Fuel\Core\Redis Object
Example
// create the Redis 'mystore' instance
$redis = Redis_Db::forge('mystore');

instance($name = 'default')

Return a named instance that was created earlier.

Static Yes
Parameters
Param Default Description
$name
'default'
The name of the redis instance requested, as created using forge().
Returns Fuel\Core\Redis Object
Example
// retrieve the Redis 'mystore' instance
$redis = Redis_Db::instance('mystore');

Redis commands

Once you have created a redis object using the instance() method, every valid redis command (see the Redis Documentation) as a method of that object.

Some examples:

// create the Redis 'mystore' instance
$redis = Redis_Db::forge('mystore');

// create some test data
$redis->rpush('particles', 'proton');
$redis->rpush('particles', 'electron');
$redis->rpush('particles', 'neutron');

// fetch the range
$particles = $redis->lrange('particles', 0, -1);

// count the number of elements
$particle_count = $redis->llen('particles');

// display the results
echo "<p>The {$particle_count} particles that make up atoms are:</p>";
echo "<ul>";
foreach ($particles as $particle) {
  echo "<li>{$particle}</li>";
}
echo "</ul>";

Pipelining

You can add multiple Redis commands into a command pipeline, and execute them all at once.

An example:

// create the Redis 'mystore' instance
$redis = Redis_Db::forge('mystore');

// pipeline some redis commands, then execute them
$result = $redis->pipeline()
	->sadd('list', 4)
	->sadd('list', 1)
	->sadd('list', 55)
	->execute();