Sha256: ce4d2fb15cd59bec74bf1592d2e1c6323b5a1deb4822fc3b28de7bda5974895a

Contents?: true

Size: 876 Bytes

Versions: 7

Compression:

Stored size: 876 Bytes

Contents

module Commons
  module Repositories
    module Catalogs
      module Concerns
        module ModelCachingExtention
          extend ActiveSupport::Concern

          included do
            # The first time you call Model.all_cached it will cache the collection,
            # each consequent call will not fire the DB query
            def all_cached
              Rails.cache.fetch("cached_#{self.class.name.underscore.to_s.gsub("/", "_")}") { @db_client.all.load }
            end

            private

            # 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).
            def clear_cache
              Rails.cache.delete("cached_#{self.class.name.underscore.to_s.gsub("/", "_")}")
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
commons_yellowme-0.16.0 lib/commons/repositories/catalogs/concerns/model_caching_extention.rb
commons_yellowme-0.15.0 lib/commons/repositories/catalogs/concerns/model_caching_extention.rb
commons_yellowme-0.12.0 lib/commons/repositories/catalogs/concerns/model_caching_extention.rb
commons_yellowme-0.11.3 lib/commons/repositories/catalogs/concerns/model_caching_extention.rb
commons_yellowme-0.11.2 lib/commons/repositories/catalogs/concerns/model_caching_extention.rb
commons_yellowme-0.11.1 lib/commons/repositories/catalogs/concerns/model_caching_extention.rb
commons_yellowme-0.11.0 lib/commons/repositories/catalogs/concerns/model_caching_extention.rb