Created
July 27, 2015 01:02
-
-
Save nhocki/dc6a5db52e953ab7b188 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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