Class: Monkeyshines::Store::ConditionalStore
- Defined in:
- lib/wukong/store/conditional_store.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :cache => { :type => :tyrant_rdb_key_store }, :store => { :type => :chunked_flat_file_store }, }
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#misses ⇒ Object
Returns the value of attribute misses.
-
#options ⇒ Object
Returns the value of attribute options.
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(_options) ⇒ ConditionalStore
constructor
cachemust behave like a hash (Hash and Monkeyshines::Store::TyrantRdbKeyStore are both cromulent choices). - #log_line ⇒ Object
-
#set(key, force = nil, &block) ⇒ Object
If key is absent, save the result of calling the block.
- #size ⇒ Object
Constructor Details
#initialize(_options) ⇒ ConditionalStore
cache must behave like a hash (Hash and
Monkeyshines::Store::TyrantRdbKeyStore are both cromulent
choices).
19 20 21 22 23 24 |
# File 'lib/wukong/store/conditional_store.rb', line 19 def initialize self. = DEFAULT_OPTIONS.deep_merge() self.cache = Monkeyshines::Store.create([:cache]) self.store = Monkeyshines::Store.create([:store]) self.misses = 0 end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
4 5 6 |
# File 'lib/wukong/store/conditional_store.rb', line 4 def cache @cache end |
#misses ⇒ Object
Returns the value of attribute misses.
4 5 6 |
# File 'lib/wukong/store/conditional_store.rb', line 4 def misses @misses end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/wukong/store/conditional_store.rb', line 4 def @options end |
#store ⇒ Object
Returns the value of attribute store.
4 5 6 |
# File 'lib/wukong/store/conditional_store.rb', line 4 def store @store end |
Instance Method Details
#close ⇒ Object
51 52 53 54 |
# File 'lib/wukong/store/conditional_store.rb', line 51 def close() cache.close store.close end |
#log_line ⇒ Object
47 48 49 |
# File 'lib/wukong/store/conditional_store.rb', line 47 def log_line [size, "%8d misses"%misses] end |
#set(key, force = nil, &block) ⇒ Object
If key is absent, save the result of calling the block. If key is present, block is never called.
Ex:
rt_store.set(url) do
fetcher.get url # will only be called if url isn't in rt_store
end
35 36 37 38 39 40 41 42 43 |
# File 'lib/wukong/store/conditional_store.rb', line 35 def set key, force=nil, &block return if (!force) && cache.include?(key) cache_val, store_val = block.call() return unless cache_val cache.set_nr key, cache_val # update cache store << store_val # save value self.misses += 1 # track the cache miss store_val end |
#size ⇒ Object
45 |
# File 'lib/wukong/store/conditional_store.rb', line 45 def size() cache.size end |