Hi guys, I've been using Drupal for about a year, and now I'm trying to start working with hooks to do some cool stuff also to making an increase in our Drupal's performance .

I'm trying to create a dynamic pages module, which is intended to grab selected content types configured in a backend config form.

I managed to create a new "dynamic_page" content type, which holds the configuration of the content i need to show using some custom tables and the following fields:

Custom node type DYNAMIC_PAGE fields:
- Page ID
- Page title
- max nodes count

Custom tables i'm using:

- Sections: this is a heading, content pane, footer, etc.
--- Section ID
--- Section name

- Page_section: this is the relation between the dynamic page node, and the section i want in that page.
--- Page ID = The page im building
--- Section ID = identifier
--- Position = order in which need to appear on the page
--- NodeID = The Drupal's node i want to show in the page

Now, sections can appear several times in the same page, for example, i coud add 2 times a content pane made of different content types like: a heading, an image gallery content pane, an article content pane, then a footer. This page structure would look something like this:

HEADING node
-----------------
GALLERY node
. . . . . . . . .
ARTICLE node
-----------------
FOOTER node

So, to get this working, i simply hooked the node view and added some logic to build the page:

function dynamic_pages_node_view($node, $view_mode, $langcode) {
  switch ($node->type){
    case 'dp_heading':
         . . . . . did some logic here
      break;
    case 'dp_carousel':
         . . . . . did some other logic here
      break;

  }
  return $node;
}

Everything goes fine here now i want to cache all this. I mean, some of this pages have a huge amount of visitors, and i don't want to build the page every time a user asks for it.

In other words, i don't want Drupal to query the DB looking for the dynamic_page node (and building all the stuff), I just want it to be loaded in REDIS (this and teh entire page) and be delivered to the user directly.

I've found this REDIS official project: https://www.drupal.org/project/redis, but it doesn't solve my problem.

I have also configured the website homepage to be one of this dynamic pages, and couldn't find a way to use REDIS cache instead of having Drupal loading the dynamic page node form the database.

How can i do this? does anyone knows? Any help will be much appreciated

Thanks in advance!

Comments

Jaypan’s picture

The Redis module is actually quite handy - it handles all the redis connection for you, so it's one I would suggest using.

Then after you have built the page, give it a unique ID based on it's content, then use the redis connection provided by the module to push it to redis. On page load, check to see if that key exists in redis, and if it does, pull from there, otherwise build the page.

Note however that if you have any forms on your page, this will not work as the forms will expire after the form has been used a single time, yet the same form will continue to be served from the database.

Your better bet may be to use the Entitycache module combined with the Redis module which will cache the entities, speeding things up, and then build your page the way you are with each page load. EntityCache and Redis work well together so you shouldn't have to worry about any caching issues.

daniel.tenzi’s picture

... I've seen the redis module, that should do the cache piece, but still i don't know how to make Drupal skip its query and use my cached stuff.

I figured out i need to change the way Drupal works, i'm trying to find a way to intersect the precise moment of the DB query execution, just right before Drupal goes to grab the content to be displayed. I'd love to have something like a "hook_query_override", where i can go get my content from Redis, and return that, skipping completely the rest Drupal's query code.

So, I've taken other approaches, and did a lot of research:
- hook_views_pre_execute: This will let me change the SQL query, but i can't avoid the query.
- hook_views_query_alter: Same thing, i can alter the query results, but can't avoid Drupal making the query
- hook_page_build: to late to make any change to the query
- hook_node_load: this let's me work at node level (which is too late), still no way i can skip the query
- hook_node_view: this let's me work at node level (which is too late), still no way i can skip the query
- hook_boot: Too early to make any change here, still in bootstrap mode.
- hook_init: still too early, i need the entire pipeline working and the themes engine too, plus, this hook doesn't run for cached pages.
- Views Cache: I went down to the core here, and found that this uses the database for caching, precisely the cache table for it. Definitely not what i need.

So, it all comes to one question:

Is there a Hook that lets me override the $view->$query->execute() method telling it "this list of nodes is cached, just return that"?

Is there any other way i can do that? maybe extending a Class? Could anyone show me a quick example?