Redis クラス

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

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

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

Static Yes
パラメータ
パラメータ 規定値 説明
$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.
返り値 Fuel\Core\Redis Object
// create the Redis 'mystore' instance
$redis = Redis::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::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::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::forge('mystore');

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