From b09822eb349ec88a573d4f450e9b57e8fa3c6473 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 20 Jun 2015 09:54:17 +0100 Subject: Fix SimpleCache bugs. This fixes two bugs in SimpleCache. 1. Rotate the cache on the next tick. There is a marginally small chance that the cache will rotate between calls to .has() and .get(). So, we do the rotation ansynchronously. This guarantees that these two functions will always see the same cache state. 2. The implementation of .clear() (which is unused, I think) has at some point become out of date (and incorrect) w.r.t. the implementation of .rotate(). --- lib/utils.coffee | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/utils.coffee b/lib/utils.coffee index 93045f32..de335452 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -323,14 +323,15 @@ class SimpleCache null rotate: (force = false) -> - if force or @entries < Object.keys(@cache).length or @expiry < new Date() - @lastRotation - @lastRotation = new Date() - @previous = @cache - @cache = {} + Utils.nextTick => + if force or @entries < Object.keys(@cache).length or @expiry < new Date() - @lastRotation + @lastRotation = new Date() + @previous = @cache + @cache = {} clear: -> - @rotate true - @rotate true + @cache = {} + @previous = {} # This is a simple class for the common case where we want to use some data value which may be immediately # available, or for which we may have to wait. It implements a use-immediately-or-wait queue, and calls the -- cgit v1.2.3