|
<?php |
|
|
|
class Fvs |
|
{ |
|
private $cache_path; |
|
private $file_ext; |
|
|
|
/** |
|
* @param string $cache_path |
|
* @param string $file_ext |
|
*/ |
|
public function __construct($cache_path = '.caches/', $file_ext = '.cache') |
|
{ |
|
if (!file_exists($cache_path)) { |
|
mkdir($cache_path); |
|
} |
|
|
|
$this->cache_path = $cache_path; |
|
$this->file_ext = $file_ext; |
|
} |
|
|
|
/** |
|
* @param $key_name |
|
* @return mixed |
|
*/ |
|
public function get($key_name) |
|
{ |
|
$file_name = $this->file_name($key_name); |
|
if (file_exists($file_name)) { |
|
return file_get_contents($file_name); |
|
} |
|
} |
|
|
|
/** |
|
* @param string $key_name |
|
* @param $value |
|
* @return mixed |
|
*/ |
|
public function set($key_name, $value) |
|
{ |
|
file_put_contents($this->file_name($key_name), $value); |
|
return $value; |
|
} |
|
|
|
/** |
|
* @param mixed $key_names |
|
* @return int |
|
*/ |
|
public function del($key_names) |
|
{ |
|
if (is_array($key_names)) { |
|
return $this->multi_del($key_names); |
|
} |
|
return $this->single_del($key_names); |
|
} |
|
|
|
public function flushall() |
|
{ |
|
if ($dirHandle = opendir($this->cache_path)) { |
|
while (false !== ($fileName = readdir($dirHandle))) { |
|
if ($fileName !== ".") { |
|
unlink($this->cache_path . $fileName); |
|
} |
|
} |
|
closedir($dirHandle); |
|
} |
|
} |
|
|
|
/** |
|
* @return array |
|
*/ |
|
public function keys() |
|
{ |
|
$search_value = $this->cache_path . '*.fvs'; |
|
$keys = []; |
|
$values = glob($search_value); |
|
foreach ($values as $v) { |
|
$k_name = preg_replace("/(.+)(\.[^.]+$)/", "$1", str_replace($this->cache_path, ' ', $v)); |
|
$keys[] = $k_name; |
|
} |
|
return $keys; |
|
} |
|
|
|
|
|
/** |
|
* @param array $key_names |
|
* @return int |
|
*/ |
|
private function multi_del($key_names) |
|
{ |
|
$result = 0; |
|
foreach ($key_names as $k) { |
|
$file_name = $this->file_name($k); |
|
if (unlink($file_name)) { |
|
$result += 1; |
|
} |
|
} |
|
return $result; |
|
} |
|
|
|
/** |
|
* @param string $key_name |
|
* @return int |
|
*/ |
|
private function single_del($key_name) |
|
{ |
|
if (unlink($this->file_name($key_name))) { |
|
return 1; |
|
} |
|
return 0; |
|
} |
|
|
|
/** |
|
* @param string $key_name |
|
* @return string |
|
*/ |
|
private function file_name($key_name) |
|
{ |
|
return $this->cache_path . $key_name . $this->file_ext; |
|
} |
|
|
|
} |