PHP MySQL Redis

MySQL cache in PHP

$r = new Redis();
$r->connect("127.0.0.1", "6379");

$id = (int) $_REQUEST["id"];
$sql = "select * from users where id = $id";

// Create a hash key
$key = "sql_cache:" . md5($sql);

// Check if data is in cache.
if ($data = @unseralize( $r->get($key) ) === false){
	$data = exec_db_query($sql);
	
	// Put data into cache for 1 hour
	$r->set($key, serialize($data) );
	$r->expires($key, 3600);
}

display_data($data);

Redis connection note

The example uses generic connection to the Redis server. To make the example work with our service you will need to use code similar to this one:

// change these parameter according to the information in your instance list
$host = '50.30.35.9';
$port = 1234;
$password = 'somehashcode';
$db = 0;

$r = new Redis();
$r->connect($host, $port);
$r->auth($password);
$r->select($db);

Link : http://redis4you.com/code.php?id=001