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