Redis
is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, , lists, , and sorted sets.
Before using Redis with Laravel, you will need to install the package (~1.0) via Composer.
The Redis configuration for your application is stored in the config/database.php
file. Within this file, you will see a redis
array containing the Redis servers used by your application:
The cluster
option will tell the Laravel Redis client to perform client-side sharding across your Redis nodes, allowing you to pool nodes and create a large amount of available RAM. However, note that client-side sharding does not handle failover; therefore, is primarily suited for cached data that is available from another primary data store.
If your Redis server requires authentication, you may supply a password by adding a password
key / value pair to your Redis server configuration array.
You may get a Redis instance by calling the Redis::connection
method:
This will give you an instance of the default Redis server. If you are not using server clustering, you may pass the server name to the connection
method to get a specific server as defined in your Redis configuration:
$redis->set('name', 'Taylor');
$name = $redis->get('name');
$values = $redis->lrange('names', 5, 10);
Notice the arguments to the command are simply passed into the magic method. Of course, you are not required to use the magic methods, you may also pass commands to the server using the method:
When you are simply executing commands against the default connection, just use static magic methods on the Redis
class:
Redis::set('name', 'Taylor');
$name = Redis::get('name');
$values = Redis::lrange('names', 5, 10);
Note: Redis and session drivers are included with Laravel.
Pipelining should be used when you need to send many commands to the server in one operation. To get started, use the command: