Chromium Code Reviews| 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:collection'; | 5 import 'dart:collection'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Store of bytes associated with string keys. | 8 * Store of bytes associated with string keys. |
| 9 * | 9 * |
| 10 * Each key must be not longer than 100 characters and consist of only `[a-z]`, | |
| 11 * `[0-9]`, `.` and `_` characters. It cannot have the sequence `..` in it. | |
|
Paul Berry
2016/10/23 21:10:18
Two other constraints:
- the key can't be the lite
| |
| 12 * | |
| 10 * Note that associations are not guaranteed to be persistent. The value | 13 * Note that associations are not guaranteed to be persistent. The value |
| 11 * associated with a key can change or become `null` at any point of time. | 14 * associated with a key can change or become `null` at any point in time. |
| 12 */ | 15 */ |
| 13 abstract class ByteStore { | 16 abstract class ByteStore { |
| 14 /** | 17 /** |
| 15 * Return the bytes associated with the given [key]. | 18 * Return the bytes associated with the given [key]. |
| 16 * Return `null` if the association does not exist. | 19 * Return `null` if the association does not exist. |
| 17 */ | 20 */ |
| 18 List<int> get(String key); | 21 List<int> get(String key); |
| 19 | 22 |
| 20 /** | 23 /** |
| 21 * Associate the given [bytes] with the [key]. | 24 * Associate the given [bytes] with the [key]. |
| 22 */ | 25 */ |
| 23 void put(String key, List<int> bytes); | 26 void put(String key, List<int> bytes); |
| 24 } | 27 } |
| 25 | 28 |
| 26 /** | 29 /** |
| 27 * [ByteStore] with LRU cache. | 30 * A wrapper around [ByteStore] which adds an in-memory LRU cache to it. |
| 31 * | |
| 32 * TODO(scheglov) Consider implementing size and/or time eviction policies. | |
| 28 */ | 33 */ |
| 29 class MemoryCachingByteStore implements ByteStore { | 34 class MemoryCachingByteStore implements ByteStore { |
| 30 final ByteStore store; | 35 final ByteStore store; |
| 31 final int maxEntries; | 36 final int maxEntries; |
| 32 | 37 |
| 33 final _map = <String, List<int>>{}; | 38 final _map = <String, List<int>>{}; |
| 34 final _keys = new LinkedHashSet<String>(); | 39 final _keys = new LinkedHashSet<String>(); |
| 35 | 40 |
| 36 MemoryCachingByteStore(this.store, this.maxEntries); | 41 MemoryCachingByteStore(this.store, this.maxEntries); |
| 37 | 42 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 52 } | 57 } |
| 53 | 58 |
| 54 void _evict() { | 59 void _evict() { |
| 55 if (_keys.length > maxEntries) { | 60 if (_keys.length > maxEntries) { |
| 56 String key = _keys.first; | 61 String key = _keys.first; |
| 57 _keys.remove(key); | 62 _keys.remove(key); |
| 58 _map.remove(key); | 63 _map.remove(key); |
| 59 } | 64 } |
| 60 } | 65 } |
| 61 } | 66 } |
| OLD | NEW |