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

发布于 2024-12-16 11:22:36 字数 1056 浏览 0 评论 0原文

我正在使用 ammap 来显示地图。单击后,用户将获得带有相应国家/地区(分类法)标记的最新 Drupal 6 节点的列表。该列表由视图生成。为了实现这一点,我使用基本的 ammap XML 代码,但我添加了一些 PHP 来包含视图,即:

<?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>

现在,由于有许多包含视图的标签,生成 XML 文件需要一些时间,这会导致加载时间较长地图。出于这个原因,我想以某种方式缓存生成的 XML 文件 - 考虑到我需要在 ammap 配置文件中添加它的路径。

我怎么能这么做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苦妄 2024-12-23 11:22:36

最好的方法是编写一个小模块。

这是最短的:

/**
 * 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();
}

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();
}
半衾梦 2024-12-23 11:22:36

您可以使用cache_set 存储生成的XML 并使用cache_get 检索它。

http://api.drupal.org/api/ drupal/includes--cache.inc/function/cache_set/6

You can store the generated XML using cache_set and retrieve it using cache_get.

http://api.drupal.org/api/drupal/includes--cache.inc/function/cache_set/6

二货你真萌 2024-12-23 11:22:36

我发现的另一个选择是通过 cron 启动 XML 的创建。在这种情况下我没有使用缓存。在自定义模块中:

<?php
function MY_MODULE_cron() {

 $content = MY_MODULE_xml();
 file_put_contents($_SERVER['DOCUMENT_ROOT'] . file_directory_path() . '/MY_FILE.XML', $content);

}

function MY_MODULE_xml() {

$page_content = '<?xml version="1.0" encoding="UTF-8"?>
...';

return $page_content;

}

?>

Another option I found was to let the creation of the XML be initiated through cron. In that case I didn't use the cache. In a custom module:

<?php
function MY_MODULE_cron() {

 $content = MY_MODULE_xml();
 file_put_contents($_SERVER['DOCUMENT_ROOT'] . file_directory_path() . '/MY_FILE.XML', $content);

}

function MY_MODULE_xml() {

$page_content = '<?xml version="1.0" encoding="UTF-8"?>
...';

return $page_content;

}

?>
追星践月 2024-12-23 11:22:36

这张地图对于每个用户来说都是不同的吗?或者它非常通用,因此非常静态?

如果是后者,我将生成地图(最有可能在 cron 运行时)并将其输出到静态文件,如sites/defaul/files/map.xml。对该文件的请求甚至不会调用 PHP 处理器,这使其成为返回该文件的最快方式,并且对 Web 服务器性能的影响最小。

Is this map something that is different for each user? Or is it pretty generic, and therefor pretty static?

If it's the latter, I would generate the map (on cron run most likely) and output it to a static file like sites/defaul/files/map.xml. Requests to that file will not even invoke the PHP processor, making it the fastest way to return it and have the least impact on your webserver's performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文