safe-memory-cache
Advanced tools
+66
| function createMem(number, limit) { | ||
| var mem = Object.create(bucketsProto) | ||
| mem.N = number | ||
| mem.max = limit | ||
| return mem | ||
| } | ||
| var bucketsProto = { | ||
| clear: function clear() { | ||
| this.size = 0 | ||
| for (var i = 0; i < this.N; i++) { | ||
| this.spawnBucket() | ||
| } | ||
| }, | ||
| spawnBucket: function spawnBucket() { | ||
| this.buckets.unshift(new Map()) | ||
| }, | ||
| rotateBuckets: function rotateBuckets() { | ||
| this.buckets.pop() | ||
| this.spawnBucket() | ||
| this.size = 0 | ||
| }, | ||
| set: function set(key, value) { | ||
| this.buckets[0][key] = value | ||
| if (!(key in this.buckets[0])) { | ||
| this.size++; | ||
| if (this.max && size >= ~~(this.max / this.buckets.length)) { | ||
| this.rotateBuckets() | ||
| } | ||
| } | ||
| }, | ||
| get: function get(key) { | ||
| for (var i = 0; i < this.buckets.length; i++) { | ||
| if (key in this.buckets[i]) { | ||
| //todo: this should be configurable | ||
| if (i) { | ||
| //put a reference in the newest bucket | ||
| this.set(key,this.buckets[i][key]) | ||
| } | ||
| return this.buckets[i][key] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| module.exports = function(opts) { | ||
| var buckets = ~~(opts.buckets) || 2; | ||
| var mem = createMem(buckets, ~~(opts.limit)) | ||
| if (opts.maxTTL) { | ||
| var intervalHandle = setInterval(mem.rotateBuckets.bind(mem), ~~(opts.maxTTL / buckets)) | ||
| } | ||
| return { | ||
| set: mem.set.bind(mem), | ||
| get: mem.get.bind(mem), | ||
| clear: mem.clear.bind(mem), | ||
| destroy: function() { | ||
| clearInterval(intervalHandle) | ||
| } | ||
| } | ||
| } |
+1
-1
| { | ||
| "name": "safe-memory-cache", | ||
| "version": "1.0.0-rc.1", | ||
| "version": "1.1.0-0", | ||
| "description": "Secure and size-limited in-memory cache for Node.js and browsers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+9
-3
@@ -27,2 +27,8 @@ # safe-memory-cache | ||
| If your engine supports `Map`, you can use the map based version. It doesn't need (nor have) sanitization on keys and it uses Maps as buckets for storage. | ||
| ``` | ||
| const safeMemoryCache = require('safe-memory-cache/map') | ||
| ``` | ||
| ### options: | ||
@@ -34,3 +40,3 @@ | ||
| maxTTL | number | N | Time in miliseconds within which an element should no longer be in cache if it was not accessed. Actual time is approximate and will be less or equal `maxTTL` | ||
| strongSanitizer | bool | N | When set to `true` sanitizes keys very carefully. Defaults to `false` - simpler sanitization<2..10> | ||
| strongSanitizer | bool | N | When set to `true` sanitizes keys to prevent memory issues in older JS engines. Defaults to `false`. No sanitization if you use the Map based version. | ||
| buckets | number | N | Overrides the number of buckets used internally. Default is 2 | ||
@@ -46,3 +52,3 @@ | ||
| Caching in general. When you need to cache results of some long running function and you don't have a strong requirement to forget every item after its exact expiry time, no sooner. | ||
| Caching in general. When you need to cache results of some long running process or a lot of them and you **don't** have a strong requirement to keep every item until its exact expiry time. | ||
@@ -66,3 +72,3 @@ ## Technicalities | ||
| `delete` keyword removes fields from objects, but doesn't release all the memory used by the key entry. As a result, adding and deleting unique fields to a plain JavaScript object will cause memory consumption to grow. Some JavaScript engines had it also leak memory. | ||
| `delete` keyword removes fields from objects, but changes the hidden class of the object which takes up some memory. As a result, adding and deleting unique fields to a plain JavaScript object may cause memory consumption to grow. Some JavaScript engines had it leak memory in various ways. | ||
@@ -69,0 +75,0 @@ *Then how do you remove old items from cache if you can't use delete?* |
8821
28.75%5
25%130
80.56%74
8.82%