Class: ActiveSupport::Cache::MongoIdCacheStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::MongoIdCacheStore
- Defined in:
- lib/active_support/cache/mongo_id_cache_store.rb
Instance Method Summary collapse
- #cleanup ⇒ Object
- #clear ⇒ Object
- #delete_entry(key, options = nil) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #deserialize(value) ⇒ Object
-
#initialize(options = {}) ⇒ MongoIdCacheStore
constructor
A new instance of MongoIdCacheStore.
- #read_entry(key, options) ⇒ Object
- #serialize(value) ⇒ Object
- #write_entry(key, entry, raw: false, **options) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ MongoIdCacheStore
8 9 10 11 12 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 8 def initialize( = {}) [:collection_name] ||= :rails_caches [:expires_in] ||= 86400 super() end |
Instance Method Details
#cleanup ⇒ Object
18 19 20 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 18 def cleanup collection.find(expires_at: {'$lt' => Time.now.utc.to_f}).delete_many end |
#clear ⇒ Object
14 15 16 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 14 def clear collection.delete_many end |
#delete_entry(key, options = nil) ⇒ Object
40 41 42 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 40 def delete_entry(key, = nil) collection.find(_id: key).delete_one end |
#delete_matched(matcher, options = nil) ⇒ Object
44 45 46 47 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 44 def delete_matched(matcher, =nil) = () collection.find(_id: key_matcher(matcher, )).delete_many end |
#deserialize(value) ⇒ Object
49 50 51 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 49 def deserialize(value) Marshal.load(value) rescue value end |
#read_entry(key, options) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 22 def read_entry(key, ) query = {_id: key, expires_at: {'$gt': Time.now.to_f}} doc = collection.find(query).first return nil if doc.nil? entry = doc['raw'] ? doc['value'] : deserialize(doc['value'].data) entry.is_a?(Entry) ? entry : Entry.new(entry) end |
#serialize(value) ⇒ Object
53 54 55 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 53 def serialize(value) BSON::Binary.new(Marshal.dump(value)) end |
#write_entry(key, entry, raw: false, **options) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/active_support/cache/mongo_id_cache_store.rb', line 30 def write_entry(key, entry, raw: false, **) query = {_id: key} updates = {'$set': { value: raw ? entry.value : serialize(entry), expires_at: entry.expires_at, raw: raw }} collection.update_one(query, updates, upsert: true) end |