Skip to content

Instantly share code, notes, and snippets.

@rxaviers
Last active December 12, 2015 01:48
Show Gist options
  • Select an option

  • Save rxaviers/4693261 to your computer and use it in GitHub Desktop.

Select an option

Save rxaviers/4693261 to your computer and use it in GitHub Desktop.
diff --git a/lib/themeroller.image.js b/lib/themeroller.image.js
index 2acf1df..f7aa855 100644
--- a/lib/themeroller.image.js
+++ b/lib/themeroller.image.js
@@ -51,6 +51,7 @@ function stream2Buffer( callback ) {
var generateIcon, generateImage, generateTexture,
cacheDirectory = __dirname + "/../tmp/cache",
+ cachingLock = {},
concurrentQueues = 4,
imageQueue = async.queue( function( task, callback ) {
task( callback );
@@ -210,9 +211,13 @@ Image.prototype = {
fs.readFile( cacheFile, function( err, data ) {
if ( err ) {
generateImage( params, function( err, data ) {
- if ( !err ) {
+ if ( !err && !cachingLock[ cacheFile ] ) {
// Write file asynchronously while sending data to callback.
- fs.writeFile( cacheFile, data );
+ cachingLock[ cacheFile ] = true;
+ fs.writeFile( cacheFile + ".part", data, function() {
+ fs.rename( cacheFile + ".part", cacheFile );
+ delete cachingLock[ cacheFile ];
+ });
}
callback( err, filename, data );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment