| 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]`, | 10 * Each key must be not longer than 100 characters and consist of only `[a-z]`, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 final _map = new LinkedHashMap<String, List<int>>(); | 56 final _map = new LinkedHashMap<String, List<int>>(); |
| 57 int _currentSizeBytes = 0; | 57 int _currentSizeBytes = 0; |
| 58 | 58 |
| 59 MemoryCachingByteStore(this._store, this._maxSizeBytes); | 59 MemoryCachingByteStore(this._store, this._maxSizeBytes); |
| 60 | 60 |
| 61 @override | 61 @override |
| 62 List<int> get(String key) { | 62 List<int> get(String key) { |
| 63 List<int> bytes = _map.remove(key); | 63 List<int> bytes = _map.remove(key); |
| 64 if (bytes == null) { | 64 if (bytes == null) { |
| 65 bytes = _store.get(key); | 65 bytes = _store.get(key); |
| 66 _map[key] = bytes; | 66 if (bytes != null) { |
| 67 _currentSizeBytes += bytes?.length ?? 0; | 67 _map[key] = bytes; |
| 68 _evict(); | 68 _currentSizeBytes += bytes.length; |
| 69 _evict(); |
| 70 } |
| 69 } else { | 71 } else { |
| 70 _map[key] = bytes; | 72 _map[key] = bytes; |
| 71 } | 73 } |
| 72 return bytes; | 74 return bytes; |
| 73 } | 75 } |
| 74 | 76 |
| 75 @override | 77 @override |
| 76 void put(String key, List<int> bytes) { | 78 void put(String key, List<int> bytes) { |
| 77 _store.put(key, bytes); | 79 _store.put(key, bytes); |
| 78 _currentSizeBytes -= _map[key]?.length ?? 0; | 80 _currentSizeBytes -= _map[key]?.length ?? 0; |
| 79 _map[key] = bytes; | 81 _map[key] = bytes; |
| 80 _currentSizeBytes += bytes.length; | 82 _currentSizeBytes += bytes.length; |
| 81 _evict(); | 83 _evict(); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void _evict() { | 86 void _evict() { |
| 85 while (_currentSizeBytes > _maxSizeBytes) { | 87 while (_currentSizeBytes > _maxSizeBytes) { |
| 86 if (_map.isEmpty) { | 88 if (_map.isEmpty) { |
| 89 // Should be impossible, since _currentSizeBytes should always match |
| 90 // _map. But recover anyway. |
| 91 assert(false); |
| 92 _currentSizeBytes = 0; |
| 87 break; | 93 break; |
| 88 } | 94 } |
| 89 String key = _map.keys.first; | 95 String key = _map.keys.first; |
| 90 List<int> bytes = _map.remove(key); | 96 List<int> bytes = _map.remove(key); |
| 91 _currentSizeBytes -= bytes.length; | 97 _currentSizeBytes -= bytes.length; |
| 92 } | 98 } |
| 93 } | 99 } |
| 94 } | 100 } |
| OLD | NEW |