<commit_before>class ProjectsController < ApplicationController
  include ApiHelper
  before_filter :check_for_and_cache_response

  def index
    muni_id = filter.fetch(:municipalities) { nil }
    subr_id = filter.fetch(:subregions)     { nil }

    if muni_id
      @projects = Project.municipality(muni_id).page(page_number).per(per_page)
    elsif subr_id
      @projects = Project.subregion(subr_id).page(page_number).per(per_page)
    else
      @projects = Project.all.page(page_number).per(per_page)
    end

    json = JSONAPI::Serializer.serialize(@projects, include: includes, is_collection: true, context: {is_collection: true})
    json[:links] = paginate(@projects)
    ImageCache.redis.hset :responses, @_request.env["REQUEST_URI"], JSON.generate( json )
    render json: json
  end

  def show
    @project = Project.find params[:id]
    json = JSONAPI::Serializer.serialize(@project, include: includes, is_collection: false)
    ImageCache.redis.hset :responses, @_request.env["REQUEST_URI"], JSON.generate( json )
    render json: json
  end

  private
    
    def check_for_and_cache_response
      response = ImageCache.redis.hget :responses, @_request.env["REQUEST_URI"]
      if response
        render json: JSON.parse( response )
        return false
      end
    end

end<commit_msg>Expire a request after 2 hours, in case projects change.<commit_after>class ProjectsController < ApplicationController
  include ApiHelper
  before_filter :check_for_and_cache_response

  def index
    muni_id = filter.fetch(:municipalities) { nil }
    subr_id = filter.fetch(:subregions)     { nil }

    if muni_id
      @projects = Project.municipality(muni_id).page(page_number).per(per_page)
    elsif subr_id
      @projects = Project.subregion(subr_id).page(page_number).per(per_page)
    else
      @projects = Project.all.page(page_number).per(per_page)
    end

    json = JSONAPI::Serializer.serialize(@projects, include: includes, is_collection: true, context: {is_collection: true})
    json[:links] = paginate(@projects)
    ImageCache.redis.hset :responses, @_request.env["REQUEST_URI"], JSON.generate( json )
    render json: json
  end

  def show
    @project = Project.find params[:id]
    json = JSONAPI::Serializer.serialize(@project, include: includes, is_collection: false)
    ImageCache.redis.hset :responses, @_request.env["REQUEST_URI"], JSON.generate( json )
    render json: json
  end

  private

    CACHE_LOCK_DELAY = 2.hour.to_i # seconds
    
    def check_for_and_cache_response
      check_for_or_reset_cache_lock
      response = ImageCache.redis.hget :responses, @_request.env["REQUEST_URI"]
      if response
        json = JSON.parse( response )
        json[:meta] = { cache_expires: ImageCache.redis.get(:cache_lock), time: Time.now }
        render json: json
        return false
      end
    end

    def check_for_or_reset_cache_lock
      unless ImageCache.redis.get :cache_lock
        ImageCache.redis.set    :cache_lock, "#{CACHE_LOCK_DELAY.seconds.from_now}"
        ImageCache.redis.expire :cache_lock, CACHE_LOCK_DELAY
        # Clear the responses cache
        ImageCache.redis.hkeys(:responses).each {|k|
          ImageCache.redis.del :responses, k
        }
      end
    end

end
{'fraction_non_alphanumeric': 0.09996924023377422, 'fraction_numerical': 0.0006151953245155337, 'mean_word_length': 3.5355648535564854, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '510072', 'n_tokens_mistral': 1035, 'n_tokens_neox': 1015, 'n_words': 211}