与块共享数据

发布于 2024-09-29 07:26:15 字数 735 浏览 3 评论 0原文

我有一个显示一些数据的页面。数据源不是Drupal节点,所以视图对我来说没有用:

function mymodule_main_page($arg1, $arg2, $arg3) {
  $results = call_remote_api_and_get_lots_of_results($arg1, $arg2, $arg3);
  return theme('mymodule_page', $results, $arg1, $arg2, $arg3);
}

我的模块也显示一个块。该块的目的是总结主页内容中返回的结果(例如:结果数:X,页数:Y 等)

/**
 * Implementation of hook_block().
 */
function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'view':
      if ($delta == 0) {
        $block['subject'] = t('Results summary');
        $block['content'] = theme('mymodule_results_summary');
      }
      break;
  }
}

我需要避免再次生成结果。我的块访问绘制主页的函数中返回的结果对象的最佳方式是什么?全局变量还是静态变量?是否存在已经尝试解决此问题的模块?

I have a page that displays some data. The source of the data is not Drupal nodes, so Views is of no use me:

function mymodule_main_page($arg1, $arg2, $arg3) {
  $results = call_remote_api_and_get_lots_of_results($arg1, $arg2, $arg3);
  return theme('mymodule_page', $results, $arg1, $arg2, $arg3);
}

My module also displays a block. The block purpose is to summarize the the results that were returned in the main page content (eg: Number of results: X, Number of pages: Y, etc)

/**
 * Implementation of hook_block().
 */
function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'view':
      if ($delta == 0) {
        $block['subject'] = t('Results summary');
        $block['content'] = theme('mymodule_results_summary');
      }
      break;
  }
}

I need to avoid generating the results again. What is the best way for my block to access the results object returned in the function that drew the main page? Global or Static vars? Is there a module that exists that already attempts to solve this problem?

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

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

发布评论

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

评论(3

吃颗糖壮壮胆 2024-10-06 07:26:15

非常好的和灵活的解决方案是使用drupal核心函数cache_set和cache_get,如ya.teck提到的,但使用 cacherouter 模块。您可以指定缓存存储引擎并使用 memcache 或共享内存进行缓存。它不使用数据库来存储数据并且速度非常快。

Very good and flexible solution is using drupal core functions cache_set and cache_get as ya.teck mentioned but extend its functionality with cacherouter module. You can specify cache storage engines and use memcache or shared memory for you cache. It doesn't use database for storing data and very fast.

浪漫人生路 2024-10-06 07:26:15

除了ya.teck提到的缓存系统之外,更简单的方法是将整个块缓存x分钟、小时、天。 Drupal 有一个针对所有块的内置缓存系统。您可以在 admin/settings/performance 中查看一些设置

更新:
drupal 的核心和 contrib 方式都是使用静态变量、数组或实际变量,并将繁重的工作存储在那里。一个例子是node_load,它将所有加载的节点存储在一个数组中,因此每个节点在每个请求期间只需要加载一次。

In addition to the cache system that ya.teck mentions, a more simple way is to cache the entire block for x mins, hours, days. Drupal has a built in cache system for all blocks. You can see some of the settings at admin/settings/performance

Update:
The drupal way both core and contrib is to use a static variable an array or the actual variable and store the heavy lifting there. An example could be node_load, which stores all of the loaded nodes in an array so each node only needs to be loaded once during each request.

情话墙 2024-10-06 07:26:15

您可以通过 drupal 缓存系统存储您的数据。
请参阅 cache_setcache_get 函数了解更多信息。

You may store your data by drupal cache system.
See cache_set and cache_get functions for more information.

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