dpge74512 2011-11-17 08:53
浏览 35
已采纳

如何在Drupal中缓存PHP生成的XML文件?

I'm using ammap to display a map. On click, the user gets a list of latest Drupal 6 nodes tagged with the respective country (taxonomy). The list is generated by a view. To accomplish that, I use the basic ammap XML code, but I added some PHP to include the view, i.e.:

<?php
//set the working directory
chdir('..');
define('DRUPAL_ROOT', getcwd());

//Load Drupal
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 

header ("Content-Type:text/xml");

?>

<map map_file="maps/world3.swf" tl_long="-117.2" tl_lat="33.3" br_long="-94.5" br_lat="-33.9" zoom="299.9999%" zoom_x="-30.94%" zoom_y="-156.8%">
  <areas>
      <!-- ... -->
      <area title="ARGENTINE" mc_name="AR">
        <description><![CDATA[<?php print views_embed_view('MY_VIEW', 'VIEW_DISPLAY_ID', 'ARGUMENT'); ?>]]></description>
      </area>
      <!-- ... -->
  </areas>
</map>

Now, since there are many tags that include a view, generating the XML file takes some moments which leads to long loading times for the map. For that reason I would like to cache the generated XML file somehow - taking into account that I need to add a path to it in the ammap configuration file.

How could I do that?

  • 写回答

4条回答 默认 最新

  • dongxu4023 2011-11-18 15:47
    关注

    The best way to do, it is to write a small module.

    Here's the shortest:

    /**
     * Implement hook_menu()
     * to define path for our xml file.
     */
    function mymodule_menu() {
        $items = array();
        $items['map.xml'] = array(
            'title' => 'Map xml',
            'page callback' => 'map_get_xml',
            'access arguments' => TRUE,
            'type' => MENU_CALLBACK
        );
        return $items;
    }
    
    /**
     * Your custom function for xml file.
     */
    function map_get_xml() {
        $cache = cache_get('your-cache-id');
        $xml = $cache->data;
    
        if (!$xml) {
            $xml = ... // perform your code to generate your XML
    
            cache_set('your-cache-id', $xml);
        }
    
        drupal_set_header("Content-Type:text/xml");
        print $xml;
        exit();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题