class Para::Cache::DatabaseStore
Public Instance Methods
clear()
click to toggle source
# File lib/para/cache/database_store.rb, line 19 def clear Item.delete_all end
delete_entry(key, options)
click to toggle source
# File lib/para/cache/database_store.rb, line 23 def delete_entry(key, options) Item.delete_all(key: key) end
read_entry(key, options={})
click to toggle source
# File lib/para/cache/database_store.rb, line 27 def read_entry(key, options={}) RequestStore.store[cache_key_for(key)] ||= Item.find_by(key: key) end
write_entry(key, entry, options)
click to toggle source
# File lib/para/cache/database_store.rb, line 31 def write_entry(key, entry, options) RequestStore.store[cache_key_for(key)] ||= Item.where(key: key).first_or_initialize item = RequestStore.store[cache_key_for(key)] options = options.clone.symbolize_keys item.value = entry.value item.expires_at = options[:expires_in].try(:since) # Save chace item in its own thread, to get a clean # ActiveRecord::Base.connection. # # It allows cache to be saved inside transactions but without waiting # transactions to be committed to commit chache changes. # # This is needed for checking progress of long running tasks, like # imports, that use the cache inside transactions but need other # processes to get their state in real time. # Thread.new do item.save! end.join # Ensure cached item in RequestStore is up to date RequestStore.store[cache_key_for(key)] = item rescue ActiveRecord::RecordNotUnique ensure clear_expired_keys end
Private Instance Methods
cache_key_for(key)
click to toggle source
# File lib/para/cache/database_store.rb, line 79 def cache_key_for(key) ['para.cache.database_store', key].join('.') end
clear_expired_keys()
click to toggle source
# File lib/para/cache/database_store.rb, line 63 def clear_expired_keys if Item.count > Para.config.database_cache_store_max_items remove_expired_items remove_old_items end end
remove_expired_items()
click to toggle source
# File lib/para/cache/database_store.rb, line 70 def remove_expired_items Item.where("expires_at < ?", Time.now).delete_all end
remove_old_items()
click to toggle source
# File lib/para/cache/database_store.rb, line 74 def remove_old_items count = (Item.count - Para.config.database_cache_store_max_items) Item.order('updated_at ASC').limit(count).delete_all if count > 0 end