【发布时间】:2014-10-02 20:44:51
【问题描述】:
我有一个在每次加载页面时都会被查询的数组,所以我想最小化开销并将数组存储到会话变量中。到目前为止,数组文件为 15kb。我仍然需要为每个数组子键添加大约 300 个单词。因此文件大小可能会增长到 100kb 到 500kb。只是猜测。
该数组用于存储页面的标题、描述、帖子内容等数据。
数组的结构如下:
11 个主键。
在这些主键中是从 1 到 20 的子键。大多数都有大约 3 到 7 个。
每个子键都有自己的数组,title、description 和 post。
标题和描述的内容不多,但post 将包含大约 300 个字或更少。
数组中的值将保持不变。
这是一个示例,其中包含 2 个主键和每个下的 2 个子键。
$pages = array(
'Administrator' => array(
'network-administrator' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
'database administrator' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
),
'Analyst' => array(
'business systems analyst' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
'data-analyst' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
),
);
我的问题分为三个部分。
1) 我可以将它放入会话变量中,并且仍然能够像直接从数组本身访问它一样访问会话数组中的数据吗?
2) 将数组放入会话中以减少每次页面加载时循环遍历数组的开销有什么好处?
这就是我从数组中访问值的方式
$content = $pages['Administrator']['network-administrator'];
$title = $content['title'];
$description = $content['description'];
$post = $content['post'];
3) 我现在是用上面的方法访问数组值还是这样写?
$pages = $_SESSION[$pages];
$content = $pages['Administrator']['network-administrator'];
$title = $content['title'];
$description = $content['description'];
$post = $content['post'];
需要澄清一下,感谢您的帮助。
【问题讨论】:
-
你如何维护可用atm的数组?
-
我看到的最大问题是空间效率。您要为每个访问您网站的用户将相同的数据保存到磁盘?
-
为什么你认为它会提高性能? IMO 除了您已经拥有的内容之外,它只会将您的数组的额外副本放入会话中。我看不出这有什么帮助。
-
@TheWolf 如果您的意思是来自 MySQL,不,我将手动将值添加到数组中。该数组保存在一个单独的文件中,并包含在模板文件中。
-
会话是相同的文件存储(如果未配置为使用其他内容)。您将在每个会话文件中存储数组的副本 - 您需要吗?我希望将序列化或 json 化的数据保存在单独的文件中(或使用 memcache 或 apc)