Class: Apollo::Cache::MongoCache

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

Instance Method Summary collapse

Methods inherited from BaseCache

#clear, #contains, #invalidate

Constructor Details

#initializeMongoCache

Returns a new instance of MongoCache.



28
29
30
31
# File 'lib/apollo_crawler/cache/mongo_cache.rb', line 28

def initialize
	@mongo_client = Mongo::MongoClient.new('localhost', 27017, :pool_size => 5, :pool_timeout => 5)
	@db = @mongo_client['apollo-crawler']
end

Instance Method Details

#get(key, *args) ⇒ Object

Get value associated with key from cache



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/apollo_crawler/cache/mongo_cache.rb', line 34

def get(key, *args)
	res = @db['docs'].find({:url => 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

#set(key, value) ⇒ Object

Set value associated with key Return cached value



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

def set(key, value)
	@db['docs'].insert(value)
	return value
end