Last active
December 12, 2015 01:48
-
-
Save rxaviers/4693261 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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