Skip to content

Instantly share code, notes, and snippets.

@Pross
Created March 19, 2013 22:45
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 Pross/5200848 to your computer and use it in GitHub Desktop.
Save Pross/5200848 to your computer and use it in GitHub Desktop.
Simple file cache
<?php
class Cache {
function __construct( $format = '' ) {
$this->format = $format;
$this->cache_life = '1200';
$this->cache_file = 'cache/' . md5( $_SERVER['REQUEST_URI'] );;
// check exists....
$this->get_cache();
}
function put_cache( $data ) {
file_put_contents( $this->cache_file, $data );
$this->do_output( $data );
}
function get_cache() {
$filemtime = @filemtime( $this->cache_file );
if ( $filemtime && ( time() - $filemtime < $this->cache_life ) ) {
$this->do_output( file_get_contents( $this->cache_file ) );
die();
}
@unlink( $this->cache_file );
}
function do_output ( $data ) {
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment