Class: HTTPClient::LRUCache
- Inherits:
-
Object
- Object
- HTTPClient::LRUCache
- Defined in:
- lib/httpclient/lru_cache.rb
Defined Under Namespace
Classes: Datum
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#retry_delay ⇒ Object
readonly
Returns the value of attribute retry_delay.
-
#soft_ttl ⇒ Object
readonly
Returns the value of attribute soft_ttl.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Instance Method Summary collapse
- #fetch(key) ⇒ Object (also: #[])
-
#initialize(opts = {}) ⇒ LRUCache
constructor
A new instance of LRUCache.
- #store(key, value) ⇒ Object (also: #[]=)
Constructor Details
#initialize(opts = {}) ⇒ LRUCache
Returns a new instance of LRUCache.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/httpclient/lru_cache.rb', line 14 def initialize(opts={}) @max_size = Integer(opts[:max_size] || 100) @ttl = Float(opts[:ttl] || 0) @soft_ttl = Float(opts[:soft_ttl] || 0) @retry_delay = Float(opts[:retry_delay] || 0) raise "max_size must not be negative" if @max_size < 0 raise "ttl must not be negative" if @ttl < 0 raise "soft_ttl must not be negative" if @soft_ttl < 0 raise "retry_delay must not be negative" if @retry_delay < 0 @data = LruRedux::Cache.new(@max_size) end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
12 13 14 |
# File 'lib/httpclient/lru_cache.rb', line 12 def max_size @max_size end |
#retry_delay ⇒ Object (readonly)
Returns the value of attribute retry_delay.
12 13 14 |
# File 'lib/httpclient/lru_cache.rb', line 12 def retry_delay @retry_delay end |
#soft_ttl ⇒ Object (readonly)
Returns the value of attribute soft_ttl.
12 13 14 |
# File 'lib/httpclient/lru_cache.rb', line 12 def soft_ttl @soft_ttl end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
12 13 14 |
# File 'lib/httpclient/lru_cache.rb', line 12 def ttl @ttl end |
Instance Method Details
#fetch(key) ⇒ Object Also known as: []
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/httpclient/lru_cache.rb', line 36 def fetch(key) datum = @data[key] if datum.nil? store(key, value = yield) elsif datum.expired? delete(key) store(key, value = yield) elsif datum.soft_expired? begin store(key, value = yield) rescue RuntimeError => e datum.soft_expiration = (Time.now + retry_delay) if retry_delay > 0 datum.value end else datum.value end end |