Skip to content

Instantly share code, notes, and snippets.

@jjt
Created October 26, 2011 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjt/1315208 to your computer and use it in GitHub Desktop.
Save jjt/1315208 to your computer and use it in GitHub Desktop.
Redis example in php, with redisphp
<?php
// --------------------------------
// Plain Redis + phpredis
// --------------------------------
$redis = new Redis();
$redis->connect('127.0.0.1');
$cache = $redis->get('key');
//Cache miss
if($cache === false) {
ob_start(); // Start output buffering
// ... output what you want cached
$cache = ob_end_clean(); // Stop ob, get contents
$redis->set('key',$cache);
}
// At this point $cache is either the retrieved cache or a fresh copy, so echo it
echo $cache;
// --------------------------------
// With Redis_OB_Func
// --------------------------------
// Named function call
function my_output_fn() {
//...echo things!
}
$rob = new Redis_OB_Func('my-key','my_output_fn');
// Using closures
$rob = new Redis_OB_Func('my-key',function() {
// ... output what you want cached
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment