Skip to content

Instantly share code, notes, and snippets.

@nhocki
Created July 27, 2015 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhocki/dc6a5db52e953ab7b188 to your computer and use it in GitHub Desktop.
Save nhocki/dc6a5db52e953ab7b188 to your computer and use it in GitHub Desktop.
# app/models/puntos.rb
class Puntos < AR::Base
PUNTOS_ALL_KEY = "all_puntos".freeze
after_save :expire_cache
after_destroy :expire_cache
# Guarda un arreglo de hashes con el JSON pa mandar.
def self.all_cached
Rails.cache.fetch(PUNTOS_ALL_KEY) do
self.all.map {|p| json_del_punto(p) }
end
end
private
# Cada que eliminan un punto o agregan un punto, mata el cache.
def expire_cache
Rails.cache.delete(PUNTOS_ALL_KEY)
end
end
# app/controllers/puntos_controller.rb
class PuntosController < AppController
def index
render json: Puntos.all_cached
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment