@@ -7,6 +7,12 @@ const promisify = require('promisify-es6')
77const bs58 = require ( 'bs58' )
88const bl = require ( 'bl' )
99const cleanMultihash = require ( '../clean-multihash' )
10+ const LRU = require ( 'lru-cache' )
11+ const lruOptions = {
12+ max : 128
13+ }
14+
15+ const cache = LRU ( lruOptions )
1016
1117module . 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