我這邊有一個(gè)多語(yǔ)言數(shù)組php文件,這邊就是寫(xiě)在function
這個(gè)里面;我每次調(diào)用多語(yǔ)言?xún)?nèi)容方法我都會(huì)去 include_once 下這個(gè)文件。我想問(wèn)下這個(gè)會(huì)不會(huì)只是加載一次;后續(xù)就不會(huì)繼續(xù)加載了吧?
代碼寫(xiě)在了 function.php
這個(gè)文件下
function getLangArray(): array
{
$lang = request()->header('lang');
$path = base_path() . "/support/lang/$lang.php";
// 判斷文件是否存在
if (!file_exists($path)) {
return include_once base_path() . "/support/lang/" . config('constants.lang') . ".php";
}
return include_once $path;
}
會(huì)不會(huì)每次請(qǐng)求進(jìn)來(lái)如果調(diào)用這個(gè)方法都是重新加載一次????
一個(gè)可愛(ài)的問(wèn)題。
<?php
for(;;){
sleep(1);
echo 1;
}
不知道你認(rèn)不認(rèn)為這是一個(gè)常駐的程序 ??
參考代碼:http://wtbis.cn/q/11036#answer_19537
<?php
namespace app\controller;
use support\Db;
use support\Request;
class IndexController
{
// 緩存的數(shù)據(jù)
protected static $data = [];
public function index(Request $request)
{
$id = $request->get('id');
return json(['result' => static::get($id)]);
}
protected static function get($id)
{
// 初始化緩存
if (!static::$data) {
ini_set('memory_limit', '512M');
static::$data = array_flip(Db::table('orders')->pluck('id')->toArray());
}
// 緩存不存在則查數(shù)據(jù)庫(kù)
if (!isset(static::$data[$id])) {
if (Db::table('orders')->find($id)) {
// 訂單存在繼續(xù)緩存
static::$data[$id] = 1;
return true;
}
return false;
}
return true;
}
}
業(yè)務(wù)初始化寫(xiě)入數(shù)據(jù):http://wtbis.cn/doc/webman/others/bootstrap.html
哥哥,我目前這邊是要加載一個(gè)語(yǔ)言數(shù)組文件在內(nèi)存中,我就是在 server-user/config/bootstrap.php 這個(gè)文件里,直接掛載全局?jǐn)?shù)組中。
$zh_CN = require 'support/lang/zh-CN.php';
$GLOBALS['zh-CN'] = $zh_CN;