Created
June 14, 2014 20:48
-
-
Save matutter/a23fc7f6e03e9d088c38 to your computer and use it in GitHub Desktop.
Created a node FS pre-cacher, my node was already optimized... 1 tick slower
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
| /* | |
| var images = new pre.cache_request('./resources/images/','*', true, function(cache) { | |
| handler.locals.cache = cache | |
| }) | |
| pre.directoryCache( [images] ) | |
| if( local.cache[pathto+file] != undefined ) { | |
| res.writeHead(200, {'content-type':mime}) | |
| res.end( local.cache[ pathto+file ] ) | |
| console.log( 'cache hit' ) | |
| } | |
| else ... regular | |
| */ | |
| // pre-cache | |
| var fs = require('fs') | |
| function fileCache() { | |
| }; | |
| // will ignore the file parameter | |
| function directoryCache( requests ) { | |
| for( var each in requests ) { | |
| if( requests[each].log ) | |
| console.log( 'caching ' + requests[each].dir ) | |
| var cached = {} | |
| fs.readdir(requests[each].dir, function(err, files) { | |
| if(err) throw err; | |
| files.forEach(function(file){ | |
| fs.readFile( requests[each].dir + file, function(err, blob) { | |
| if( err ) { | |
| if( requests[each].log ) | |
| console.log( '- '+file ) | |
| } | |
| else { | |
| cached[ requests[each].dir+file ] = blob | |
| if( requests[each].log ) | |
| console.log( '+ '+file ) | |
| } | |
| }) // end readFile | |
| }) // end inner loop | |
| }) // end readdir | |
| requests[each].cb( cached ) | |
| } // end loop | |
| }; | |
| function cache_request(ndir, nf, nlog, ncb) { | |
| this.dir = ndir; | |
| this.file = nf; | |
| this.log = nlog; | |
| this.cb = ncb; | |
| }; | |
| exports.directoryCache = directoryCache | |
| exports.cache_request = cache_request | |
| exports.fileCache = fileCache | |
| /* // let up all caching here | |
| handler.locals.init = function() { | |
| handler.jade.renderFile( handler.locals.viewDir + 'notfound.jade', null, function( err, page ) { | |
| console.log( err ) | |
| if(err) | |
| handler.locals.errPage = 'set up an error page' | |
| else | |
| handler.locals.errPage = page | |
| }) | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment