Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Unified Diff: tools/gardening/lib/src/cache.dart

Issue 2797253006: Add find_timeouts and caching to tools/gardening (Closed)
Patch Set: Updated cf. comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gardening/lib/src/buildbot_structures.dart ('k') | tools/gardening/lib/src/logdog.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gardening/lib/src/cache.dart
diff --git a/tools/gardening/lib/src/cache.dart b/tools/gardening/lib/src/cache.dart
new file mode 100644
index 0000000000000000000000000000000000000000..4dfac0128c5d714420b40bd549e2b68867862470
--- /dev/null
+++ b/tools/gardening/lib/src/cache.dart
@@ -0,0 +1,48 @@
+// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'dart:async';
+import 'dart:io';
+
+import 'util.dart';
+
+final Cache cache = new Cache(Uri.base.resolve('temp/gardening-cache/'));
+
+/// Simple cache for test step output.
+class Cache {
+ Uri base;
+
+ Cache(this.base);
+
+ Map<String, String> memoryCache = <String, String>{};
+
+ /// Load the cache text for [path] or call [ifAbsent] to fetch the data.
+ Future<String> read(String path, Future<String> ifAbsent()) async {
+ if (memoryCache.containsKey(path)) {
+ log('Found $path in memory cache');
+ return memoryCache[path];
+ }
+ File file = new File.fromUri(base.resolve(path));
+ String text;
+ if (file.existsSync()) {
+ log('Found $path in file cache');
+ text = file.readAsStringSync();
+ memoryCache[path] = text;
+ } else {
+ log('Loading $path');
+ text = await ifAbsent();
+ write(path, text);
+ }
+ return text;
+ }
+
+ /// Store [text] as the cache data for [path].
+ void write(String path, String text) {
+ log('Creating $path in file cache');
+ File file = new File.fromUri(base.resolve(path));
+ file.createSync(recursive: true);
+ file.writeAsStringSync(text);
+ memoryCache[path] = text;
+ }
+}
« no previous file with comments | « tools/gardening/lib/src/buildbot_structures.dart ('k') | tools/gardening/lib/src/logdog.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698