Last active
March 7, 2021 12:59
-
-
Save navt/257c8d495d798d05c7d175afedcacd68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class cmsCacheFiles { | |
private $cache_path; | |
public function __construct($config) { | |
$this->cache_path = $config->cache_path.'data/'; | |
} | |
public function set($key, $value, $ttl){ | |
$data = array( | |
'ttl' => $ttl, | |
'time' => time(), | |
'value' => $value | |
); | |
list($path, $file_path) = $this->getPathAndFile($key); | |
@mkdir($path, 0777, true); | |
@chmod($path, 0777); | |
@chmod(pathinfo($path, PATHINFO_DIRNAME), 0777); | |
$file_path_tmp = $file_path.'.tmp'; | |
$success = file_put_contents($file_path_tmp, serialize($data)); | |
if($success){ | |
rename($file_path_tmp, $file_path); | |
} | |
return $success; | |
} | |
public function has($key){ | |
list($path, $file) = $this->getPathAndFile($key); | |
return is_readable($file); | |
} | |
public function get($key){ | |
list($path, $file) = $this->getPathAndFile($key); | |
if (false === $s = file_get_contents($file)) { | |
return false; | |
} | |
$data = unserialize($s); | |
if (!isset($data['value']) || | |
!isset($data['time']) || | |
!isset($data['ttl']) || | |
time() > ($data['time'] + $data['ttl'])){ | |
$this->clean($key); | |
return false; | |
} | |
return $data['value']; | |
} | |
public function clean($key=false){ | |
if ($key){ | |
$path = $this->cache_path . str_replace('.', '/', $key); | |
if(is_file($path.'.dat')){ | |
@unlink($path.'.dat'); | |
} | |
return files_remove_directory($path); | |
} else { | |
return files_clear_directory($this->cache_path); | |
} | |
} | |
public function getPathAndFile($key){ | |
$path = $this->cache_path.str_replace('.', '/', $key); | |
return array(dirname($path), $path.'.dat'); | |
} | |
public function start(){ return true; } | |
public function stop(){ return true; } | |
public function getStats(){ | |
return array(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment