| 1 |
<?php |
| 2 |
|
| 3 |
class RL21CFile{ |
| 4 |
|
| 5 |
public const TTL_LONG = "long"; |
| 6 |
public const TTL_SHORT = "short"; |
| 7 |
|
| 8 |
private $fp_long = ''; |
| 9 |
private $fp_short = ''; |
| 10 |
|
| 11 |
public function __construct($request_url) |
| 12 |
{ |
| 13 |
$hash = md5($request_url); |
| 14 |
$this->fp_long = RL21UtilWP::get_cache_dir(self::TTL_LONG).DIRECTORY_SEPARATOR.$hash; |
| 15 |
$this->fp_short = RL21UtilWP::get_cache_dir(self::TTL_SHORT).DIRECTORY_SEPARATOR.$hash; |
| 16 |
} |
| 17 |
|
| 18 |
public function exists($ttl, $shouldBeAfter=0){ |
| 19 |
$fp = $ttl==RL21CFile::TTL_LONG ? $this->fp_long.'_c': $this->fp_short.'_c'; |
| 20 |
$fe = file_exists($fp); |
| 21 |
if($fe && $shouldBeAfter && $shouldBeAfter>631152000){ |
| 22 |
$mt = filemtime($fp); |
| 23 |
if($mt && $mt<$shouldBeAfter){ |
| 24 |
//post is modified after cache was generated |
| 25 |
$fe = false; |
| 26 |
} |
| 27 |
} |
| 28 |
return $fe; |
| 29 |
} |
| 30 |
|
| 31 |
public function delete($ttl){ |
| 32 |
$fp = $ttl==RL21CFile::TTL_LONG ? $this->fp_long: $this->fp_short; |
| 33 |
$count = 0; |
| 34 |
if(is_file($fp.'_c') && @unlink($fp.'_c')){ |
| 35 |
$count++; |
| 36 |
} |
| 37 |
if(is_file($fp.'_h') && @unlink($fp.'_h')){ |
| 38 |
$count++; |
| 39 |
} |
| 40 |
return $count; |
| 41 |
} |
| 42 |
|
| 43 |
public function invalidate(){ |
| 44 |
$fp = $this->fp_long.'_c'; |
| 45 |
if(is_file($fp)) { |
| 46 |
$content = file_get_contents($fp); |
| 47 |
if($content!==false){ |
| 48 |
$content = str_ireplace(['"rlCacheRebuild": "N"', '"rlCacheRebuild":"N"'], '"rlCacheRebuild": "Y"', $content); |
| 49 |
} |
| 50 |
RabbitLoader_21_Util_Core::fpc($fp, $content, WP_DEBUG); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public function &get($ttl, $type){ |
| 56 |
$fp = $ttl==RL21CFile::TTL_LONG ? $this->fp_long: $this->fp_short; |
| 57 |
$content = ''; |
| 58 |
if(file_exists($fp.'_'.$type)){ |
| 59 |
$content = file_get_contents($fp.'_'.$type); |
| 60 |
} |
| 61 |
return $content; |
| 62 |
} |
| 63 |
|
| 64 |
public function serve(){ |
| 65 |
if($this->exists(RL21CFile::TTL_LONG)){ |
| 66 |
$content = file_get_contents($this->fp_long.'_c'); |
| 67 |
if ($content!==false){ |
| 68 |
if (RabbitLoader_21_Core::strHasValidClosingTags($content)) { |
| 69 |
if(file_exists($this->fp_long.'_h')){ |
| 70 |
//header is optional |
| 71 |
RabbitLoader_21_Util_Core::send_headers(file_get_contents($this->fp_long.'_h')); |
| 72 |
} |
| 73 |
RabbitLoader_21_Core::sendHeader('x-rl-cache: hit', true); |
| 74 |
echo $content; |
| 75 |
return true; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
public function save($ttl, &$content, &$headers){ |
| 83 |
$fp = $ttl==RL21CFile::TTL_LONG ? $this->fp_long: $this->fp_short; |
| 84 |
$headers = json_encode($headers, JSON_INVALID_UTF8_IGNORE); |
| 85 |
$count = 0; |
| 86 |
if(RabbitLoader_21_Util_Core::fpc($fp.'_h', $headers, WP_DEBUG)){ |
| 87 |
$count++; |
| 88 |
} |
| 89 |
if(RabbitLoader_21_Util_Core::fpc($fp.'_c', $content, WP_DEBUG)){ |
| 90 |
$count++; |
| 91 |
} |
| 92 |
return $count; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
?> |