Skip to content

Commit

Permalink
Add memory cache directly into ProtectedFileByteStore.
Browse files Browse the repository at this point in the history
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 <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
  • Loading branch information
scheglov authored and [email protected] committed Oct 31, 2017
1 parent 9b14a36 commit 9b8e8c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions pkg/front_end/lib/src/byte_store/protected_file_byte_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -27,16 +28,18 @@ class ProtectedFileByteStore implements ByteStore {
final GetCurrentTime _getCurrentTimeFunction;

final FileByteStore _fileByteStore;
final Cache<String, List<int>> _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() {
Expand All @@ -61,7 +64,7 @@ class ProtectedFileByteStore implements ByteStore {

@override
List<int> get(String key) {
return _fileByteStore.get(key);
return _cache.get(key, () => _fileByteStore.get(key));
}

@override
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 9b8e8c2

Please sign in to comment.