class Apollo::Cache::MongoCache

Public Class Methods

new(options = @@DEFAULT_OPTIONS) click to toggle source
Calls superclass method Apollo::Cache::BaseCache::new
# File lib/apollo_crawler/cache/mongo_cache.rb, line 37
def initialize(options = @@DEFAULT_OPTIONS)
        super(options)
        
        opts = @@DEFAULT_OPTIONS.merge(options)
        
        @mongo_client = Mongo::MongoClient.new(opts[:host], opts[:port], :pool_size => opts[:pool_size], :pool_timeout => opts[:pool_timeout])
        @db = @mongo_client[opts[:db]]
        @coll = @db[opts[:collection]]
end

Public Instance Methods

get(key) click to toggle source
# File lib/apollo_crawler/cache/mongo_cache.rb, line 47
def get(key)
        @coll.find({:url => key})
end
set(key, value) click to toggle source

Set value associated with key Return cached value

# File lib/apollo_crawler/cache/mongo_cache.rb, line 68
def set(key, value)
        @coll.insert(value)
        return value
end
try_get(key, *args) { |args| ... } click to toggle source

Get value associated with key from cache

# File lib/apollo_crawler/cache/mongo_cache.rb, line 52
def try_get(key, *args)
        key = key.to_s

        res = get(key)

        # Not found, Create, cache and return
        if res.nil? || res.count < 1 && block_given?
                res = yield args
                return self.set(key, res)
        end

        return res.to_a[0]
end