Created
October 26, 2011 02:12
-
-
Save jjt/1315208 to your computer and use it in GitHub Desktop.
Redis example in php, with redisphp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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