@@ -68,7 +68,16 @@ impl From<ModuleInfoCacheSourceHash> for String {
68
68
/// deno_graph.
69
69
pub struct ModuleInfoCache {
70
70
conn : CacheDB ,
71
- pending_inserts : Mutex < Vec < ( String , & ' static str , String , String ) > > ,
71
+ sender : Mutex <
72
+ Option <
73
+ tokio:: sync:: mpsc:: UnboundedSender < (
74
+ String ,
75
+ & ' static str ,
76
+ String ,
77
+ String ,
78
+ ) > ,
79
+ > ,
80
+ > ,
72
81
}
73
82
74
83
impl ModuleInfoCache {
@@ -80,7 +89,7 @@ impl ModuleInfoCache {
80
89
pub fn new ( conn : CacheDB ) -> Self {
81
90
Self {
82
91
conn,
83
- pending_inserts : Mutex :: new ( Vec :: new ( ) ) ,
92
+ sender : Default :: default ( ) ,
84
93
}
85
94
}
86
95
@@ -89,7 +98,7 @@ impl ModuleInfoCache {
89
98
pub ( crate ) fn recreate_with_version ( self , version : & ' static str ) -> Self {
90
99
Self {
91
100
conn : self . conn . recreate_with_version ( version) ,
92
- pending_inserts : Mutex :: new ( Vec :: new ( ) ) ,
101
+ sender : Default :: default ( ) ,
93
102
}
94
103
}
95
104
@@ -140,50 +149,47 @@ impl ModuleInfoCache {
140
149
source_hash : & ModuleInfoCacheSourceHash ,
141
150
module_info : & ModuleInfo ,
142
151
) -> Result < ( ) , AnyError > {
143
- self . pending_inserts . lock ( ) . push ( (
152
+ let data = (
144
153
specifier. to_string ( ) ,
145
154
serialize_media_type ( media_type) ,
146
155
source_hash. as_str ( ) . to_string ( ) ,
147
156
serde_json:: to_string ( & module_info) . unwrap ( ) ,
148
- ) ) ;
149
- Ok ( ( ) )
150
- }
151
-
152
- pub fn flush_pending_inserts ( & self ) {
153
- let pending_inserts = {
154
- let mut items = self . pending_inserts . lock ( ) ;
155
- let items = & mut * items;
156
- std:: mem:: take ( items)
157
- } ;
157
+ ) ;
158
158
159
- if pending_inserts. is_empty ( ) {
160
- return ;
159
+ let mut sender = self . sender . lock ( ) ;
160
+ if let Some ( sender) = & mut * sender {
161
+ let _ = sender. send ( data) ;
162
+ } else {
163
+ let ( tx, mut rx) = tokio:: sync:: mpsc:: unbounded_channel ( ) ;
164
+ let conn = self . conn . clone ( ) ;
165
+ let _ = deno_core:: unsync:: spawn ( async move {
166
+ while let Some ( ( specifier, media_type, source_hash, module_info) ) =
167
+ rx. recv ( ) . await
168
+ {
169
+ let conn = conn. clone ( ) ;
170
+ deno_core:: unsync:: spawn_blocking ( move || {
171
+ let sql = "
172
+ INSERT OR REPLACE INTO
173
+ moduleinfocache (specifier, media_type, source_hash, module_info)
174
+ VALUES
175
+ (?1, ?2, ?3, ?4)" ;
176
+ let result = conn. execute (
177
+ sql,
178
+ params ! [ specifier, media_type, source_hash, module_info] ,
179
+ ) ;
180
+ if let Err ( err) = result {
181
+ log:: debug!( "Error saving module cache info. {:#}" , err) ;
182
+ }
183
+ } )
184
+ . await
185
+ . unwrap ( ) ;
186
+ }
187
+ } ) ;
188
+ tx. send ( data) . unwrap ( ) ;
189
+ * sender = Some ( tx) ;
161
190
}
162
191
163
- let conn = self . conn . clone ( ) ;
164
- let _ignore = deno_core:: unsync:: spawn_blocking ( move || {
165
- let sql = "
166
- INSERT OR REPLACE INTO
167
- moduleinfocache (specifier, media_type, source_hash, module_info)
168
- VALUES
169
- (?1, ?2, ?3, ?4)" ;
170
- let result = conn. execute_bulk (
171
- sql,
172
- pending_inserts. iter ( ) ,
173
- |( specifier, media_type, source_hash, module_info) | {
174
- use deno_runtime:: deno_webstorage:: rusqlite:: ToSql ;
175
- [
176
- specifier as & dyn ToSql ,
177
- media_type as & dyn ToSql ,
178
- source_hash as & dyn ToSql ,
179
- module_info as & dyn ToSql ,
180
- ]
181
- } ,
182
- ) ;
183
- if let Err ( err) = result {
184
- log:: debug!( "Error saving module cache info. {:#}" , err) ;
185
- }
186
- } ) ;
192
+ Ok ( ( ) )
187
193
}
188
194
189
195
pub fn as_module_analyzer < ' a > (
0 commit comments