mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Expose vips internal cache settings and status
This commit is contained in:
parent
ae9a8b0f57
commit
d5d85a8697
16
README.md
16
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
|
||||
|
7
index.js
7
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);
|
||||
|
@ -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": {
|
||||
|
17
src/sharp.cc
17
src/sharp.cc
@ -323,6 +323,22 @@ Handle<Value> resize(const Arguments& args) {
|
||||
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) {
|
||||
HandleScope scope;
|
||||
vips_shutdown();
|
||||
@ -333,6 +349,7 @@ extern "C" void init(Handle<Object> target) {
|
||||
vips_init("");
|
||||
AtExit(at_exit);
|
||||
NODE_SET_METHOD(target, "resize", resize);
|
||||
NODE_SET_METHOD(target, "cache", cache);
|
||||
}
|
||||
|
||||
NODE_MODULE(sharp, init)
|
||||
|
@ -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());
|
||||
});
|
||||
|
@ -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());
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user