forge($name = 'default', $config = array())
Create a new named redis instance, optionally with a custom configuration
Static | Yes | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
|||||||||
Returns | Fuel\Core\Redis Object | |||||||||
Example |
|
The Redis_Db class allows you to interact with a Redis key-value store.
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 |
|
Hostname |
port | integer |
|
Port to use in the connection |
timeout | mixed |
|
Connection timeout |
database | integer |
|
Database zero-based index |
password | mixed |
|
Optional password if the database requires authentication |
Create a new named redis instance, optionally with a custom configuration
Static | Yes | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
|||||||||
Returns | Fuel\Core\Redis Object | |||||||||
Example |
|
Return a named instance that was created earlier.
Static | Yes | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | Fuel\Core\Redis Object | ||||||
Example |
|
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>";
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();