From 9b8e8c234495e2f5ef70bba0753c8cfbde9ed31d Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Tue, 31 Oct 2017 18:10:05 +0000 Subject: [PATCH] Add memory cache directly into ProtectedFileByteStore. So, it can be used standalone, without wrapping into MemoryCachingByteStore, and IKG can check for it to protect keys. Bug: Change-Id: I50635c773b73ad7e57d0dfe1fd9e3c4574f1a1aa Reviewed-on: https://dart-review.googlesource.com/17560 Reviewed-by: Alexander Aprelev Commit-Queue: Konstantin Shcheglov --- .../lib/src/byte_store/protected_file_byte_store.dart | 10 +++++++--- .../src/byte_store/protected_file_byte_store_test.dart | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pkg/front_end/lib/src/byte_store/protected_file_byte_store.dart b/pkg/front_end/lib/src/byte_store/protected_file_byte_store.dart index 8c50a0b6e22d..4b2bf15bd373 100644 --- a/pkg/front_end/lib/src/byte_store/protected_file_byte_store.dart +++ b/pkg/front_end/lib/src/byte_store/protected_file_byte_store.dart @@ -6,6 +6,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:front_end/src/byte_store/byte_store.dart'; +import 'package:front_end/src/byte_store/cache.dart'; import 'package:front_end/src/byte_store/file_byte_store.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart'; @@ -27,16 +28,18 @@ class ProtectedFileByteStore implements ByteStore { final GetCurrentTime _getCurrentTimeFunction; final FileByteStore _fileByteStore; + final Cache> _cache; /// Create a new instance of the [ProtectedFileByteStore]. /// /// The [protectionDuration] specifies how long temporary protected keys /// stay protected. ProtectedFileByteStore(this._cachePath, Duration protectionDuration, - {GetCurrentTime getCurrentTime}) + {GetCurrentTime getCurrentTime, int cacheSizeBytes: 128 * 1024 * 1024}) : _protectionDuration = protectionDuration, _getCurrentTimeFunction = getCurrentTime ?? _getCurrentTimeDefault, - _fileByteStore = new FileByteStore(_cachePath); + _fileByteStore = new FileByteStore(_cachePath), + _cache = new Cache(cacheSizeBytes, (bytes) => bytes.length); /// Remove all not protected keys. void flush() { @@ -61,7 +64,7 @@ class ProtectedFileByteStore implements ByteStore { @override List get(String key) { - return _fileByteStore.get(key); + return _cache.get(key, () => _fileByteStore.get(key)); } @override @@ -70,6 +73,7 @@ class ProtectedFileByteStore implements ByteStore { throw new ArgumentError('The key $key is reserved.'); } _fileByteStore.put(key, bytes); + _cache.put(key, bytes); } /// The [add] keys are added to the set of temporary protected keys, and diff --git a/pkg/front_end/test/src/byte_store/protected_file_byte_store_test.dart b/pkg/front_end/test/src/byte_store/protected_file_byte_store_test.dart index 2c68bc2528fc..9668d81e6cd0 100644 --- a/pkg/front_end/test/src/byte_store/protected_file_byte_store_test.dart +++ b/pkg/front_end/test/src/byte_store/protected_file_byte_store_test.dart @@ -40,7 +40,7 @@ class ProtectedFileByteStoreTest { cachePath = cacheDirectory.absolute.path; store = new ProtectedFileByteStore( cachePath, new Duration(milliseconds: 10), - getCurrentTime: _getTime); + cacheSizeBytes: 256, getCurrentTime: _getTime); } void tearDown() {