WEB Drupal question (clear_cache_all when certain node types are saved)

Discussion in 'OT Technology' started by Mikenotmike, Jan 27, 2010.

  1. Mikenotmike

    Mikenotmike

    Joined:
    Jun 1, 2001
    Messages:
    6,244
    Likes Received:
    0
    Location:
    USA
    Not sure if anyone can help, but the Drupal IRC channel is slow right now.

    My module has many cache_set() functions saving data to a custom cache table in the database. I can clear the cache table with a clear_cache_all() function, but I only want the function to run when a specific content type is saved. Anyone know what hook I would invoke?
     
  2. Ractoon

    Ractoon Shibumi in progress...

    Joined:
    Apr 30, 2001
    Messages:
    256
    Likes Received:
    0
    hook_insert() if you're saving a new node.
    hook_update() if you're saving an edited node, whose core data has already been written to the database.
     
  3. Ractoon

    Ractoon Shibumi in progress...

    Joined:
    Apr 30, 2001
    Messages:
    256
    Likes Received:
    0
    Just a note, those will only work if you're invoking those on it's own type. For example somecontent_insert() will only work on the somecontent module key.

    If you wanted to target nodes that aren't the same type, you'll need to look at hook_nodeapi() and the $op parameter that is passed in if $op == 'insert' or $op == 'update'
     
  4. Mikenotmike

    Mikenotmike

    Joined:
    Jun 1, 2001
    Messages:
    6,244
    Likes Received:
    0
    Location:
    USA
    Thank you, my modules only purpose at the moment is to establish the custom cache table, and force that cache table to be cleared whenever a certain ubercart content type (class) is edited or created.

    So I have a cachechef.install that adds the table to the database. And of course my cachechef.info.

    And this is my cachechef.module
    Code:
    <?php
    // $Id$
    
    /**
     * @file cachechef.install
     * Cache system chefnstration module.
     */
    
    /**
     * Implementation of hook_flush_caches().
     *
     * This tells Drupal's cache handling system the name of our caching 
     * table, so expired items will be purged automatically and this table 
     * also affected by the empty all caches function.
     */
    function cachechef_flush_caches() {
      return array('cache_cachechef');
    }
    
    /**
     * Implementation of hook_cron().
     *
     * For chefnstration purposes, we will use hook_cron to write some bogus 
     * data to our cache table, so if you keep running cron you'll see the 
     * table fill up and get emptied as records expire.
     */
    function cachechef_cron() {
      $time = time();
      // Both key and value is current timestamp, and it expires in an hour.
      cache_set($time, $time, 'cache_cachechef', $time + 3600);
    }
    
    I would assume I need to use hook_nodeapi() in this case?
     
  5. Ractoon

    Ractoon Shibumi in progress...

    Joined:
    Apr 30, 2001
    Messages:
    256
    Likes Received:
    0
    hook_nodeapi() sounds right for what you're trying to do. Good luck!
     
  6. Mikenotmike

    Mikenotmike

    Joined:
    Jun 1, 2001
    Messages:
    6,244
    Likes Received:
    0
    Location:
    USA
    Thank you, I went with the following

    Code:
    <?php
    // $Id$
    
    /**
     * @file cachechef.install
     * Cache system chefnstration module.
     */
    
    /**
     * Implementation of hook_flush_caches().
     *
     * This tells Drupal's cache handling system the name of our caching 
     * table, so expired items will be purged automatically and this table 
     * also affected by the empty all caches function.
     */
    function cachechef_flush_caches() {
      return array('cache_cachechef');
    }
    
    /**
     * Implementation of hook_cron().
     *
     * For chefnstration purposes, we will use hook_cron to write some bogus 
     * data to our cache table, so if you keep running cron you'll see the 
     * table fill up and get emptied as records expire.
     */
    function cachechef_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
    {
      switch ($op) {
        case 'insert':
          if($node->type == 'menu-item') {
    		cache_clear_all('*', 'cache_cachechef', $wildcard = TRUE);
          }
        break;  
        case 'update':
          if($node->type == 'menu-item') {
    		cache_clear_all('*', 'cache_cachechef', $wildcard = TRUE);
          }
        break;
      }
    }
    
    But that caused a host of other problems, I'll have to revisit this later I guess
     

Share This Page