Sha256: 3132ef6f981381d10f8f78a02d177d0e5d3620621378f5f0ed34430f2b9821b4

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module Aqila
  module Cache
    module Methods
      extend ActiveSupport::Concern

      included do
        def self.cache_ttl=(cache_ttl)
          @cache_ttl = cache_ttl
        end

        def self.cache_ttl
          @cache_ttl || Aqila::Cache.configuration.default_ttl
        end

        def self.update_log_table
          Aqila::Cache::TableLastChange
            .find_or_initialize_by(table_name: table_name)
            .update(updated_at: Time.current)
        end

        def self.stale?(controller:)
          check_ttl

          controller.stale?(last_modified: Aqila::Cache::TableLastChange.for(model: self))
        end

        def self.check_ttl
          return unless cache_ttl

          log_table = Aqila::Cache::TableLastChange.for(model: self)
          return update_log_table unless log_table

          expired = (Time.current - log_table) > cache_ttl
          update_log_table if expired
        end

        private_class_method :check_ttl
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aqila-cache-0.2.0 lib/aqila/cache/methods.rb