Sha256: f4e783f7e40d72beb56fb6782628e1872e345135f0e81b55f66e9300f807343f

Contents?: true

Size: 1.5 KB

Versions: 16

Compression:

Stored size: 1.5 KB

Contents

module Monkeyshines
  module Store
    class ConditionalStore < Monkeyshines::Store::Base
      attr_accessor :options, :cache, :store, :misses

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

      #
      #
      # +cache+ must behave like a hash (Hash and
      #  Monkeyshines::Store::TyrantRdbKeyStore are both cromulent
      #  choices).
      #
      #
      #
      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

      #
      # 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
      #
      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

      def size() cache.size  end

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

      def close()
        cache.close
        store.close
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
wukong-3.0.0.pre old/wukong/store/conditional_store.rb
wukong-2.0.2 lib/wukong/store/conditional_store.rb
wukong-2.0.1 lib/wukong/store/conditional_store.rb
wukong-2.0.0 lib/wukong/store/conditional_store.rb
wukong-1.5.4 lib/wukong/store/conditional_store.rb
wukong-1.5.3 lib/wukong/store/conditional_store.rb
wukong-1.5.2 lib/wukong/store/conditional_store.rb
wukong-1.5.1 lib/wukong/store/conditional_store.rb
wukong-1.5.0 lib/wukong/store/conditional_store.rb
wukong-1.4.12 lib/wukong/store/conditional_store.rb
wukong-1.4.11 lib/wukong/store/conditional_store.rb
monkeyshines-0.2.3 lib/monkeyshines/store/conditional_store.rb
monkeyshines-0.2.2 lib/monkeyshines/store/conditional_store.rb
monkeyshines-0.2.1 lib/monkeyshines/store/conditional_store.rb
monkeyshines-0.2.0 lib/monkeyshines/store/conditional_store.rb
monkeyshines-0.0.2 lib/monkeyshines/store/conditional_store.rb