views:

50

answers:

3

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?

A: 

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

ya.teck
Thanks. One thing I don't like about this solution is the extra database traffic. To draw a page, I'll need to both write and read to the cache tables. Seems like something that should be only in memory since the data only needs to exist for the current page request.
rcourtna
In this case, that way is not suitable. Youг can make per request cache by *static var in call_remote_api_and_get_lots_of_results* function.
ya.teck
Right ok, that's what I'll do. I've been searching for a module that might provide an api that aims to solve this problem in a generalized way, but using static vars is simple enough.
rcourtna
A: 

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.

googletorp
Thanks, but I'm not interested in caching blocks. I'm interested in sharing data with my block.
rcourtna
@rcourtna I see what you were asking updated my answer.
googletorp
+1  A: 

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.

Denis Shishkov
very interesting module ... I'll play with it.
rcourtna

related questions