Class: MongoStore::Cache

Inherits:
ActiveSupport::Cache::Store
  • Object
show all
Defined in:
lib/mongo_store/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, options = {}) ⇒ Cache

Returns a new instance of Cache.



26
27
28
# File 'lib/mongo_store/cache.rb', line 26

def initialize(collection, options={})
  @collection, @options = collection, options || {}
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



24
25
26
# File 'lib/mongo_store/cache.rb', line 24

def collection
  @collection
end

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/mongo_store/cache.rb', line 24

def options
  @options
end

Instance Method Details

#clear(options = nil) ⇒ Object



52
53
54
# File 'lib/mongo_store/cache.rb', line 52

def clear(options=nil)
  rescue_connection_failure { @collection.remove }
end

#delete_entry(key, options) ⇒ Object

we don’t get any info from mongo driver if delete actually deleted anything. this will always return true.



47
48
49
50
# File 'lib/mongo_store/cache.rb', line 47

def delete_entry(key, options)
  rescue_connection_failure { @collection.remove({:_id => key.to_s}) }
  true
end

#expires_inObject



30
31
32
# File 'lib/mongo_store/cache.rb', line 30

def expires_in
  @expires_in ||= options[:expires_in] || 1.month
end

#read_entry(key, options) ⇒ Object



40
41
42
43
44
# File 'lib/mongo_store/cache.rb', line 40

def read_entry(key, options)
  entry = nil
  rescue_connection_failure { entry = @collection.find_one(:_id => key.to_s) }
  entry ? Marshal.load(entry['value'].to_s) : nil
end

#write_entry(key, entry, options) ⇒ Object



34
35
36
37
38
# File 'lib/mongo_store/cache.rb', line 34

def write_entry(key, entry, options)
  doc = {:_id => key.to_s, :value => BSON::Binary.new(Marshal.dump(entry))}
  rescue_connection_failure { @collection.save(doc) }
  true
end