Class: ActiveSupport::Cache::MongoDBCacheStore
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::MongoDBCacheStore
- Defined in:
- lib/active_support/cache/mongo_db_cache_store.rb
Instance Method Summary collapse
- #cleanup ⇒ Object
- #clear ⇒ Object
- #collection ⇒ Object
- #delete_entry(key, options = nil) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #deserialize(value) ⇒ Object
-
#initialize(options = {}) ⇒ MongoDBCacheStore
constructor
A new instance of MongoDBCacheStore.
- #read_entry(key, options) ⇒ Object
- #serialize(value) ⇒ Object
- #write_entry(key, entry, raw: false, **options) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ MongoDBCacheStore
Returns a new instance of MongoDBCacheStore.
9 10 11 12 13 14 15 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 9 def initialize( = {}) [:db_uri] ||= 'mongodb://127.0.0.1:27017/rails_cache_store' [:collection_name] ||= :rails_caches [:expires_in] ||= 86400 super() @client = Mongo::Client.new([:db_uri]) end |
Instance Method Details
#cleanup ⇒ Object
21 22 23 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 21 def cleanup collection.find(expires_at: {'$lt' => Time.now.utc.to_f}).delete_many end |
#clear ⇒ Object
17 18 19 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 17 def clear collection.delete_many end |
#collection ⇒ Object
60 61 62 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 60 def collection @client[[:collection_name]] end |
#delete_entry(key, options = nil) ⇒ Object
43 44 45 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 43 def delete_entry(key, = nil) collection.find(_id: key).delete_one end |
#delete_matched(matcher, options = nil) ⇒ Object
47 48 49 50 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 47 def delete_matched(matcher, =nil) = () collection.find(_id: key_matcher(matcher, )).delete_many end |
#deserialize(value) ⇒ Object
52 53 54 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 52 def deserialize(value) Marshal.load(value) rescue value end |
#read_entry(key, options) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 25 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
56 57 58 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 56 def serialize(value) BSON::Binary.new(Marshal.dump(value)) end |
#write_entry(key, entry, raw: false, **options) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/active_support/cache/mongo_db_cache_store.rb', line 33 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 |