Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 119361f

Browse files
committed
feat(object): add lru cache to object get/put
1 parent 4e7b8e4 commit 119361f

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"ipfs-merkle-dag": "^0.7.1",
2929
"is-ipfs": "^0.2.0",
3030
"isstream": "^0.1.2",
31+
"lru-cache": "^4.0.1",
3132
"multiaddr": "^2.0.2",
3233
"multipart-stream": "^2.0.1",
3334
"ndjson": "^1.4.3",
@@ -103,4 +104,4 @@
103104
"url": "https://github.com/ipfs/js-ipfs-api/issues"
104105
},
105106
"homepage": "https://github.com/ipfs/js-ipfs-api"
106-
}
107+
}

src/api/object.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ const promisify = require('promisify-es6')
66
const bs58 = require('bs58')
77
const bl = require('bl')
88
const cleanMultihash = require('../clean-multihash')
9+
const LRU = require('lru-cache')
10+
const lruOptions = {
11+
max: 128
12+
}
13+
14+
const cache = LRU(lruOptions)
915

1016
module.exports = (send) => {
1117
const api = {
@@ -14,6 +20,7 @@ module.exports = (send) => {
1420
callback = options
1521
options = {}
1622
}
23+
1724
if (!options) {
1825
options = {}
1926
}
@@ -24,6 +31,12 @@ module.exports = (send) => {
2431
return callback(err)
2532
}
2633

34+
const node = cache.get(multihash)
35+
36+
if (node) {
37+
return callback(null, node)
38+
}
39+
2740
send({
2841
path: 'object/get',
2942
args: multihash
@@ -37,9 +50,12 @@ module.exports = (send) => {
3750
return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash)))
3851
}))
3952

53+
cache.set(multihash, node)
54+
4055
callback(null, node)
4156
})
4257
}),
58+
4359
put: promisify((obj, options, callback) => {
4460
if (typeof options === 'function') {
4561
callback = options
@@ -107,6 +123,8 @@ module.exports = (send) => {
107123
return callback(new Error('Stored object was different from constructed object'))
108124
}
109125

126+
cache.set(result.Hash, node)
127+
110128
callback(null, node)
111129
})
112130
}),

0 commit comments

Comments
 (0)