104 lines
3.3 KiB
PHP
104 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Component\Models;
|
|
|
|
class Cache
|
|
{
|
|
public static function get($code, $plugin, $input)
|
|
{
|
|
unset($input['ip']);
|
|
unset($input['token']);
|
|
unset($input['admin_user_id']);
|
|
unset($input['host']);
|
|
$method = $input['method_name'];
|
|
unset($input['method_name']);
|
|
$dirname = CORE_PATH . '/../../Cache/' . $code . '/' . $plugin . '/';
|
|
if (!is_dir($dirname)) {
|
|
mkdir($dirname, 0777);
|
|
}
|
|
$filename = $method . ".tmp";
|
|
if (file_exists($dirname . $filename)) {
|
|
$caches = json_decode(file_get_contents($dirname . $filename), true);
|
|
foreach ($caches as $key => $cache) {
|
|
if ($cache['input'] == $input) {
|
|
return $cache['result'];
|
|
}
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function store($code, $plugin, $input, $result)
|
|
{
|
|
unset($input['ip']);
|
|
unset($input['token']);
|
|
unset($input['admin_user_id']);
|
|
unset($input['host']);
|
|
$method = $input['method_name'];
|
|
unset($input['method_name']);
|
|
$data = [];
|
|
$info = array('input' => $input, 'result' => $result);
|
|
array_push($data, $info);
|
|
$dirname = CORE_PATH . '/../../Cache/' . $code . '/' . $plugin . '/';
|
|
if (!is_dir($dirname)) {
|
|
mkdir($dirname, 0777);
|
|
}
|
|
$filename = $method . ".tmp";
|
|
if (!file_exists($dirname . $filename)) {
|
|
$handle = fopen($dirname . $filename, 'w');
|
|
fputs($handle, json_encode($data));
|
|
fclose($handle);
|
|
return $result;
|
|
} else {
|
|
$handle = fopen($dirname . $filename, 'r');
|
|
$caches = fgets($handle);
|
|
fclose($handle);
|
|
$caches = json_decode($caches, true);
|
|
foreach ($caches as $key => $cache) {
|
|
if ($cache == $info) {
|
|
return $info['result'];
|
|
} else {
|
|
array_push($caches, $info);
|
|
$handle = fopen($dirname . $filename, 'w');
|
|
fputs($handle, json_encode($caches));
|
|
fclose($handle);
|
|
return $info['result'];
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static function clear($code, $plugin, $input, $type)
|
|
{
|
|
$dirname = CORE_PATH . '/../../Cache/' . $code . '/' . $plugin . '/';
|
|
if ($type == 'single') {
|
|
$method = $input['method_name'];
|
|
if (is_dir($dirname)) {
|
|
$filename = $method . ".tmp";
|
|
if (file_exists($dirname . $filename)) {
|
|
unlink($dirname . $filename);
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
if ($type == 'full') {
|
|
$files = glob($dirname."/*");
|
|
if (count($files) > 0) {
|
|
foreach ($files as $file) {
|
|
if (file_exists($file)) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |