Skip to content

Commit

Permalink
Search completion; better SimpleCache.
Browse files Browse the repository at this point in the history
SimpleCache should reset the timer very time it's rotated (including
when the allowed number of entries is exceeded.
  • Loading branch information
smblott-github committed May 5, 2015
1 parent 32895a9 commit 43bdd27
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/utils.coffee
Expand Up @@ -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
Expand Down

0 comments on commit 43bdd27

Please sign in to comment.