From 43bdd2787f2bffc4fc9c3397937a0ce9a183beda Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Tue, 5 May 2015 16:25:27 +0100 Subject: Search completion; better SimpleCache. SimpleCache should reset the timer very time it's rotated (including when the allowed number of entries is exceeded. --- lib/utils.coffee | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib/utils.coffee') diff --git a/lib/utils.coffee b/lib/utils.coffee index 07528714..1b2a7a3f 100644 --- a/lib/utils.coffee +++ b/lib/utils.coffee @@ -211,16 +211,18 @@ globalRoot.extend = (hash1, hash2) -> # they are discarded. class SimpleCache # expiry: expiry time in milliseconds (default, one hour) - # entries: maximum number of entries + # entries: maximum number of entries in @cache (there may be this many entries in @previous, too) constructor: (@expiry = 60 * 60 * 1000, @entries = 1000) -> @cache = {} - @previous = {} - rotate = => @rotate() - setInterval rotate, @expiry + @rotate() # Force starts the rotation timer. rotate: -> @previous = @cache @cache = {} + # We reset the timer every time the cache is rotated (which could be because a previous timer expired, or + # because the number of @entries was exceeded. + clearTimeout @timer if @timer? + @timer = Utils.setTimeout @expiry, => @rotate() has: (key) -> (key of @cache) or key of @previous -- cgit v1.2.3