Expose vips internal cache settings and status

This commit is contained in:
Lovell Fuller 2014-02-25 23:31:33 +00:00
parent ae9a8b0f57
commit d5d85a8697
6 changed files with 44 additions and 3 deletions

View File

@ -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. `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 ```javascript
sharp.resize("input.jpg", "output.jpg", 300, 200, function(err) { 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 ## Testing
npm test npm test

View File

@ -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); 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 */ /* Deprecated v0.0.x methods */
module.exports.crop = function(input, output, width, height, sharpen, callback) { module.exports.crop = function(input, output, width, height, sharpen, callback) {
sharp.resize(input, output, width, height, {canvas: "c", sharpen: true}, callback); sharp.resize(input, output, width, height, {canvas: "c", sharpen: true}, callback);

View File

@ -1,6 +1,6 @@
{ {
"name": "sharp", "name": "sharp",
"version": "0.1.6", "version": "0.1.7",
"author": "Lovell Fuller", "author": "Lovell Fuller",
"description": "High performance module to resize JPEG, PNG, WebP and TIFF images using the libvips image processing library", "description": "High performance module to resize JPEG, PNG, WebP and TIFF images using the libvips image processing library",
"scripts": { "scripts": {

View File

@ -323,6 +323,22 @@ Handle<Value> resize(const Arguments& args) {
return scope.Close(Undefined()); return scope.Close(Undefined());
} }
Handle<Value> 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<Object> 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) { static void at_exit(void* arg) {
HandleScope scope; HandleScope scope;
vips_shutdown(); vips_shutdown();
@ -333,6 +349,7 @@ extern "C" void init(Handle<Object> target) {
vips_init(""); vips_init("");
AtExit(at_exit); AtExit(at_exit);
NODE_SET_METHOD(target, "resize", resize); NODE_SET_METHOD(target, "resize", resize);
NODE_SET_METHOD(target, "cache", cache);
} }
NODE_MODULE(sharp, init) NODE_MODULE(sharp, init)

View File

@ -26,4 +26,6 @@ async.mapSeries([1, 1, 2, 4, 8, 16, 32, 64, 128], function(parallelism, next) {
next(); next();
} }
); );
}, function() {}); }, function() {
console.dir(sharp.cache());
});

View File

@ -420,4 +420,5 @@ async.series({
Object.keys(results).forEach(function(format) { Object.keys(results).forEach(function(format) {
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format); assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
}); });
console.dir(sharp.cache());
}); });