@@ -7,6 +7,12 @@ const promisify = require('promisify-es6')
7
7
const bs58 = require ( 'bs58' )
8
8
const bl = require ( 'bl' )
9
9
const cleanMultihash = require ( '../clean-multihash' )
10
+ const LRU = require ( 'lru-cache' )
11
+ const lruOptions = {
12
+ max : 128
13
+ }
14
+
15
+ const cache = LRU ( lruOptions )
10
16
11
17
module . exports = ( send ) => {
12
18
const api = {
@@ -15,6 +21,7 @@ module.exports = (send) => {
15
21
callback = options
16
22
options = { }
17
23
}
24
+
18
25
if ( ! options ) {
19
26
options = { }
20
27
}
@@ -25,6 +32,12 @@ module.exports = (send) => {
25
32
return callback ( err )
26
33
}
27
34
35
+ const node = cache . get ( multihash )
36
+
37
+ if ( node ) {
38
+ return callback ( null , node )
39
+ }
40
+
28
41
send ( {
29
42
path : 'object/get' ,
30
43
args : multihash
@@ -38,9 +51,12 @@ module.exports = (send) => {
38
51
return new DAGLink ( l . Name , l . Size , new Buffer ( bs58 . decode ( l . Hash ) ) )
39
52
} ) )
40
53
54
+ cache . set ( multihash , node )
55
+
41
56
callback ( null , node )
42
57
} )
43
58
} ) ,
59
+
44
60
put : promisify ( ( obj , options , callback ) => {
45
61
if ( typeof options === 'function' ) {
46
62
callback = options
@@ -122,6 +138,8 @@ module.exports = (send) => {
122
138
return callback ( new Error ( 'Stored object was different from constructed object' ) )
123
139
}
124
140
141
+ cache . set ( result . Hash , node )
142
+
125
143
callback ( null , node )
126
144
} )
127
145
}
@@ -142,6 +160,12 @@ module.exports = (send) => {
142
160
return callback ( err )
143
161
}
144
162
163
+ const node = cache . get ( multihash )
164
+
165
+ if ( node ) {
166
+ return callback ( null , node . data )
167
+ }
168
+
145
169
send ( {
146
170
path : 'object/data' ,
147
171
args : multihash
@@ -172,6 +196,12 @@ module.exports = (send) => {
172
196
return callback ( err )
173
197
}
174
198
199
+ const node = cache . get ( multihash )
200
+
201
+ if ( node ) {
202
+ return callback ( null , node . links )
203
+ }
204
+
175
205
send ( {
176
206
path : 'object/links' ,
177
207
args : multihash
0 commit comments