Module: MongoStore::Cache::Rails3

Defined in:
lib/active_support/cache/mongo_store.rb

Instance Method Summary collapse

Instance Method Details

#delete_entry(key, options = nil) ⇒ Object



87
88
89
# File 'lib/active_support/cache/mongo_store.rb', line 87

def delete_entry(key, options=nil)
  collection.remove({'_id' => key})
end

#delete_matched(pattern, options = nil) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/active_support/cache/mongo_store.rb', line 90

def delete_matched(pattern, options=nil)
  options = merged_options(options)
  instrument(:delete_matched, pattern.inspect) do
    matcher = key_matcher(pattern, options)  # Handles namespacing with regexes
    delete_entry(matcher, options) 
  end
end

#read_entry(key, options = nil) ⇒ Object



83
84
85
86
# File 'lib/active_support/cache/mongo_store.rb', line 83

def read_entry(key, options=nil)
  doc = collection.find_one('_id' => key, 'expires' => {'$gt' => Time.now})
  ActiveSupport::Cache::Entry.new(doc['value']) if doc
end

#write_entry(key, entry, options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/active_support/cache/mongo_store.rb', line 73

def write_entry(key, entry, options)
  expires = Time.now + options[:expires_in]
  value = entry.value
  value = value.to_mongo if value.respond_to? :to_mongo
  begin
    collection.update({'_id' => key}, {'$set' => {'value' => value, 'expires' => expires}}, :upsert => true)
  rescue BSON::InvalidDocument
    value = value.to_s and retry unless value.is_a? String
  end
end