module Commons::Repositories::Catalogs::Concerns::ModelCachingExtention

Public Instance Methods

all_cached() click to toggle source

The first time you call Model.all_cached it will cache the collection, each consequent call will not fire the DB query

# File lib/commons/repositories/catalogs/concerns/model_caching_extention.rb, line 11
def all_cached
  Rails.cache.fetch("cached_#{self.class.name.underscore.to_s.gsub("/", "_")}") { @db_client.all.load }
end
clear_cache() click to toggle source

Making sure, that data is in consistent state by removing the cache everytime, the table is touched (eg some record is edited/created/destroyed etc).

# File lib/commons/repositories/catalogs/concerns/model_caching_extention.rb, line 19
def clear_cache
  Rails.cache.delete("cached_#{self.class.name.underscore.to_s.gsub("/", "_")}")
end