Class: Apollo::Cache::MongoCache
- 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
- #get(key) ⇒ Object
-
#initialize(options = @@DEFAULT_OPTIONS) ⇒ MongoCache
constructor
A new instance of MongoCache.
-
#set(key, value) ⇒ Object
Set value associated with key Return cached value.
-
#try_get(key, *args) ⇒ Object
Get value associated with key from cache.
Methods inherited from BaseCache
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( = @@DEFAULT_OPTIONS) super() opts = @@DEFAULT_OPTIONS.merge() @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 |