Class: Apollo::Cache::MongoCache

Inherits:
BaseCache show all
Defined in:
lib/apollo_crawler/cache/mongo_cache.rb

Constant Summary collapse

@@DEFAULT_OPTIONS =
{
  :host => 'localhost', 
  :port => 27017, 
  :pool_size => 5, 
  :pool_timeout => 5,
  :db => 'apollo-crawler',
  :collection => 'cached_docs'
}

Instance Method Summary collapse

Methods inherited from BaseCache

#remove

Constructor Details

#initialize(options = @@DEFAULT_OPTIONS) ⇒ MongoCache

Returns a new instance of MongoCache.



37
38
39
40
41
42
43
44
45
# 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

Instance Method Details

#get(key) ⇒ Object



47
48
49
# File 'lib/apollo_crawler/cache/mongo_cache.rb', line 47

def get(key)
  @coll.find({:url => key})
end

#set(key, value) ⇒ Object

Set value associated with key Return cached value



68
69
70
71
# File 'lib/apollo_crawler/cache/mongo_cache.rb', line 68

def set(key, value)
  @coll.insert(value)
  return value
end

#try_get(key, *args) ⇒ Object

Get value associated with key from cache



52
53
54
55
56
57
58
59
60
61
62
63
64
# 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