This thread aims to get the best of:

1. This newer one #349373: Replace file_put_contents with file_save_data

2. The old issue #223610: Race condition in cache file creation

and close them both.

In both issues above the proposed solutions are similar.

old issue:

+      $tempfile = $filename . getmypid();
+      if (file_put_contents($tempfile, $data) === FALSE) {
+        watchdog('boost', t('Unable to write file: %file', array('%file' => $tempfile)), WATCHDOG_WARNING);
+      } else {
+        // put the temp file in its final location
+        if (rename($tempfile,$filename) === FALSE) {
+          watchdog('boost', t('Unable to rename file: %file', array('%file' => $tempfile)), WATCHDOG_WARNING);
+        }

new issue uses : (with the problem that we need to set our cache to be in the drupal file system (where images are etc...) )

function file_save_data($data, $dest, $replace = FILE_EXISTS_RENAME) {
  $temp = file_directory_temp();
  // On Windows, tempnam() requires an absolute path, so we use realpath().
  $file = tempnam(realpath($temp), 'file');
  if (!$fp = fopen($file, 'wb')) {
    drupal_set_message(t('The file could not be created.'), 'error');
    return 0;
  }
  fwrite($fp, $data);
  fclose($fp);

  if (!file_move($file, $dest, $replace)) {
    return 0;
  }

  return $file;
}

I am actually a fan of the second one - let me explain:

till now we were accustomed that boost keeps caches in :

cache/example.com/

What if we turn this on its head and make boost keep caches in:

sites/default/files/html_cache (default site)
sites/example.com/html_cache (multisite instance)

thus :

1. we eliminate the need for an explicit set ting in the UI (less work for the user that sets up)
2. can use the drupal_file_save func safely
3. we are still multisite aware ...

So?

Comments

rsvelko’s picture

3. we are still multisite aware ...

= I see here one obstacle - since the .htaccess file is only one central file for all multisite installs it has to be general enough - a thing done currently with SERVER_NAME :

RewriteCond %{DOCUMENT_ROOT}/cache/%{SERVER_NAME}%{REQUEST_URI}_%{QUERY_STRING}.html -f
RewriteRule ^(.*)$ cache/%{SERVER_NAME}/$1_%{QUERY_STRING}.html [L]

so my proposal above will work fine for all multisites axcept for the sites/default one

3 possible solutions -
1. on boost install make the user (in that part. order )
1.1. set the new file system paths in drupal frontend
1.2. clean the caches (all caches including flush imagecache presets)
1.1. move settings.php and files/ and tmp/ from sites/default to sites/example.com - only a simple mv command - no more -

site url is the same, settings.php will work and files will work too.

2. make a symlink sites/example.com -> sites/default

3. make boost .htaccess rules somehow work both on a sites/example.com and sites/default dir structure - either by doubling the code or via some .htaccess magic ...

3 is my favourite cause seems to hold no hidden bugs and does involve tedious migration processes. (I tried 1. and it made my imges dissapear leaving me wondering what have I forgot to flush or set ... )

Either way I feel that static html_cache's place is next to the sites/example.com/js and css folders

mikeytown2’s picture

FYI, this is low on the totem pole (not a high priority) as your proposal requires a change to the .htaccess file in order for boost to keep working. Things of that nature would go in a version 2. Most important rule is to keep it simple. Right now the default dir issue makes this too complicated IMO. People running multisite would be better at handling complicated instructions.

rsvelko’s picture

Title: solve the file_put_contents race condition and decide where to keep the cache dir » move cache dir to sites/example.com/files/html_cache
Status: Active » Postponed

agree.

postponing this

mikeytown2’s picture

Status: Postponed » Closed (won't fix)

moving this down the food chain to won't fix as it would make boost too complicated... supporting subdir multi-sites wouldn't be easy.
sites/www.example.com.subdir.deep maps to http://www.example.com/subdir/deep
race condition is fixed in the latest dev.

mikeytown2’s picture

Status: Closed (won't fix) » Closed (fixed)

latest version allows for custom cache dir