在PHP会话中存储大型网站菜单是个好主意吗?

时间:2018-09-06 17:12:09

标签: php performance session

我有一个电子商务网站,该网站的菜单系统非常大而复杂,包含许多类别和子类别。有一百多个条目。

将整个内容存储为字符串或数组作为PHP会话变量是一个好主意,这样就不会一次又一次地从数据库的DB查询导航菜单吗?

对于会话变量来说太大了吗?

2 个答案:

答案 0 :(得分:3)

  

将整个内容存储为字符串或数组作为PHP会话变量是一个好主意,这样就不会一次又一次地从数据库的DB查询导航菜单吗?

不。不要在会话中存储不是特定于该会话的数据。

有很多更好的选项可以缓存这些数据。其中有几个是

答案 1 :(得分:0)

我建议为此使用任何缓存系统。

例如,您可以使用APC(高级PHP缓存):

安装它(基于Debian的Unix的示例):

sudo apt-get install php-apc

并实际使用它:

function getMenuDataFromDatabaseWithCache()
{

// first - let's try to get results from cache
$cachedResults = apc_fetch('db_results');

// if there is a result - let's return it
if($cachedResults !== false) {
    return $cachedResults;
}

// if there is no results let's fetch all you need from the database
$fetchedResults = $mysql->someFunctionToGetData();

// store results in the cache for 1 day (1 day = 86400 seconds)
apc_add('db_results', $fetchedResults, 86400); 

// so hest time the function will return cached results, without fetching from DB
return $fetchedResults;

}
相关问题