Class: Moneta::Adapters::MongoDB

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/moneta/adapters/mongodb.rb

Instance Method Summary collapse

Methods included from Defaults

#[]=, #fetch

Constructor Details

#initialize(options = {}) ⇒ MongoDB

Returns a new instance of MongoDB.



13
14
15
16
17
18
19
20
21
22
# File 'lib/moneta/adapters/mongodb.rb', line 13

def initialize(options = {})
  options = {
    :host => ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost',
    :port => ENV['MONGO_RUBY_DRIVER_PORT'] || Mongo::Connection::DEFAULT_PORT,
    :db => 'cache',
    :collection => 'cache'
  }.update(options)
  conn = Mongo::Connection.new(options[:host], options[:port])
  @cache = conn.db(options[:db]).collection(options[:collection])
end

Instance Method Details

#[](key) ⇒ Object



28
29
30
31
# File 'lib/moneta/adapters/mongodb.rb', line 28

def [](key)
  res = @cache.find_one('_id' => key_for(key))
  res && deserialize(res['data'])
end

#clearObject



46
47
48
# File 'lib/moneta/adapters/mongodb.rb', line 46

def clear(*)
  @cache.remove
end

#delete(key) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/moneta/adapters/mongodb.rb', line 33

def delete(key, *)
  string_key = key_for(key)

  value = self[key]
  @cache.remove('_id' => string_key) if value
  value
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/moneta/adapters/mongodb.rb', line 24

def key?(key, *)
  !!self[key]
end

#store(key, value) ⇒ Object



41
42
43
44
# File 'lib/moneta/adapters/mongodb.rb', line 41

def store(key, value, *)
  key = key_for(key)
  @cache.insert({ '_id' => key, 'data' => serialize(value) })
end