Class: Para::Cache::DatabaseStore
- Inherits:
-
ActiveSupport::Cache::Store
- Object
- ActiveSupport::Cache::Store
- Para::Cache::DatabaseStore
- Defined in:
- lib/para/cache/database_store.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #delete_entry(key, options) ⇒ Object
- #read_entry(key, options = {}) ⇒ Object
- #write_entry(key, entry, options) ⇒ Object
Instance Method Details
#clear ⇒ Object
19 20 21 |
# File 'lib/para/cache/database_store.rb', line 19 def clear Item.delete_all end |
#delete_entry(key, options) ⇒ Object
23 24 25 |
# File 'lib/para/cache/database_store.rb', line 23 def delete_entry(key, ) Item.delete_all(key: key) end |
#read_entry(key, options = {}) ⇒ Object
27 28 29 |
# File 'lib/para/cache/database_store.rb', line 27 def read_entry(key, ={}) RequestStore.store[cache_key_for(key)] ||= Item.find_by(key: key) end |
#write_entry(key, entry, options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/para/cache/database_store.rb', line 31 def write_entry(key, entry, ) RequestStore.store[cache_key_for(key)] ||= Item.where(key: key).first_or_initialize item = RequestStore.store[cache_key_for(key)] = .clone.symbolize_keys item.value = entry.value item.expires_at = [:expires_in].try(:since) item.save! # Ensure cached item in RequestStore is up to date RequestStore.store[cache_key_for(key)] = item rescue ActiveRecord::RecordNotUnique ensure clear_expired_keys end |