Last active
December 27, 2015 10:48
-
-
Save 23inhouse/7313582 to your computer and use it in GitHub Desktop.
Cache Digests
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
# cache digest talk | |
# | |
# There are 2 hard problems in computer science: caching, naming, and off-by-1 errors | |
# | |
# Gemfile | |
gem 'cache_digests' | |
# | |
# Models | |
# | |
class Winelist < ActiveRecord::Base | |
belongs_to :seller | |
has_many :product_listings, :dependent => :destroy | |
end | |
class ProductListing < ActiveRecord::Base | |
belongs_to :winelist, :touch => true | |
end | |
# | |
# ERB | |
# | |
# <% cache([subdomain_cache_key, @winelist]) do %> | |
# <% cache([subdomain_cache_key, @product_listing]) do %> | |
# <% end %> | |
# <% end %> | |
# | |
# Conditional caching | |
# | |
def cache_if(condition, object, &block) | |
if condition | |
cache(object, &block) | |
else | |
yield | |
end | |
end | |
# <%= simple_form_for(@winelist, :url => seller_winelist_url(@winelist)) do |form| %> | |
# <% cache_if(@winelist.errors.blank?, [@winelist, 'edit']) do %> | |
# <% end %> | |
# <% end %> | |
# | |
# Observers | |
# | |
# config/application | |
config.active_record.observers = :seller_observer, :winelist_observer | |
class SellerObserver < ActiveRecord::Observer | |
def after_save(seller) | |
if touch_winelists?(seller) | |
seller.product_listings.each(&:touch) | |
end | |
end | |
private | |
def touch_winelists?(seller) | |
interesction = seller.changes.keys & [ | |
'minimum_bottles_per_order', | |
'merchant_gateway', | |
'winery_website_wine_page', | |
'paypal_api_username', | |
'paypal_api_password', | |
'paypal_api_signature', | |
'card_access_username', | |
'card_access_password', | |
'disabled', | |
'show_cases_left' | |
] | |
interesction.size > 0 | |
end | |
end | |
# | |
# pre-caching | |
# | |
class WinelistObserver < ActiveRecord::Observer | |
def after_save(winelist) | |
pre_cache_winelist(winelist) | |
end | |
private | |
def pre_cache_winelist(winelist) | |
return if test_env? || development_env? | |
command = curl_url(winelists(winelist))+ ' &' | |
winelist.logger.info "\nsystem call #{command}\n\n" | |
system command | |
end | |
def curl_url(url) | |
"curl --insecure --head #{url} > /dev/null 2>&1" | |
end | |
def winelists(winelist) | |
seller = winelist.seller | |
options = { :host => DEFAULT_HOST, :protocol => 'https' } | |
Rails.application.routes.url_helpers.winelists_url(seller.subdomain, winelist.anchor, options) | |
end | |
end | |
# | |
# Problems | |
# | |
# 2 Winelists - 'Retail' and 'VIP' | |
# Currently assigned to Retail | |
# [ X ] - Bob | |
# [ ] - Don | |
# [ X ] - Sally | |
# [ reassign to VIP ] | |
class Winelist < ActiveRecord::Base | |
has_many :memberships | |
end | |
class Membership < ActiveRecord::Base | |
belongs_to :winelist, :touch => true | |
end | |
# try 1 | |
def reassign | |
winelist = Winelist.where(:name => params[:name]).first | |
Membership.update_all({:winelist_id => winelist.id}, {:id => params[:membership_ids]}) | |
end | |
# try 2 | |
def reassign | |
winelist = Winelist.where(:name => params[:name]).first | |
Membership.where(:id => params[:membership_ids]).all.each { |m| m.update_attributes(:winelist_id => winelist.id) } | |
end | |
# try 3 | |
def reassign | |
winelists = Membership.where(:id => params[:membership_ids]).all.collect(&:winelist).uniq | |
winelists.each(&:touch) | |
winelist = Winelist.where(:name => params[:name]).first | |
winelist.touch | |
Membership.update_all({:winelist_id => winelist.id}, {:id => params[:membership_ids]}) | |
Membership.where(:id => params[:membership_ids]).all.each(&:touch) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment