@@ -58,12 +58,7 @@ func GetObjectFromIPFS(ctx commands.Context, p peer.ID, name string, maxCacheLen
58
58
getObjectFromIPFSCacheMu .Lock ()
59
59
defer getObjectFromIPFSCacheMu .Unlock ()
60
60
61
- entry , ok := getObjectFromIPFSCache [getIPFSCacheKey (p , name )]
62
- if ! ok {
63
- return fetchObjectFromIPFS (ctx , p , name )
64
- }
65
-
66
- if entry .created .Add (maxCacheLen ).Before (time .Now ()) {
61
+ fetchAndUpdateCache := func () ([]byte , error ) {
67
62
objBytes , err := fetchObjectFromIPFS (ctx , p , name )
68
63
if err != nil {
69
64
return nil , err
@@ -77,19 +72,16 @@ func GetObjectFromIPFS(ctx commands.Context, p peer.ID, name string, maxCacheLen
77
72
return objBytes , nil
78
73
}
79
74
75
+ entry , ok := getObjectFromIPFSCache [getIPFSCacheKey (p , name )]
76
+ if ! ok || entry .created .Add (maxCacheLen ).Before (time .Now ()) {
77
+ return fetchAndUpdateCache ()
78
+ }
79
+
80
80
// Update cache in background after a successful read
81
- func () {
81
+ go func () {
82
82
getObjectFromIPFSCacheMu .Lock ()
83
83
defer getObjectFromIPFSCacheMu .Unlock ()
84
- objBytes , err := fetchObjectFromIPFS (ctx , p , name )
85
- if err != nil {
86
- log .Error ("error update inventory cache:" , err )
87
- }
88
-
89
- getObjectFromIPFSCache [getIPFSCacheKey (p , name )] = getObjectFromIPFSCacheEntry {
90
- bytes : objBytes ,
91
- created : time .Now (),
92
- }
84
+ fetchAndUpdateCache ()
93
85
}()
94
86
95
87
return entry .bytes , nil
@@ -101,6 +93,7 @@ func fetchObjectFromIPFS(ctx commands.Context, p peer.ID, name string) ([]byte,
101
93
if err != nil {
102
94
return nil , err
103
95
}
96
+
104
97
bytes , err := ipfs .Cat (ctx , root , time .Minute )
105
98
if err != nil {
106
99
return nil , err
0 commit comments