aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorStephen Blott2015-05-05 16:25:27 +0100
committerStephen Blott2015-05-05 16:25:29 +0100
commit43bdd2787f2bffc4fc9c3397937a0ce9a183beda (patch)
tree46b843026957552a3c373a8e81fea44bf4e09381 /lib
parent32895a96b3da225bd1b46d4b96ab98fd71755281 (diff)
downloadvimium-43bdd2787f2bffc4fc9c3397937a0ce9a183beda.tar.bz2
Search completion; better SimpleCache.
SimpleCache should reset the timer very time it's rotated (including when the allowed number of entries is exceeded.
Diffstat (limited to 'lib')
-rw-r--r--lib/utils.coffee10
1 files changed, 6 insertions, 4 deletions
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