Skip to content

Commit 235403d

Browse files
committed
fix: move freeing of previous entry to top to not interfere with cleanup logic
1 parent aa65a0c commit 235403d

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

packages/canister/lib/src/cache/local_cache.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ class LocalCache<K, V> implements Cache<K, V> {
131131
/// 4. It subtracts the weight of the new entry from the available weight and inserts the entry into the cache.
132132
@override
133133
void put(K key, V value) {
134+
// Free up the previous entry's weight
135+
var previousEntry = _map[key];
136+
if (previousEntry != null) {
137+
_availableWeight += previousEntry.weight;
138+
}
139+
134140
var entry = _createEntry(key, value);
135141
if (_availableWeight < entry.weight) {
136142
var requiredWeight = entry.weight - _availableWeight;
@@ -147,11 +153,6 @@ class LocalCache<K, V> implements Cache<K, V> {
147153
cleanup();
148154
}
149155

150-
// Free up the previous entry's weight
151-
var previousEntry = _map[key];
152-
if (previousEntry != null) {
153-
_availableWeight += previousEntry.weight;
154-
}
155156

156157
_availableWeight -= entry.weight;
157158
_map[key] = entry;

0 commit comments

Comments
 (0)