Skip to content

Instantly share code, notes, and snippets.

@nmmmnu
Created July 19, 2011 21:52
Show Gist options
  • Save nmmmnu/1093826 to your computer and use it in GitHub Desktop.
Save nmmmnu/1093826 to your computer and use it in GitHub Desktop.
PHP Cache
<?
require_once "php_cache.php";
echo 123456;
phpinfo();
<?
$CACHE_ON = true;
$CACHE_METHOD = "memcached";
// Filecache stuff...
$CACHE_DIRECTORY = "/dev/shm/" . "page_cache_";
// Memcache stuff...
$CACHE_KEY = "page_cache_";
$CACHE_HANDLE = NULL;
$CACHE_TIME = 60;
// =================================================================
// Memcached cache
// =================================================================
function memcached_cache_connect(){
global $CACHE_HANDLE;
$CACHE_HANDLE = new Memcached();
if (! $CACHE_HANDLE->addServer('localhost', 11211) )
return false;
$CACHE_HANDLE->setOption(Memcached::OPT_NO_BLOCK, true );
$CACHE_HANDLE->setOption(Memcached::OPT_TCP_NODELAY, true );
$CACHE_HANDLE->setOption(Memcached::OPT_POLL_TIMEOUT, 100 );
$CACHE_HANDLE->setOption(Memcached::OPT_RETRY_TIMEOUT, 1 );
$CACHE_HANDLE->setOption(Memcached::OPT_CONNECT_TIMEOUT, 100 );
return true;
}
function memcached_cache_key(){
global $CACHE_KEY;
return $CACHE_KEY . md5(
$_SERVER["HTTP_HOST"] .
$_SERVER["REQUEST_URI"] .
$_SERVER["QUERY_STRING"] .
// $_SERVER["HTTP_USER_AGENT"] .
""
);
}
function memcached_cache_load($key){
global $CACHE_HANDLE;
if ( ! @memcached_cache_connect())
return false;
return $CACHE_HANDLE->get($key);
}
function memcached_cache_save($key, $data){
global $CACHE_HANDLE;
global $CACHE_TIME;
if ( ! @memcached_cache_connect())
return false;
return $CACHE_HANDLE->set($key, $data, $CACHE_TIME);
}
// =================================================================
// File cache
// =================================================================
function file_cache_key(){
global $CACHE_DIRECTORY;
return $CACHE_DIRECTORY . md5(
$_SERVER["HTTP_HOST"] .
$_SERVER["REQUEST_URI"] .
$_SERVER["QUERY_STRING"] .
// $_SERVER["HTTP_USER_AGENT"] .
""
);
}
function file_cache_load($key){
return @file_get_contents($key);
}
function file_cache_save($key, $data){
return @file_put_contents($key, $data);
}
// =================================================================
function cache_key(){
global $CACHE_METHOD;
$fn_cache_key = "{$CACHE_METHOD}_cache_key";
return $fn_cache_key();
}
function cache_load($key){
global $CACHE_METHOD;
$fn_cache_load = "{$CACHE_METHOD}_cache_load";
return $fn_cache_load($key);
}
function cache_save($key, $data){
global $CACHE_METHOD;
$fn_cache_save = "{$CACHE_METHOD}_cache_save";
return $fn_cache_save($key, $data);
}
function cache_shutdown(){
$data = ob_get_contents();
// Put it into the cache
cache_save(cache_key(), $data);
}
function cache_start(){
$out = cache_load(cache_key());
if ( $out ){
// Cache hit. Deliver the page.
echo $out;
exit;
}
register_shutdown_function("cache_shutdown");
ob_start();
}
if ($CACHE_ON)
cache_start();
// Unlike other files, this file is not tested !!!
function redis_cache_connect(){
global $CACHE_HANDLE;
$CACHE_HANDLE = new Redis();
tey{
$CACHE_HANDLE->connect("127.0.0.1", "6379");
}catch(Exception $e){
$CACHE_HANDLE = NULL;
return false;
}
return true;
}
function redis_cache_key(){
global $CACHE_KEY;
return $CACHE_KEY . md5(
$_SERVER["HTTP_HOST"] .
$_SERVER["REQUEST_URI"] .
$_SERVER["QUERY_STRING"] .
// $_SERVER["HTTP_USER_AGENT"] .
""
);
}
function redis_cache_load($key){
global $CACHE_HANDLE;
if ( ! @redis_cache_connect())
return false;
return $CACHE_HANDLE->get($key);
}
function redis_cache_save($key, $data){
global $CACHE_HANDLE;
global $CACHE_TIME;
if ( ! @redis_cache_connect())
return false;
return $CACHE_HANDLE->set($key, $data);
return $CACHE_HANDLE->expire($key, $CACHE_TIME);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment