app/controllers/cancel_info_controller.rb
def update_cache_if_needed(hash = {})
      grade = hash[:grade].to_s

      cache = CancelInfo.find_by(grade: grade)
      if cache.nil? || Date.today - cache.updated_at.to_date >= 1
        CancelInfo.where(grade: grade).destroy_all
        json = get_cancel_info(grade: grade)
        json.each do |item|
          CancelInfo.create!(
            grade:       grade,
            type_str:    item['type_str'],
            date_str:    item['date_str'],
            date:        CancelInfo.parse_date_str(item['date_str']),
            altdate_str: item['altdate_str'],
            altdate:     CancelInfo.parse_date_str(item['altdate_str']),
            subject:     item['subject'],
            classroom:   item['classroom'],
            department:  item['department'],
            teacher:     item['teacher'],
            note:        item['note']
          )
        end
      end
    end
# Update cache if cache is empty, or cache's timestamp is before yesterday
Update cache if cache is empty, or cache's timestamp is before yesterday
Update cache if cache is empty or cache ' s timestamp is before yesterday
def update_cache_if_needed(hash = {})
      grade = hash[:grade].to_s
      cache = CancelInfo.find_by(grade: grade)
      if cache.nil? || Date.today - cache.updated_at.to_date >= 1
        CancelInfo.where(grade: grade).destroy_all
        json = get_cancel_info(grade: grade)
        json.each do |item|
          CancelInfo.create!(
            grade:       grade,
            type_str:    item['type_str'],
            date_str:    item['date_str'],
            date:        CancelInfo.parse_date_str(item['date_str']),
            altdate_str: item['altdate_str'],
            altdate:     CancelInfo.parse_date_str(item['altdate_str']),
            subject:     item['subject'],
            classroom:   item['classroom'],
            department:  item['department'],
            teacher:     item['teacher'],
            note:        item['note']
          )
        end
      end
    end
def update_cache_if_needed ( hash = { } ) grade = hash [ :grade ] . to_s cache = CancelInfo . find_by ( grade : grade ) if cache . nil? || Date . today - cache . updated_at . to_date >= 1 CancelInfo . where ( grade : grade ) . destroy_all json = get_cancel_info ( grade : grade ) json . each do | item | CancelInfo . create! ( grade : grade , type_str : item [ 'type_str' ] , date_str : item [ 'date_str' ] , date : CancelInfo . parse_date_str ( item [ 'date_str' ] ) , altdate_str : item [ 'altdate_str' ] , altdate : CancelInfo . parse_date_str ( item [ 'altdate_str' ] ) , subject : item [ 'subject' ] , classroom : item [ 'classroom' ] , department : item [ 'department' ] , teacher : item [ 'teacher' ] , note : item [ 'note' ] ) end end end
Update cache if cache is empty, or cache's timestamp is before yesterday
Update cache if cache is empty or cache ' s timestamp is before yesterday
hash