51 lines
999 B
PHP
51 lines
999 B
PHP
<?php
|
|
|
|
if (!defined('_PS_VERSION_')) {
|
|
exit;
|
|
}
|
|
|
|
class CustomCache
|
|
{
|
|
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 = 'var/cache/prod/' . $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];
|
|
}
|
|
}
|