Class: RightScale::Dispatcher::Dispatched
- Defined in:
- lib/right_agent/dispatcher.rb
Overview
Cache for requests that have been dispatched recently This cache is intended for use in checking for duplicate requests
Constant Summary collapse
- MAX_AGE =
Maximum number of seconds to retain a dispatched request in cache This must be greater than the maximum possible retry timeout to avoid duplicate execution of a request
12 * 60 * 60
Instance Method Summary collapse
-
#fetch(token) ⇒ Object
Fetch request.
-
#initialize ⇒ Dispatched
constructor
Initialize cache.
-
#size ⇒ Object
Get cache size.
-
#stats ⇒ Object
Get cache statistics.
-
#store(token) ⇒ Object
Store dispatched request token in cache.
Constructor Details
#initialize ⇒ Dispatched
Initialize cache
41 42 43 44 |
# File 'lib/right_agent/dispatcher.rb', line 41 def initialize @cache = {} @lru = [] end |
Instance Method Details
#fetch(token) ⇒ Object
Fetch request
Parameters
- token(String)
-
Generated message identifier
Return
- (Boolean)
-
true if request has been dispatched, otherwise false
73 74 75 76 77 78 |
# File 'lib/right_agent/dispatcher.rb', line 73 def fetch(token) if @cache[token] @cache[token] = Time.now.to_i @lru.push(@lru.delete(token)) end end |
#size ⇒ Object
Get cache size
Return
- (Integer)
-
Number of cache entries
84 85 86 |
# File 'lib/right_agent/dispatcher.rb', line 84 def size @cache.size end |
#stats ⇒ Object
Get cache statistics
Return
- stats(Hash|nil)
-
Current statistics, or nil if cache empty
- “total”(Integer)
-
Total number in cache, or nil if none
- “oldest”(Integer)
-
Number of seconds since oldest cache entry created or updated
- “youngest”(Integer)
-
Number of seconds since youngest cache entry created or updated
95 96 97 98 99 100 101 102 103 |
# File 'lib/right_agent/dispatcher.rb', line 95 def stats if size > 0 { "total" => size, "oldest age" => size > 0 ? Time.now.to_i - @cache[@lru.first] : 0, "youngest age" => size > 0 ? Time.now.to_i - @cache[@lru.last] : 0 } end end |
#store(token) ⇒ Object
Store dispatched request token in cache
Parameters
- token(String)
-
Generated message identifier
Return
- true
-
Always return true
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/right_agent/dispatcher.rb', line 53 def store(token) now = Time.now.to_i if @cache.has_key?(token) @cache[token] = now @lru.push(@lru.delete(token)) else @cache[token] = now @lru.push(token) @cache.delete(@lru.shift) while (now - @cache[@lru.first]) > MAX_AGE end true end |