Class: BungieClient::Cache
- Inherits:
-
Object
- Object
- BungieClient::Cache
- Defined in:
- lib/bungie_client/cache.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Instance Method Summary collapse
-
#get(key) ⇒ Object
Get value.
-
#initialize(options = {}) ⇒ Cache
constructor
Initialize handlers of cache client with options.
-
#set(key, value, ttl = nil) ⇒ Object
Set value.
Constructor Details
#initialize(options = {}) ⇒ Cache
Initialize handlers of cache client with options
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/bungie_client/cache.rb', line 32 def initialize( = {}) @ttl = ([:ttl].is_a?(Integer) && [:ttl] > 0) ? [:ttl] : 900 if [:client].nil? raise 'You must define the client initialization.' else @client = [:client] end if [:get].is_a? Proc @get = [:get] else raise 'You must define the get method for caching.' end if [:set].is_a? Proc @set = [:set] else raise 'You must define the set method for caching.' end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
2 3 4 |
# File 'lib/bungie_client/cache.rb', line 2 def client @client end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
3 4 5 |
# File 'lib/bungie_client/cache.rb', line 3 def ttl @ttl end |
Instance Method Details
#get(key) ⇒ Object
Get value
10 11 12 13 14 |
# File 'lib/bungie_client/cache.rb', line 10 def get(key) result = @get.call @client, key.to_s Marshal.load result unless result.nil? end |
#set(key, value, ttl = nil) ⇒ Object
Set value
21 22 23 |
# File 'lib/bungie_client/cache.rb', line 21 def set(key, value, ttl = nil) @set.call @client, key.to_s, Marshal.dump(value), (ttl || @ttl) end |