vol1ura/Sat_9am_5km

View on GitHub
app/services/clear_cache.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# frozen_string_literal: true

# Clears rails cache
class ClearCache < ApplicationService
  CLEAR_TIME_KEY = 'cache_clear_time'
  TIME_THRESHOLD = 5.minutes.freeze

  def call
    return false if cache_clear_time && cache_clear_time > TIME_THRESHOLD.ago

    Rails.cache.clear
    Rails.cache.write(CLEAR_TIME_KEY, Time.current)
    true
  end

  private

  def cache_clear_time
    @cache_clear_time ||= Rails.cache.read(CLEAR_TIME_KEY)
  end
end