first commit
This commit is contained in:
46
autoload/class.Cache.php
Normal file
46
autoload/class.Cache.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
class Cache
|
||||
{
|
||||
public static function store( $key, $data, $ttl = 86400 )
|
||||
{
|
||||
file_put_contents( self::get_file_name( $key ), gzdeflate( serialize( array( time() + $ttl, $data ) ) ) );
|
||||
}
|
||||
|
||||
private static function get_file_name( $key )
|
||||
{
|
||||
$md5 = md5( $key );
|
||||
$dir = 'temp/' . $md5[0] . '/' . $md5[1] . '/';
|
||||
|
||||
if ( !is_dir( $dir ) )
|
||||
mkdir( $dir , 0755 , true );
|
||||
|
||||
return $dir . 's_cache_' . $md5;
|
||||
}
|
||||
|
||||
public static function fetch( $key )
|
||||
{
|
||||
$filename = self::get_file_name( $key );
|
||||
|
||||
if ( !file_exists( $filename ) || !is_readable( $filename ) )
|
||||
return false;
|
||||
|
||||
$data = gzinflate( file_get_contents( $filename ) );
|
||||
|
||||
$data = @unserialize( $data );
|
||||
if ( !$data )
|
||||
{
|
||||
unlink( $filename );
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( time() > $data[0] )
|
||||
{
|
||||
if ( file_exists( $filename ) )
|
||||
unlink( $filename );
|
||||
return false;
|
||||
}
|
||||
|
||||
return $data[1];
|
||||
}
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user