Skip to content

Instantly share code, notes, and snippets.

@r38y
Forked from robotmay/application_controller.rb
Created May 17, 2012 18:21
Show Gist options
  • Save r38y/2720735 to your computer and use it in GitHub Desktop.
Save r38y/2720735 to your computer and use it in GitHub Desktop.
Examples of low level caching
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter do
@categories = Rails.cache.fetch("global/categories", expires_in: 10.minutes) do
Category.where("posts_count > 0").all
end
end
end
class Place < ActiveRecord::Base
def flickr_place
flickr = FlickRaw::Flickr.new
@flickr_place ||= Rails.cache.fetch("places/#{id}-#{updated_at}/flickr/place_id", expires_in: 7.days) do
if latitude? && longitude?
begin
places = flickr.places.findByLatLon(lat: latitude, lon: longitude)
places.try(:first)
rescue Exception => e
logger.info "Exception occurred fetching Flickr place ID: #{e.to_s}"
nil
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment