public static function exec($conds, $name, $fn, $args = array(), $re = false) { $name = 'Cache::exec' . $name; return Once::exec($name, function ($args, $r, $hash) use($name, $fn, $conds, $re) { $data = Mem::get($name . $hash); if (!$data) { $data = array('time' => 0); } $execute = Access::adminIsTime($data['time'], function ($cache_time) use($conds) { if (!sizeof($conds)) { return false; //Если нет conds кэш навсегда и develop не поможет } $max_time = 1; for ($i = 0, $l = sizeof($conds); $i < $l; $i++) { $mark = $conds[$i]; $mark = Path::theme($mark); if (!$mark) { continue; } $m = filemtime($mark); if ($m > $max_time) { $max_time = $m; } if (!is_dir($mark)) { continue; } foreach (glob($mark . '*.*') as $filename) { $m = filemtime($filename); if ($m > $max_time) { $max_time = $m; } } } return $max_time > $cache_time; }, $re); if ($execute) { $cache = Nostore::check(function () use(&$data, $fn, $args, $re) { $data['result'] = call_user_func_array($fn, array_merge($args, array($re))); }); if ($cache) { $data['time'] = time(); Mem::set($name . $hash, $data); } else { Mem::delete($name . $hash); } } return $data['result']; }, array($args), $re); }
private static function load($path) { $args = array($path); $res = Once::exec('Load::load', function ($path) { //php файлы эмитация веб запроса //всё остальное file_get_content $_r_e_s_ = array(); $_r_e_s_['cache'] = !Nostore::check(function () use($path, &$_r_e_s_) { if (Path::isDir($path)) { $p = explode('?', $path, 2); $p[0] .= 'index.php'; $path = implode('?', $p); } $load_path = Path::theme($path); $fdata = Load::srcinfo($load_path); if ($load_path && $fdata['file']) { $plug = Path::theme($fdata['path']); if ($fdata['ext'] == 'php') { $getstr = Path::toutf($fdata['query']); //get параметры в utf8, с вопросом $getstr = preg_replace("/^\\?/", '', $getstr); parse_str($getstr, $get); if (!$get) { $get = array(); } $GET = $_GET; $_GET = $get; $REQUEST = $_REQUEST; $_REQUEST = array_merge($_GET, $_POST, $_COOKIE); $SERVER_QUERY_STRING = $_SERVER['QUERY_STRING']; $_SERVER['QUERY_STRING'] = $getstr; $from_php_old = Load::isphp(); Load::isphp(true); ob_start(); //headers надо ловить $ans = array(); $rrr = (include $plug); $result = ob_get_contents(); $resecho = $result; ob_end_clean(); Load::isphp($from_php_old); if ($rrr !== 1 && !is_null($rrr)) { //Есть возвращённый результат $result = $rrr; if ($resecho) { //Сообщение об ошибке... далее всё ломаем $result = $resecho . Load::json_encode($result); //Есть вывод в браузер и return } } $_SERVER['QUERY_STRING'] = $SERVER_QUERY_STRING; $_REQUEST =& $REQUEST; $_GET =& $GET; $data = $result; //$data='php file'; } else { $data = file_get_contents($plug); } $_r_e_s_ = array(); //Если в include это имя использовалось. Главное чтобы оно небыло ссылкой & $_r_e_s_['status'] = 200; $_r_e_s_['value'] = $data; } else { $_r_e_s_['status'] = 404; $_r_e_s_['value'] = ''; } }); return $_r_e_s_; }, $args); if (!$res['cache']) { Nostore::on(); } return $res['value']; }
public static function cache($name, $fn, $args = array(), $re = false) { //Запускается один раз для админа, остальные разы возвращает кэш из памяти $name = 'Access::cache ' . $name; return Once::exec($name, function ($args, $name) use($name, $fn, $re) { $path = $name . '_' . Hash::make($args); $data = Mem::get($path); if (!$data) { $data = array('time' => 0); } $execute = self::adminIsTime($data['time'], function () { return true; }, $re); if ($execute) { $cache = !Nostore::check(function () use(&$data, $fn, $args, $re) { $data['result'] = call_user_func_array($fn, array_merge($args, array($re))); }); if ($cache) { $data['time'] = time(); Mem::set($path, $data); } else { Mem::delete($path); } } return $data['result']; }, array($args, $name), $re); }