Index: common.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/common.inc,v retrieving revision 1.1251 diff -u -p -r1.1251 common.inc --- common.inc 12 Nov 2010 02:59:30 -0000 1.1251 +++ common.inc 12 Nov 2010 16:08:48 -0000 @@ -3316,60 +3316,91 @@ function drupal_build_css_cache($css) { $data = ''; $uri = ''; $map = variable_get('drupal_css_cache_files', array()); - $key = hash('sha256', serialize($css)); + $version = variable_get('drupal_css_cache_version', 1); + $key = hash('sha256', $version . serialize($css)); if (isset($map[$key])) { $uri = $map[$key]; } - if (empty($uri) || !file_exists($uri)) { - // Build aggregate CSS file. - foreach ($css as $stylesheet) { - // Only 'file' stylesheets can be aggregated. - if ($stylesheet['type'] == 'file') { - $contents = drupal_load_stylesheet($stylesheet['data'], TRUE); - // Return the path to where this CSS file originated from. - $base = base_path() . dirname($stylesheet['data']) . '/'; - _drupal_build_css_path(NULL, $base); - // Prefix all paths within this CSS file, ignoring external and absolute paths. - $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents); - } - } + $lock_acquire_attempts = 0; + $lock_name = 'css_cache:' . $key; + $mtime = $uri ? filemtime($uri) : 0; + do { + if (empty($uri) || !$mtime || (isset($map['clear']) && $mtime < $map['clear'])) { + if ($lock_acquired = lock_acquire($lock_name)) { + // Build aggregate CSS file. + foreach ($css as $stylesheet) { + // Only 'file' stylesheets can be aggregated. + if ($stylesheet['type'] == 'file') { + $contents = drupal_load_stylesheet($stylesheet['data'], TRUE); + // Return the path to where this CSS file originated from. + $base = base_path() . dirname($stylesheet['data']) . '/'; + _drupal_build_css_path(NULL, $base); + // Prefix all paths within this CSS file, ignoring external and absolute paths. + $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents); + } + } - // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, - // @import rules must proceed any other style, so we move those to the top. - $regexp = '/@import[^;]+;/i'; - preg_match_all($regexp, $data, $matches); - $data = preg_replace($regexp, '', $data); - $data = implode('', $matches[0]) . $data; - - // Prefix filename to prevent blocking by firewalls which reject files - // starting with "ad*". - $filename = 'css_' . drupal_hash_base64($data) . '.css'; - // Create the css/ within the files folder. - $csspath = 'public://css'; - $uri = $csspath . '/' . $filename; - // Create the CSS file. - file_prepare_directory($csspath, FILE_CREATE_DIRECTORY); - if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) { - return FALSE; - } - // If CSS gzip compression is enabled, clean URLs are enabled (which means - // that rewrite rules are working) and the zlib extension is available then - // create a gzipped version of this file. This file is served conditionally - // to browsers that accept gzip using .htaccess rules. - if (variable_get('css_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) { - if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($data, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { - return FALSE; + // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, + // @import rules must proceed any other style, so we move those to the top. + $regexp = '/@import[^;]+;/i'; + preg_match_all($regexp, $data, $matches); + $data = preg_replace($regexp, '', $data); + $data = implode('', $matches[0]) . $data; + + // Prefix filename to prevent blocking by firewalls which reject files + // starting with "ad*". + $filename = 'css_' . drupal_hash_base64($data) . '.css'; + // Create the css/ within the files folder. + $csspath = 'public://css'; + $uri = $csspath . '/' . $filename; + // Create the CSS file. + file_prepare_directory($csspath, FILE_CREATE_DIRECTORY); + if (!file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) { + lock_release($lock_name); + return FALSE; + } + // Update the map atomically. + _drupal_update_css_js_map('css', $key, $uri); + lock_release($lock_name); + } + else { + if ($mtime) { + break; + } + if ($lock_acquire_attempts == 0) { + $prev_key = hash('sha256', ($version - 1) . serialize($css)); + if (isset($map[$prev_key]) && filemtime($map[$prev_key])) { + return $map[$prev_key]; + } + } + lock_wait($lock_name); } + $mtime = $uri ? filemtime($uri) : 0; } - // Save the updated map. - $map[$key] = $uri; - variable_set('drupal_css_cache_files', $map); } + while (!$mtime && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5); return $uri; } /** + * Helper function to atomically set the css and js cache maps. + */ +function _drupal_update_css_js_map($type, $key, $uri) { + $lock_name = $type . '_cache'; + $var_name = 'drupal_' . $type . '_cache_files'; + $lock_acquire_attempts = 0; + while (!lock_acquire($lock_name) && ++$lock_acquire_attempts < 5) { + lock_wait($lock_name); + } + $vars = variable_initialize(array()); + $map = $vars[$var_name]; + $map[$key] = $uri; + variable_set($var_name, $map); + lock_release($lock_name); +} + +/** * Helper function for drupal_build_css_cache(). * * This function will prefix all paths within a CSS file. @@ -4637,42 +4668,67 @@ function drupal_build_js_cache($files) { $contents = ''; $uri = ''; $map = variable_get('drupal_js_cache_files', array()); - $key = hash('sha256', serialize($files)); + $version = variable_get('drupal_js_cache_version', 1); + $key = hash('sha256', $version . serialize($files)); if (isset($map[$key])) { $uri = $map[$key]; } - if (empty($uri) || !file_exists($uri)) { - // Build aggregate JS file. - foreach ($files as $path => $info) { - if ($info['preprocess']) { - // Append a ';' and a newline after each JS file to prevent them from running together. - $contents .= file_get_contents($path) . ";\n"; + $lock_acquire_attempts = 0; + $lock_name = 'js_cache:' . $uri; + $mtime = $uri ? filemtime($uri) : 0; + do { + if (empty($uri) || !$mtime || (isset($map['clear']) && $mtime < $map['clear'])) { + if ($lock_acquired = lock_acquire($lock_name)) { + // Build aggregate JS file. + foreach ($files as $path => $info) { + if ($info['preprocess']) { + // Append a ';' and a newline after each JS file to prevent them from running together. + $contents .= file_get_contents($path) . ";\n"; + } + } + // Prefix filename to prevent blocking by firewalls which reject files + // starting with "ad*". + $filename = 'js_' . drupal_hash_base64($contents) . '.js'; + // Create the js/ within the files folder. + $jspath = 'public://js'; + $uri = $jspath . '/' . $filename; + // Create the JS file. + file_prepare_directory($jspath, FILE_CREATE_DIRECTORY); + if (!file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) { + lock_release($lock_name); + return FALSE; + } + // If JS gzip compression is enabled, clean URLs are enabled (which means + // that rewrite rules are working) and the zlib extension is available then + // create a gzipped version of this file. This file is served conditionally + // to browsers that accept gzip using .htaccess rules. + if (variable_get('js_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) { + if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($contents, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { + lock_release($lock_name); + return FALSE; + } + } + // Update the map atomically. + _drupal_update_css_js_map('js', $key, $uri); + lock_release($lock_name); } - } - // Prefix filename to prevent blocking by firewalls which reject files - // starting with "ad*". - $filename = 'js_' . drupal_hash_base64($contents) . '.js'; - // Create the js/ within the files folder. - $jspath = 'public://js'; - $uri = $jspath . '/' . $filename; - // Create the JS file. - file_prepare_directory($jspath, FILE_CREATE_DIRECTORY); - if (!file_exists($uri) && !file_unmanaged_save_data($contents, $uri, FILE_EXISTS_REPLACE)) { - return FALSE; - } - // If JS gzip compression is enabled, clean URLs are enabled (which means - // that rewrite rules are working) and the zlib extension is available then - // create a gzipped version of this file. This file is served conditionally - // to browsers that accept gzip using .htaccess rules. - if (variable_get('js_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib')) { - if (!file_exists($uri . '.gz') && !file_unmanaged_save_data(gzencode($contents, 9, FORCE_GZIP), $uri . '.gz', FILE_EXISTS_REPLACE)) { - return FALSE; + else { + if ($mtime) { + break; + } + if ($lock_acquire_attempts == 0) { + $prev_key = hash('sha256', ($version - 1) . serialize($css)); + if (isset($map[$prev_key]) && filemtime($map[$prev_key])) { + return $map[$prev_key]; + } + } + lock_wait($lock_name); } + $mtime = $uri ? filemtime($uri) : 0; } - $map[$key] = $uri; - variable_set('drupal_js_cache_files', $map); } + while (!$mtime && $lock_acquired === FALSE && ++$lock_acquire_attempts < 5); return $uri; }