-
-
Save r38y/2720735 to your computer and use it in GitHub Desktop.
Examples of low level caching
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
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 |
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
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