Ca va ENCORE parler de bits!

2014-03-18  |  Tips 'n' Tricks  |  laravel4 redis

Introduction

Redis is a NoSQL database to store a value referenced by key. This server software allow to register data like "cached data" and restitute it very fast.

Install client and server

Get last stable revision from redis.io/download
tar -xvf redis-2.8.7.tar.gz
cd redis-2.8.7
Compile the client binary file and install it
make
make test
sudo make install
And finally install the redis server
cd utils/
sudo ./install_server.sh

Use it with Laravel4

To allow Laravel4 to work with a redis server, simply edit the configuration file /app/config/database.php and add the redis config array.

<\?php

return array(

	/*
	|--------------------------------------------------------------------------
	| PDO Fetch Style
	|--------------------------------------------------------------------------
	|
	| By default, database results will be returned as instances of the PHP
	| stdClass object; however, you may desire to retrieve records in an
	| array format for simplicity. Here you can tweak the fetch style.
	|
	*/

	'fetch' => PDO::FETCH_CLASS,

	/*
	|--------------------------------------------------------------------------
	| Default Database Connection Name
	|--------------------------------------------------------------------------
	|
	| Here you may specify which of the database connections below you wish
	| to use as your default connection for all database work. Of course
	| you may use many connections at once using the Database library.
	|
	*/

	'default' => 'mysql',

	/*
	|--------------------------------------------------------------------------
	| Database Connections
	|--------------------------------------------------------------------------
	|
	| Here are each of the database connections setup for your application.
	| Of course, examples of configuring each database platform that is
	| supported by Laravel is shown below to make development simple.
	|
	|
	| All database work in Laravel is done through the PHP PDO facilities
	| so make sure you have the driver for your particular database of
	| choice installed on your machine before you begin development.
	|
	*/

	'connections' => array(

		'sqlite' => array(
			'driver'   => 'sqlite',
			'database' => __DIR__.'/../database/production.sqlite',
			'prefix'   => '',
		),

		'mysql' => array(
			'driver'    => 'mysql',
			'host'      => 'MYSQL_HOST',
			'database'  => 'MYSQL_DATABASE',
			'username'  => 'MYSQL_USERNAME',
			'password'  => 'MYSQL_PASSWORD',
			'charset'   => 'utf8',
			'collation' => 'utf8_unicode_ci',
			'prefix'    => '',
		),

		'pgsql' => array(
			'driver'   => 'pgsql',
			'host'     => 'localhost',
			'database' => 'database',
			'username' => 'root',
			'password' => '',
			'charset'  => 'utf8',
			'prefix'   => '',
			'schema'   => 'public',
		),

		'sqlsrv' => array(
			'driver'   => 'sqlsrv',
			'host'     => 'localhost',
			'database' => 'database',
			'username' => 'root',
			'password' => '',
			'prefix'   => '',
		),

	),

	/*
	|--------------------------------------------------------------------------
	| Migration Repository Table
	|--------------------------------------------------------------------------
	|
	| This table keeps track of all the migrations that have already run for
	| your application. Using this information, we can determine which of
	| the migrations on disk have not actually be run in the databases.
	|
	*/

	'migrations' => 'migrations',

	/*
	|--------------------------------------------------------------------------
	| Redis Databases
	|--------------------------------------------------------------------------
	|
	| Redis is an open source, fast, and advanced key-value store that also
	| provides a richer set of commands than a typical key-value systems
	| such as APC or Memcached. Laravel makes it easy to dig right in.
	|
	*/

	'redis' => array(

		'cluster' => true,

		'default' => array(
			'username' => 'REDIS_USERNAME',
			'password' => 'REDIS_PASSWORD',
			'host'     => 'REDIS_HOST',
			'port'     => REDIS_PORT,
			'database' => 0,
		),

	),

);

Now you will be able to connect and request the cache server. Following, a little example of a very short use case (Here Laravel documentation about redis):

$redis = Redis::connection();

// Set add data in your cache server
$redis->set('domain', 'cavaencoreparlerdebits.fr');

// Now you can get back your value whenever you want
// Take to not overload it ;-)
$domain = $redis->get('domain');

 


Share this article :

blog comments powered by Disqus

© 2012-2017 Ca va ENCORE parler de bits! All Rights Reserved.