Igor Simic
8 years ago

How to clean specific page from cache in Drupal


To clear a single URL from cache in Drupal and from boost cache without clearing complete cache  here is what needs to be done.

To clear DB cache you have to get node id for your url and call the function cache_clear_all(); with node id and table name as parameters:

cache_clear_all('field:node:'.$value['nid'], 'cache_field');

to get node id based on URL you can create custom function which will get it for you:

function _drupal_gwo_get_nodeid_by_alias($alias, $language) {
  $nid = drupal_lookup_path('source', $alias, $language);
  if (substr( $nid, 0, 5 ) == "node/") {
    $nid_length = strlen($nid);
    $nid = substr($nid, 5, $nid_length);
    return $nid;
  }
  else {
    return 0;
  }
}

and then to get id just call that function 
$form_state['values']['nid'] = _drupal_gwo_get_nodeid_by_alias('page_url', 'language');

To clear single page from boost cache (file cache) using boost moduel, you can create function to clear single page or all similar pages if they have additional parameters. So for example you can have more than one instances of same page:
dlp/first_page.html
dlp/first_page.html?param=1&param=2


function boostcachecleaneri18n_delete_single_page($url_alias = NULL, $langcode, $similar = FALSE) {

  if (!$url_alias) {
    return;
  }

  global $_boost;
 
  $base_dir = _boostcachecleaneri18n_get_base_dirs()[$langcode];
  if (empty($base_dir)) {
    dpm('Directory unknown: '.$base_dir);
    return;
  }
   
   
    // if similar = true search for files with same base URL - it could be that some pages are cached with parameters and delete them all
    if($similar){

      foreach(glob($_SERVER['DOCUMENT_ROOT'].$base_dir.$url_alias.'*') as $file){ 
   
        unlink($file); 

        watchdog('boostcachecleaneri18n', 'Page: %page is deleted from boost cache from directory  ', array('%page' => $url_to_delete), WATCHDOG_NOTICE);
  
      }// loop files

    }else{

      // prepare file URL to delete
      $url_to_delete = $_SERVER['DOCUMENT_ROOT'].$base_dir.$url_alias.'_.html';
 
      // if file for deletion exists
      if( file_exists($url_to_delete) ){
        unlink($url_to_delete);
        //dpm("file deleted: ".$url_to_delete);

        watchdog('boostcachecleaneri18n', 'Page: %page is deleted from boost cache from directory  ', array('%page' => $url_to_delete), WATCHDOG_NOTICE);

      }else{

        dpm('Page: '.$url_alias.' is not cached!');

      } 

    }// if similar = true

  
  
}

function to get base URL looks like this:
function _boostcachecleaneri18n_get_base_dirs() {
  global $base_path /*, $base_root*/ ;
  
  $base_dirs = array();
  $active_languages = _boostcachecleaneri18n_get_active_languages(); 
  
  foreach ($active_languages as $active_language) {
    $parts = parse_url($active_language->domain);
     if (!empty($parts['host'])) {
      $base_dirs[$active_language->language] = boost_get_normal_cache_dir() . '/' . $parts['host'] . $base_path;
    }
  }
  return $base_dirs;
}