dongxing5525 2013-02-19 22:05
浏览 22
已采纳

使用PHP动态,快速地生成页面

I can create dynamic pages already, and it is working, but the landing page loads very slowly.

I would really like some help. Below is how my site functions.

Let's start with a link in 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 


?>

Now here is 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

}

?>

I just learned about sessions today (I thought it would make things faster); before, I sent the data into the address bar from search.php, then read it on the landing page to carry the variables over (cringe, I know, I am very new to php and development in general). The page load speed of the landing page has not changed.

  • 写回答

1条回答 默认 最新

  • 普通网友 2013-02-19 22:34
    关注

    As mentioned in a comment, in its current shape, your script cannot go faster than the response time of the queried API.

    You don't say anything about the API, so it's not possible to guarantee a speed up in that area. However, if that API consistently returns the same result for the same query, you could alleviate the issue by implementing a result cache.

    Basically, you should store in an persistent array the query result indexed by its parameters, and each time a query is made, check if the result is not already there. You may choose to make that array a session value, in database, or in a memory based cache mechanism, like memcache.

    Each approach has it pros and cons, and choosing one solution over another will also depends on the API:

    • a session based cache will be constrained per client, which will greatly reduce its effectiveness, but might be mandatory if the use of the API must be client confidential,
    • a db based can be somewhat effective if its access is faster than the API server, eg. a local db with a sufficiently large bandwidth,
    • the memcache option should be the fastest one, but the cache data will be lost if the service must be restarted,

    You may also combine the db and memcache, to obtain the speed of the later with the persistence of the former.

    This is all assuming that you can somewhat predict if a query can be cached, and how long it will stay valid. You will also need to have some control on the server configuration, or hope that it already supports the functionalities discussed above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等