From 03955a0e9a50b0ef7ddc5780ab1a179f140b5ee3 Mon Sep 17 00:00:00 2001 From: Muhammad Denaw Date: Wed, 31 May 2023 16:59:24 +0700 Subject: [PATCH] [CACHE] GET Event and Location --- app/controllers/api/events_controller.rb | 2 +- app/models/event.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/events_controller.rb b/app/controllers/api/events_controller.rb index df66bf4..dc73d42 100644 --- a/app/controllers/api/events_controller.rb +++ b/app/controllers/api/events_controller.rb @@ -3,7 +3,7 @@ class EventsController < ApiController load_and_authorize_resource def show - render json: serialized_event + render json: serialized_event if stale?(@event) end def create diff --git a/app/models/event.rb b/app/models/event.rb index 9e6dcb5..3fc0673 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -47,6 +47,8 @@ def calculate_midpoint_location! private def get_location(latitude:, longitude:) - Location.find_or_initialize_by(latitude:, longitude:) + Rails.cache.fetch("location-#{latitude}-#{longitude}", expires_in: 2.days) do + Location.find_or_initialize_by(latitude:, longitude:) + end end end