move item cache sweeping and flex syncing to background tasks

This commit is contained in:
Emi Matchu 2013-12-09 00:12:05 -06:00
parent 4144b4dc74
commit 728ff60c5f
10 changed files with 73 additions and 24 deletions

View file

@ -37,7 +37,7 @@ GIT
GIT
remote: git://github.com/matchu/flex.git
revision: d62f508f795ecdbb383f406865daa72368b43ba5
revision: 6d7e3fd6e2845ae978545be67b912323888d1ebb
specs:
flex (0.4.1)
multi_json (~> 1.3.4)
@ -186,7 +186,7 @@ GEM
patron (0.4.18)
polyglot (0.3.3)
progressbar (0.11.0)
prompter (0.1.5)
prompter (0.1.6)
dye (>= 0.1.1)
yard (>= 0.6.3)
rack (1.4.5)
@ -292,7 +292,7 @@ GEM
activesupport (>= 2.3.4)
chronic (~> 0.6.3)
will_paginate (3.0.4)
yard (0.8.5.2)
yard (0.8.7.3)
PLATFORMS
ruby

View file

@ -41,7 +41,8 @@ class Item < ActiveRecord::Base
scope :with_closet_hangers, joins(:closet_hangers)
flex.sync self
# Syncing is now handled in background tasks, created in the ItemObserver.
flex.sync self, callbacks: []
def flex_source
indexed_attributes = {

View file

@ -0,0 +1,21 @@
class Item
class CreateTask
extend FragmentExpiration
TIMEOUT_IN_SECONDS = 10
@queue = :item_create
def self.perform(id)
Timeout::timeout(TIMEOUT_IN_SECONDS) do
Item.find(id).flex.sync
expire_newest_items
end
end
def self.expire_newest_items
expire_fragment_in_all_locales('outfits#new newest_items')
expire_fragment_in_all_locales('items#index newest_items')
end
end
end

View file

@ -0,0 +1,17 @@
class Item
class DestroyTask
extend FragmentExpiration
TIMEOUT_IN_SECONDS = 10
@queue = :item_destroy
def self.perform(id)
Timeout::timeout(TIMEOUT_IN_SECONDS) do
Item.find(id).flex.sync
# TODO: it's kinda ugly to reach across classes like this
CreateTask.expire_newest_items
end
end
end
end

View file

@ -0,0 +1,26 @@
class Item
class UpdateTask
extend FragmentExpiration
TIMEOUT_IN_SECONDS = 10
@queue = :item_update
def self.perform(id)
Timeout::timeout(TIMEOUT_IN_SECONDS) do
item = Item.find(id)
expire_cache_for(item)
item.flex.sync
end
end
private
def self.expire_cache_for(item)
expire_fragment_in_all_locales("items/#{item.id}#item_link_partial")
expire_fragment_in_all_locales("items/#{item.id} header")
expire_fragment_in_all_locales("items/#{item.id} info")
expire_key_in_all_locales("items/#{item.id}#as_json")
end
end
end

View file

@ -2,31 +2,15 @@ class ItemObserver < ActionController::Caching::Sweeper
include FragmentExpiration
def after_create(item)
Rails.logger.debug "Item #{item.id} was just created"
expire_newest_items
Resque.enqueue(Item::CreateTask, item.id)
end
def after_update(item)
Rails.logger.debug "Item #{item.id} was just updated"
expire_cache_for(item)
# CONSIDER: can pass item.changes.keys in to choose which caches to expire
Resque.enqueue(Item::UpdateTask, item.id)
end
def after_destroy(item)
Rails.logger.debug "Item #{item.id} was just destroyed"
expire_cache_for(item)
end
private
def expire_cache_for(item)
expire_fragment_in_all_locales("items/#{item.id}#item_link_partial")
expire_fragment_in_all_locales("items/#{item.id} header")
expire_fragment_in_all_locales("items/#{item.id} info")
expire_key_in_all_locales("items/#{item.id}#as_json")
end
def expire_newest_items
expire_fragment_in_all_locales('outfits#new newest_items')
expire_fragment_in_all_locales('items#index newest_items')
Resque.enqueue(Item::DestroyTask, item.id)
end
end

Binary file not shown.

BIN
vendor/cache/prompter-0.1.6.gem vendored Normal file

Binary file not shown.

Binary file not shown.

BIN
vendor/cache/yard-0.8.7.3.gem vendored Normal file

Binary file not shown.