如何在 Drupal 中缓存 PHP 生成的 XML 文件?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最好的方法是编写一个小模块。
这是最短的:
The best way to do, it is to write a small module.
Here's the shortest:
您可以使用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
我发现的另一个选择是通过 cron 启动 XML 的创建。在这种情况下我没有使用缓存。在自定义模块中:
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:
这张地图对于每个用户来说都是不同的吗?或者它非常通用,因此非常静态?
如果是后者,我将生成地图(最有可能在 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.