Sha256: 9406f3d479a5f297b01ab98717235045791c2b89d333bdd8245c08b46f6acc74

Contents?: true

Size: 1.54 KB

Versions: 10

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true
# Helper methods for caching for monitoring status.
module QaServer
  class CacheExpiryService
    class << self
      # @return [Float] number of seconds until cache should expire
      def cache_expiry
        cache_expires_at - QaServer::TimeService.current_time
      end

      def end_of_hour_expiry
        ct = QaServer::TimeService.current_time
        ct.end_of_hour - ct
      end

      # @param key [String] cache key
      # @param force [Boolean] if true, forces cache to regenerate by returning true; otherwise, uses cache expiry to determine whether cache has expired
      # @return [Boolean] true if cache has expired or is being forced to expire
      def cache_expired?(key:, force:, next_expiry:)
        # will return true only if the full expiry has passed or force was requested
        force = Rails.cache.fetch(key, expires_in: 5.minutes, race_condition_ttl: 30.seconds, force: force) { true }
        # reset cache so it will next expired at expected time
        Rails.cache.fetch(key, expires_in: next_expiry, race_condition_ttl: 30.seconds, force: true) { false }
        force
      end

    private

      # @return [ActiveSupport::TimeWithZone] DateTime at which cache should expire
      def cache_expires_at
        offset = QaServer.config.hour_offset_to_expire_cache
        offset_time = QaServer::TimeService.current_time
        offset_time = offset_time.tomorrow unless (offset_time + 5.minutes).hour < offset
        offset_time.beginning_of_day + offset.hours - 5.minutes
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
qa_server-8.0.1 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.9.2 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.9.1 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.9.0 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.8.0 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.7.1 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.7.0 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.6.0 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.5.1 app/cache_processors/qa_server/cache_expiry_service.rb
qa_server-7.5.0 app/cache_processors/qa_server/cache_expiry_service.rb