Skip to content
Browse files

added tmp folder caching

  • Loading branch information...
1 parent 9bfd9c6 commit d6cf43e5cc2f85438de644885218ac57b7ffaced @inadarei committed Jan 13, 2013
Showing with 37 additions and 7 deletions.
  1. +11 −7 README.md
  2. +25 −0 lib/connect-thumbs.js
  3. +1 −0 package.json
View
18 README.md
@@ -66,20 +66,23 @@ and must call callback, upon completion, with following syntax:
# Performance and Scalability
-Node.js is very fast, but Imagemagick most certainly is not (and neither may be your custom decodeFn function if it is doing a database
-lookup for every request), so in any production set up it is highly recommended to put thubmnailed URLs behind some sort of
-proxy/cache. Good options may be:
-
-- Varnish
-- A good CDN such as Amazon's CloudFront
+Node.js is very fast, but Imagemagick most certainly is not (and neither may be your custom decodeFn function if it
+is doing a database lookup for every request), so in any production set up it is highly recommended to put thubmnailing
+of images behind some sort of proxy/cache. Viable options may be:
+
+- Enable the disk-based cache provided by the Connect-Thumbs itself. You can do this by passing `tmpCacheTTL`
+configuration variable when initializing Thumbs. This variable is set in seconds and is 0 (i.e. cache disabled) by default.
+- Put Varnish in front of the thumbnail URLs
+- Use a robust CDN such as Amazon's CloudFront
- Pick your own poison.
# Customization
```
app.use(thumbs({
- "ttl": "92000"
+ "ttl": 92000
+ , "tmpCacheTTL": 86400
, "tmpDir": "/tmp/mynodethumbnails"
, "decodeFn": someModule.loadImageUrlFromDbById
, "allowedExtensions": ['png', 'jpg']
@@ -103,6 +106,7 @@ proxy/cache. Good options may be:
where:
* ttl - is the cache duration that will be returned in the HTTP headers for the resulting thumbnail
+ * tmpCacheTTL - time (in seconds) to cache thumbnails in temp folder. Defaults to 0 (cache disabled).
* tmpDir - is the Node-writable temp folder where file operations will be performed. Defaults to: `/tmp/nodethumbnails`.
You may want to periodically clean-up that folder.
* decodeFn - custom decoder function. Defaults to one that decodes base64-encoded full URLs.
View
25 lib/connect-thumbs.js
@@ -6,12 +6,14 @@ if (!module.parent) { console.log("Please don't call me directly. I am just the
var options = {}
, pauser
, ttl
+ , tmpCacheTTL
, tmpDir
, presets
, decodeFn
, regexp = '';
var mkdirp = require('mkdirp')
+ , moment = require('moment')
, async = require('async')
, request = require('request')
, im = require('imagemagick')
@@ -74,6 +76,28 @@ exports = module.exports = function thumbs(opts) {
filepath = targetDir + '/' + hashedName + ext;
modifiedFilePath = targetDir + '/' + hashedName + "-" + imagePreset + ext;
+ // see if we can serve the file from file cahce, if ttl has not yet expired
+ if (tmpCacheTTL > 0) {
+ try {
+ var stats = fs.statSync(filepath);
+ var fileUnix = moment(stats.mtime).unix();
+ var nowUnix = moment().unix();
+ var diffUnix = nowUnix - fileUnix;
+
+ if (diffUnix < tmpCacheTTL) { // file is fresh, no need to download/resize etc.
+ send(req, modifiedFilePath)
+ .maxage(ttl || 0)
+ .pipe(res);
+
+ return resume(false);
+ }
+ } catch (err) {
+ // no action necessary, just continue with normal flow
+ }
+ }
+
+
+
fileStream = fs.createWriteStream(filepath);
request.get(decodedImageURL).pipe(fileStream);
@@ -128,6 +152,7 @@ var hash = function(str, algo, encoding) {
var parseOptions = function (options) {
ttl = options.ttl || (3600 * 24); // cache for 1 day by default.
+ tmpCacheTTL = options.tmpCacheTTL || 0; // disabled by default
decodeFn = options.decodeFn || exports.decodeURL;
presets = options.presets || defaultPresets();
View
1 package.json
@@ -24,6 +24,7 @@
, "send" : "0.x"
, "mkdirp" : "0.x"
, "imagemagick" : "0.x"
+ , "moment" : "1.7.x"
}
, "devDependencies": {"connect": "*"},
"optionalDependencies": {}

0 comments on commit d6cf43e

Please sign in to comment.
Something went wrong with that request. Please try again.