OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'util.dart'; | 8 import 'util.dart'; |
9 | 9 |
10 final Cache cache = new Cache(Uri.base.resolve('temp/gardening-cache/')); | 10 final Cache cache = new Cache(Uri.base.resolve('temp/gardening-cache/')); |
11 | 11 |
12 /// Simple cache for test step output. | 12 /// Simple cache for test step output. |
13 class Cache { | 13 class Cache { |
14 Uri base; | 14 Uri base; |
15 | 15 |
16 Cache(this.base); | 16 Cache(this.base); |
17 | 17 |
18 Map<String, String> memoryCache = <String, String>{}; | 18 Map<String, String> memoryCache = <String, String>{}; |
19 | 19 |
20 /// Load the cache text for [path] or call [ifAbsent] to fetch the data. | 20 /// Load the cache text for [path] or call [ifAbsent] to fetch the data. |
21 Future<String> read(String path, Future<String> ifAbsent()) async { | 21 Future<String> read(String path, Future<String> ifAbsent()) async { |
22 if (memoryCache.containsKey(path)) { | 22 if (memoryCache.containsKey(path)) { |
23 log('Found $path in memory cache'); | 23 log('Found $path in memory cache'); |
24 return memoryCache[path]; | 24 return memoryCache[path]; |
25 } | 25 } |
| 26 if (base == null) { |
| 27 return ifAbsent(); |
| 28 } |
26 File file = new File.fromUri(base.resolve(path)); | 29 File file = new File.fromUri(base.resolve(path)); |
27 String text; | 30 String text; |
28 if (file.existsSync()) { | 31 if (file.existsSync()) { |
29 log('Found $path in file cache'); | 32 log('Found $path in file cache'); |
30 text = file.readAsStringSync(); | 33 text = file.readAsStringSync(); |
31 memoryCache[path] = text; | 34 memoryCache[path] = text; |
32 } else { | 35 } else { |
33 log('Loading $path'); | 36 log('Loading $path'); |
34 text = await ifAbsent(); | 37 text = await ifAbsent(); |
35 write(path, text); | 38 write(path, text); |
36 } | 39 } |
37 return text; | 40 return text; |
38 } | 41 } |
39 | 42 |
40 /// Store [text] as the cache data for [path]. | 43 /// Store [text] as the cache data for [path]. |
41 void write(String path, String text) { | 44 void write(String path, String text) { |
| 45 if (base == null) return; |
42 log('Creating $path in file cache'); | 46 log('Creating $path in file cache'); |
43 File file = new File.fromUri(base.resolve(path)); | 47 File file = new File.fromUri(base.resolve(path)); |
44 file.createSync(recursive: true); | 48 file.createSync(recursive: true); |
45 file.writeAsStringSync(text); | 49 file.writeAsStringSync(text); |
46 memoryCache[path] = text; | 50 memoryCache[path] = text; |
47 } | 51 } |
48 } | 52 } |
OLD | NEW |