diff --git a/README.md b/README.md index 25877c3e..3e605bda 100755 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Scale and crop to `width` x `height` calling `callback` when complete. `callback` gets two arguments `(err, buffer)` where `err` is an error message, if any, and `buffer` is the resultant image data when a Buffer is requested. -### Examples +#### Examples ```javascript sharp.resize("input.jpg", "output.jpg", 300, 200, function(err) { @@ -101,6 +101,20 @@ sharp.resize("input.jpg", sharp.buffer.webp, 200, 300, {canvas: sharp.canvas.emb }); ``` +### cache([limit]) + +If `limit` is provided, set the `vips` internal cache limit to this value in MB. The default value is 100. + +Always returns cache statistics, namely current usage, high water mark and maximum limit. + +The high water mark may be higher than the maximum limit. + +```javascript +var stats = sharp.cache(); // { current: 98, high: 115, limit: 100 } +sharp.cache(200); // { current: 98, high: 115, limit: 200 } +sharp.cache(50); // { current: 49, high: 115, limit: 50 } +``` + ## Testing npm test diff --git a/index.js b/index.js index b68da114..c69c5745 100755 --- a/index.js +++ b/index.js @@ -53,6 +53,13 @@ module.exports.resize = function(input, output, width, height, options, callback sharp.resize(options.inFile, options.inBuffer, output, width, height, canvas, sharpen, progessive, sequentialRead, callback); }; +module.exports.cache = function(limit) { + if (Number.isNaN(limit)) { + limit = null; + } + return sharp.cache(limit); +} + /* Deprecated v0.0.x methods */ module.exports.crop = function(input, output, width, height, sharpen, callback) { sharp.resize(input, output, width, height, {canvas: "c", sharpen: true}, callback); diff --git a/package.json b/package.json index 552665b6..dccf9177 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sharp", - "version": "0.1.6", + "version": "0.1.7", "author": "Lovell Fuller", "description": "High performance module to resize JPEG, PNG, WebP and TIFF images using the libvips image processing library", "scripts": { diff --git a/src/sharp.cc b/src/sharp.cc index 8398aa2c..f79701d2 100755 --- a/src/sharp.cc +++ b/src/sharp.cc @@ -323,6 +323,22 @@ Handle resize(const Arguments& args) { return scope.Close(Undefined()); } +Handle cache(const Arguments& args) { + HandleScope scope; + + // Set cache limit + if (args[0]->IsInt32()) { + vips_cache_set_max_mem(args[0]->Int32Value() * 1048576); + } + + // Get cache statistics + Local cache = Object::New(); + cache->Set(String::NewSymbol("current"), Number::New(vips_tracked_get_mem() / 1048576)); + cache->Set(String::NewSymbol("high"), Number::New(vips_tracked_get_mem_highwater() / 1048576)); + cache->Set(String::NewSymbol("limit"), Number::New(vips_cache_get_max_mem() / 1048576)); + return scope.Close(cache); +} + static void at_exit(void* arg) { HandleScope scope; vips_shutdown(); @@ -333,6 +349,7 @@ extern "C" void init(Handle target) { vips_init(""); AtExit(at_exit); NODE_SET_METHOD(target, "resize", resize); + NODE_SET_METHOD(target, "cache", cache); } NODE_MODULE(sharp, init) diff --git a/tests/parallel.js b/tests/parallel.js index 5b3d4acd..2f3246bb 100755 --- a/tests/parallel.js +++ b/tests/parallel.js @@ -26,4 +26,6 @@ async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) { next(); } ); -}, function() {}); +}, function() { + console.dir(sharp.cache()); +}); diff --git a/tests/perf.js b/tests/perf.js index 414ddbd1..5117175b 100755 --- a/tests/perf.js +++ b/tests/perf.js @@ -420,4 +420,5 @@ async.series({ Object.keys(results).forEach(function(format) { assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format); }); + console.dir(sharp.cache()); });