diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-27 17:36:13 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-27 17:36:13 +0200 |
commit | 5f466137ad6ac596600e3ff53c9b786815398445 (patch) | |
tree | f914c221874f0b16bf3def7ac01d59d1a99a3b0b /node_modules/lru-cache/README.md | |
parent | c9f5ac8e763eda19aa0564179300cfff76785435 (diff) |
node_modules, clean up package.json
Diffstat (limited to 'node_modules/lru-cache/README.md')
-rw-r--r-- | node_modules/lru-cache/README.md | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/node_modules/lru-cache/README.md b/node_modules/lru-cache/README.md index c06814e04..5bdf98c0f 100644 --- a/node_modules/lru-cache/README.md +++ b/node_modules/lru-cache/README.md @@ -2,12 +2,20 @@ A cache object that deletes the least-recently-used items. +[](https://travis-ci.org/isaacs/node-lru-cache) [](https://coveralls.io/github/isaacs/node-lru-cache) + +## Installation: + +```javascript +npm install lru-cache --save +``` + ## Usage: ```javascript var LRU = require("lru-cache") , options = { max: 500 - , length: function (n) { return n * 2 } + , length: function (n, key) { return n * 2 + key.length } , dispose: function (key, n) { n.close() } , maxAge: 1000 * 60 * 60 } , cache = LRU(options) @@ -16,6 +24,12 @@ var LRU = require("lru-cache") cache.set("key", "value") cache.get("key") // "value" +// non-string keys ARE fully supported +var someObject = {} +cache.set(someObject, 'a value') +cache.set('[object Object]', 'a different value') +assert.equal(cache.get(someObject), 'a value') + cache.reset() // empty the cache ``` @@ -24,24 +38,6 @@ If you put more stuff in it, then items will fall out. If you try to put an oversized thing in it, then it'll fall out right away. -## Keys should always be Strings or Numbers - -Note: this module will print warnings to `console.error` if you use a -key that is not a String or Number. Because items are stored in an -object, which coerces keys to a string, it won't go well for you if -you try to use a key that is not a unique string, it'll cause surprise -collisions. For example: - -```JavaScript -// Bad Example! Dont' do this! -var cache = LRU() -var a = {} -var b = {} -cache.set(a, 'this is a') -cache.set(b, 'this is b') -console.log(cache.get(a)) // prints: 'this is b' -``` - ## Options * `max` The maximum size of the cache, checked by applying the length @@ -53,9 +49,10 @@ console.log(cache.get(a)) // prints: 'this is b' drop it and return undefined instead of giving it to you. * `length` Function that is used to calculate the length of stored items. If you're storing strings or buffers, then you probably want - to do something like `function(n){return n.length}`. The default is - `function(n){return 1}`, which is fine if you want to store `max` - like-sized things. + to do something like `function(n, key){return n.length}`. The default is + `function(){return 1}`, which is fine if you want to store `max` + like-sized things. The item is passed as the first argument, and + the key is passed as the second argumnet. * `dispose` Function that is called on items when they are dropped from the cache. This can be handy if you want to close file descriptors or do other cleanup tasks when items are no longer @@ -76,8 +73,12 @@ console.log(cache.get(a)) // prints: 'this is b' * `get(key) => value` Both of these will update the "recently used"-ness of the key. - They do what you think. `max` is optional and overrides the - cache `max` option if provided. + They do what you think. `maxAge` is optional and overrides the + cache `maxAge` option if provided. + + If the key is not found, `get()` will return `undefined`. + + The key and val can be any value. * `peek(key)` @@ -107,6 +108,12 @@ console.log(cache.get(a)) // prints: 'this is b' in the cache, in order of recent-ness. (Ie, more recently used items are iterated over first.) +* `rforEach(function(value,key,cache), [thisp])` + + The same as `cache.forEach(...)` but items are iterated over in + reverse order. (ie, less recently used items are iterated over + first.) + * `keys()` Return an array of the keys in the cache. @@ -115,7 +122,7 @@ console.log(cache.get(a)) // prints: 'this is b' Return an array of the values in the cache. -* `length()` +* `length` Return total length of objects in cache taking into account `length` options function. @@ -135,3 +142,7 @@ console.log(cache.get(a)) // prints: 'this is b' Loads another cache entries array, obtained with `sourceCache.dump()`, into the cache. The destination cache is reset before loading new entries + +* `prune()` + + Manually iterates over the entire cache proactively pruning old entries |