[英]Generating pages dynamically and quickly with PHP
我已经可以创建动态页面,并且可以正常工作,但是登录页面的加载速度非常慢。
我真的很想帮忙。 以下是我的网站的功能。
让我们从http://example.com/search.php
的链接开始:
<?php
session_start();
//(pretend there is code here that gets, decodes, and displays data from an api)
$title = $titleFromApi;
$a = $dataFromApi;
$_SESSION['storeTitle'] = $title; // stores 'title' in a session variable
$_SESSION['store_a'] = $a; // stores 'a' in a session variable
echo '<a href="http://example.com/'. $a .'/' . $title .'> ' . $title . '</a>';
// the line above is a clickable link that will take them to the landing page
?>
现在,这是登录页面( http://example.com/$a/$title
):
<?php
session_start();
$al = $_SESSION['store_a']; // stores session variable in new variable 'al'
$getter = 'http://api.somewebsite.com/dev/' . $al . '/get_these_variables';
// the line above gets data from an api using variable 'al'
// (pretend that there is code here that decodes the data)
// the code below displays the data retrieved from the api
foreach($data as $entry){
echo '
<div>
' . $entry['decoded_data_1']
. '
</div>
<div>
' . $entry['decoded_data_2'] // and so on
. '
</div>
'; // ends echo
}
?>
我今天刚刚了解了一些会议(我认为这样可以使事情更快)。 之前,我将数据从search.php发送到地址栏中,然后在登录页面上进行读取以携带变量(很遗憾,我知道,对于php和一般开发人员来说我还是很陌生)。 登陆页面的页面加载速度没有改变。
正如评论中提到的那样,在当前状态下,脚本的运行速度不会比查询的API的响应时间快。
您没有说任何有关API的信息,因此无法保证该领域的速度。 但是,如果该API始终为同一查询返回相同的结果,则可以通过实现结果缓存来缓解此问题。
基本上,您应该将通过其参数索引的查询结果存储在持久性数组中,并且每次进行查询时,请检查结果是否还不存在。 您可以选择使该数组成为数据库或基于内存的缓存机制(如memcache)中的会话值。
每种方法各有利弊,选择一个解决方案而不是另一个解决方案还取决于API:
您还可以将db和memcache结合使用,以获得前者的持久性和后者的速度。
所有这些都假设您可以某种程度地预测是否可以缓存查询,以及查询将保持有效的时间。 您还需要对服务器配置进行一些控制,或者希望它已经支持上面讨论的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.