@@ -151,13 +151,17 @@ class InMemoryFilesystemCache implements FilesystemCache {
151
151
final maybeResult = _bytesContentCache[id];
152
152
if (maybeResult != null ) return maybeResult;
153
153
154
- // check canRead first?
154
+ // Not in cache. Check pending writes and deletes.
155
155
if (_pendingDeletes.containsKey (id)) throw AssetNotFoundException (id);
156
156
if (_pendingWrites.containsKey (id)) {
157
- throw StateError ('Whoops, should have been cached : $id ' );
157
+ throw StateError ('Should be in cache : $id ' );
158
158
}
159
159
160
- return _bytesContentCache[id] = ifAbsent ();
160
+ // Read and cache.
161
+ final bytes = _bytesContentCache[id] = ifAbsent ();
162
+ // Might not be a valid String, only convert to String when read as String.
163
+ _stringContentCache.remove (id);
164
+ return bytes;
161
165
}
162
166
163
167
@override
@@ -174,18 +178,21 @@ class InMemoryFilesystemCache implements FilesystemCache {
174
178
final maybeResult = _stringContentCache[id];
175
179
if (maybeResult != null ) return maybeResult;
176
180
177
- // check canRead first?
181
+ // Not in cache. Check pending writes and deletes.
178
182
if (_pendingDeletes.containsKey (id)) throw AssetNotFoundException (id);
179
183
if (_pendingWrites.containsKey (id)) {
184
+ // Might have been written as bytes; only cached as String when it's
185
+ // read as String, because it might not be a valid String.
180
186
final bytes = _bytesContentCache[id];
181
187
if (bytes == null ) {
182
- throw StateError ('Whoops, should have been cached : $id ' );
188
+ throw StateError ('Should be in cache : $id ' );
183
189
} else {
184
190
return _stringContentCache[id] = utf8.decode (_bytesContentCache[id]! );
185
191
}
186
192
}
187
193
188
194
final bytes = ifAbsent ();
195
+ _bytesContentCache[id] = bytes;
189
196
return _stringContentCache[id] = utf8.decode (bytes);
190
197
}
191
198
@@ -195,13 +202,12 @@ class InMemoryFilesystemCache implements FilesystemCache {
195
202
List <int > contents, {
196
203
required void Function () writer,
197
204
}) {
198
- _bytesContentCache[id] =
199
- (contents is Uint8List ? contents : Uint8List .fromList (contents));
200
205
// [contents] might not be a valid string so defer trying to
201
206
// decode it until if/when it's read as a string.
202
207
_stringContentCache.remove (id);
208
+ _bytesContentCache[id] =
209
+ (contents is Uint8List ? contents : Uint8List .fromList (contents));
203
210
_canReadCache[id] = true ;
204
-
205
211
_pendingDeletes.remove (id);
206
212
_pendingWrites[id] = writer;
207
213
}
@@ -213,19 +219,28 @@ class InMemoryFilesystemCache implements FilesystemCache {
213
219
Encoding encoding = utf8,
214
220
required void Function () writer,
215
221
}) {
216
- // TODO: encoding?
217
- _stringContentCache[id] = contents;
218
- _bytesContentCache[id] = utf8.encode (contents);
219
- _canReadCache[id] = true ;
222
+ // Strings are only cached if utf8.
223
+ if (encoding == utf8) {
224
+ _stringContentCache[id] = contents;
225
+ } else {
226
+ _stringContentCache.remove (id);
227
+ }
228
+
229
+ final encoded = encoding.encode (contents);
230
+ _bytesContentCache[id] =
231
+ encoded is Uint8List ? encoded : Uint8List .fromList (encoded);
220
232
233
+ _canReadCache[id] = true ;
221
234
_pendingDeletes.remove (id);
222
235
_pendingWrites[id] = writer;
223
236
}
224
237
225
238
@override
226
239
void delete (AssetId id, {required void Function () deleter}) {
240
+ _stringContentCache.remove (id);
241
+ _bytesContentCache.remove (id);
242
+ _canReadCache[id] = false ;
227
243
_pendingWrites.remove (id);
228
244
_pendingDeletes[id] = deleter;
229
- _canReadCache[id] = false ;
230
245
}
231
246
}
0 commit comments