Class: Monkeyshines::Store::ConditionalStore

Inherits:
Base
  • Object
show all
Defined in:
lib/wukong/store/conditional_store.rb

Constant Summary

DEFAULT_OPTIONS =
{
  :cache => { :type => :tyrant_rdb_key_store    },
  :store => { :type => :chunked_flat_file_store },
}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ConditionalStore) initialize(_options)

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 _options
  self.options = DEFAULT_OPTIONS.deep_merge(_options)
  self.cache  = Monkeyshines::Store.create(options[:cache])
  self.store  = Monkeyshines::Store.create(options[:store])
  self.misses = 0
end

Instance Attribute Details

- (Object) cache

Returns the value of attribute cache



4
5
6
# File 'lib/wukong/store/conditional_store.rb', line 4

def cache
  @cache
end

- (Object) misses

Returns the value of attribute misses



4
5
6
# File 'lib/wukong/store/conditional_store.rb', line 4

def misses
  @misses
end

- (Object) options

Returns the value of attribute options



4
5
6
# File 'lib/wukong/store/conditional_store.rb', line 4

def options
  @options
end

- (Object) store

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

- (Object) close



51
52
53
54
# File 'lib/wukong/store/conditional_store.rb', line 51

def close()
  cache.close
  store.close
end

- (Object) log_line



47
48
49
# File 'lib/wukong/store/conditional_store.rb', line 47

def log_line
  [size, "%8d misses"%misses]
end

- (Object) set(key, force = nil, &block)

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

- (Object) size



45
# File 'lib/wukong/store/conditional_store.rb', line 45

def size() cache.size  end