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

Commit 0fefbd1

Browse files
authored
Merge pull request #386 from ipfs/feat/lru
feat(object): add lru cache to object get/put
2 parents f46cdc5 + c439f39 commit 0fefbd1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"is-ipfs": "^0.2.1",
3535
"isstream": "^0.1.2",
3636
"multiaddr": "^2.0.3",
37+
"lru-cache": "^4.0.1",
3738
"multipart-stream": "^2.0.1",
3839
"ndjson": "^1.4.3",
3940
"once": "^1.4.0",

src/api/object.js

+30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const promisify = require('promisify-es6')
77
const bs58 = require('bs58')
88
const bl = require('bl')
99
const cleanMultihash = require('../clean-multihash')
10+
const LRU = require('lru-cache')
11+
const lruOptions = {
12+
max: 128
13+
}
14+
15+
const cache = LRU(lruOptions)
1016

1117
module.exports = (send) => {
1218
const api = {
@@ -15,6 +21,7 @@ module.exports = (send) => {
1521
callback = options
1622
options = {}
1723
}
24+
1825
if (!options) {
1926
options = {}
2027
}
@@ -25,6 +32,12 @@ module.exports = (send) => {
2532
return callback(err)
2633
}
2734

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

54+
cache.set(multihash, node)
55+
4156
callback(null, node)
4257
})
4358
}),
59+
4460
put: promisify((obj, options, callback) => {
4561
if (typeof options === 'function') {
4662
callback = options
@@ -122,6 +138,8 @@ module.exports = (send) => {
122138
return callback(new Error('Stored object was different from constructed object'))
123139
}
124140

141+
cache.set(result.Hash, node)
142+
125143
callback(null, node)
126144
})
127145
}
@@ -142,6 +160,12 @@ module.exports = (send) => {
142160
return callback(err)
143161
}
144162

163+
const node = cache.get(multihash)
164+
165+
if (node) {
166+
return callback(null, node.data)
167+
}
168+
145169
send({
146170
path: 'object/data',
147171
args: multihash
@@ -172,6 +196,12 @@ module.exports = (send) => {
172196
return callback(err)
173197
}
174198

199+
const node = cache.get(multihash)
200+
201+
if (node) {
202+
return callback(null, node.links)
203+
}
204+
175205
send({
176206
path: 'object/links',
177207
args: multihash

0 commit comments

Comments
 (0)