class Routemaster::Dirty::Map
Collects information about entities whose state has changed and need to be refreshed. Typically mark
is called when notified state has changed (e.g. from the bus) and sweep
when one wants to know what has changed.
Use case: when some entites are very volatile, the map will hold a “dirty” state for multiple updates until the client is ready to update.
Constants
- KEY
Public Class Methods
new(redis: nil)
click to toggle source
# File lib/routemaster/dirty/map.rb, line 19 def initialize(redis: nil) @redis = redis || Config.drain_redis end
Public Instance Methods
all()
click to toggle source
# File lib/routemaster/dirty/map.rb, line 38 def all @redis.smembers(KEY) end
count()
click to toggle source
Number of currently dirty entities.
# File lib/routemaster/dirty/map.rb, line 58 def count @redis.scard(KEY) end
mark(url)
click to toggle source
Marks an entity as dirty. Return true if newly marked, false if re-marking.
# File lib/routemaster/dirty/map.rb, line 25 def mark(url) @redis.sadd(KEY, url).tap do |marked| publish(:dirty_entity, url) if marked end end
sweep(limit = 0) { |url| ... }
click to toggle source
Yields URLs for dirty entitities. The entity will only be marked as clean if the block returns truthy. It is possible to call next
or break
from the block.
# File lib/routemaster/dirty/map.rb, line 45 def sweep(limit = 0) unswept = [] while (url = @redis.spop(KEY)) unswept.push url is_swept = !! yield(url) unswept.pop if is_swept break if (limit -=1).zero? end ensure @redis.sadd(KEY, unswept) if unswept.any? end
sweep_one(url, &block)
click to toggle source
Runs the block. The entity will only be marked as clean if the block returns truthy.
# File lib/routemaster/dirty/map.rb, line 33 def sweep_one(url, &block) return unless block.call(url) @redis.srem(KEY, url) end