| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:isolate'; | 7 import 'dart:isolate'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/dart/analysis/byte_store.dart'; | 9 import 'package:analyzer/src/dart/analysis/byte_store.dart'; |
| 10 import 'package:path/path.dart'; | 10 import 'package:path/path.dart'; |
| 11 | 11 |
| 12 /** | 12 /** |
| 13 * The request that is sent from the main isolate to the clean-up isolate. |
| 14 */ |
| 15 class CacheCleanUpRequest { |
| 16 final String cachePath; |
| 17 final int maxSizeBytes; |
| 18 final SendPort replyTo; |
| 19 |
| 20 CacheCleanUpRequest(this.cachePath, this.maxSizeBytes, this.replyTo); |
| 21 } |
| 22 |
| 23 /** |
| 13 * [ByteStore] that stores values as files. | 24 * [ByteStore] that stores values as files. |
| 14 */ | 25 */ |
| 15 class FileByteStore implements ByteStore { | 26 class FileByteStore implements ByteStore { |
| 16 static bool _cleanUpSendPortShouldBePrepared = true; | 27 static bool _cleanUpSendPortShouldBePrepared = true; |
| 17 static SendPort _cleanUpSendPort; | 28 static SendPort _cleanUpSendPort; |
| 18 | 29 |
| 19 final String _cachePath; | 30 final String _cachePath; |
| 20 final String _tempName = 'temp_${pid}'; | 31 final String _tempName = 'temp_${pid}'; |
| 21 final int _maxSizeBytes; | 32 final int _maxSizeBytes; |
| 22 | 33 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 } else { | 78 } else { |
| 68 while (_cleanUpSendPort == null) { | 79 while (_cleanUpSendPort == null) { |
| 69 await new Future.delayed(new Duration(milliseconds: 100), () {}); | 80 await new Future.delayed(new Duration(milliseconds: 100), () {}); |
| 70 } | 81 } |
| 71 } | 82 } |
| 72 | 83 |
| 73 if (!_evictionIsolateIsRunning) { | 84 if (!_evictionIsolateIsRunning) { |
| 74 _evictionIsolateIsRunning = true; | 85 _evictionIsolateIsRunning = true; |
| 75 try { | 86 try { |
| 76 ReceivePort response = new ReceivePort(); | 87 ReceivePort response = new ReceivePort(); |
| 77 _cleanUpSendPort.send([_cachePath, _maxSizeBytes, response.sendPort]); | 88 _cleanUpSendPort.send(new CacheCleanUpRequest( |
| 89 _cachePath, _maxSizeBytes, response.sendPort)); |
| 78 await response.first; | 90 await response.first; |
| 79 } finally { | 91 } finally { |
| 80 _evictionIsolateIsRunning = false; | 92 _evictionIsolateIsRunning = false; |
| 81 _bytesWrittenSinceCleanup = 0; | 93 _bytesWrittenSinceCleanup = 0; |
| 82 } | 94 } |
| 83 } | 95 } |
| 84 } | 96 } |
| 85 | 97 |
| 86 /** | 98 /** |
| 87 * This function is started in a new isolate, receives cache folder clean up | 99 * This function is started in a new isolate, receives cache folder clean up |
| 88 * requests and evicts older files from the folder. | 100 * requests and evicts older files from the folder. |
| 89 */ | 101 */ |
| 90 static void _cacheCleanUpFunction(SendPort initialReplyTo) { | 102 static void _cacheCleanUpFunction(SendPort initialReplyTo) { |
| 91 ReceivePort port = new ReceivePort(); | 103 ReceivePort port = new ReceivePort(); |
| 92 initialReplyTo.send(port.sendPort); | 104 initialReplyTo.send(port.sendPort); |
| 93 port.listen((args) async { | 105 port.listen((request) async { |
| 94 if (args is List && | 106 if (request is CacheCleanUpRequest) { |
| 95 args.length == 3 && | 107 await _cleanUpFolder(request.cachePath, request.maxSizeBytes); |
| 96 args[0] is String && | 108 // Let the client know that we're done. |
| 97 args[1] is int && | 109 request.replyTo.send(true); |
| 98 args[2] is SendPort) { | |
| 99 String cachePath = args[0] as String; | |
| 100 int maxSizeBytes = args[1] as int; | |
| 101 await _cleanUpFolder(cachePath, maxSizeBytes); | |
| 102 // Let that client know that we're done. | |
| 103 SendPort replyTo = args[2] as SendPort; | |
| 104 replyTo.send(true); | |
| 105 } | 110 } |
| 106 }); | 111 }); |
| 107 } | 112 } |
| 108 | 113 |
| 109 static Future<Null> _cleanUpFolder(String cachePath, int maxSizeBytes) async { | 114 static Future<Null> _cleanUpFolder(String cachePath, int maxSizeBytes) async { |
| 110 // Prepare the list of files and their statistics. | 115 // Prepare the list of files and their statistics. |
| 111 List<File> files = <File>[]; | 116 List<File> files = <File>[]; |
| 112 Map<File, FileStat> fileStatMap = {}; | 117 Map<File, FileStat> fileStatMap = {}; |
| 113 int currentSizeBytes = 0; | 118 int currentSizeBytes = 0; |
| 114 List<FileSystemEntity> resources = new Directory(cachePath).listSync(); | 119 List<FileSystemEntity> resources = new Directory(cachePath).listSync(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 132 if (currentSizeBytes < maxSizeBytes) { | 137 if (currentSizeBytes < maxSizeBytes) { |
| 133 break; | 138 break; |
| 134 } | 139 } |
| 135 try { | 140 try { |
| 136 await file.delete(); | 141 await file.delete(); |
| 137 } catch (_) {} | 142 } catch (_) {} |
| 138 currentSizeBytes -= fileStatMap[file].size; | 143 currentSizeBytes -= fileStatMap[file].size; |
| 139 } | 144 } |
| 140 } | 145 } |
| 141 } | 146 } |
| OLD | NEW |