I had a question about caching and cache sweepers. I understand that
cache behaves much like a filter and an observer, watching actions
and make appropriate expirations when changes occur. But when you
have an action that is dependent on parameters derived from a URL,
such as a date based URL. The caching mechanism misses on these
actions. I know you can just wipe all the files using ruby''s
delete_all method but I don''t see how to derive a pattern for this,
when the actual edits are not made at the same URL as the cache file
(e.g. Admin Area). And deleting all the cache files can wipe the
performance and defeats the purpose of cache in the first place.
I don''t mean to over-complicate things but the caching functionality
is probably one of the top features in Rails and really would like to
get a better explanation of it beyond the Agile Book, which provides
an excellent jumping off point and the source code isn''t quite giving
me the info, or I''m not seeing it. I have code samples below. Thanks
in advanced, and once I get a grip on caching I''ll do a WIKI entry
and a post on my site about caching, because for some reason no one
has ever touched on this there
# Sweeper Action Responsibile for Expirations; Called on Save,
Update, and Create
def sweep_cache(record)
expire_page(:controller => "article",
:action => %w
(:index, :category, :user, :archive, :category_archive, :user_archive),
:id => record.id)
# I thought this would work but it seems as thought it still misses
# I''m guessing I need to do something with IO? This Writes the path
incorrectly anyway
## host.com/article?year=2005 blah blah blah
# Beside it couldn''t be that easy even in Ruby
expire_page(:controller => "article/"+params[:year]
+''/''+params[:month]+''/''+params[:day]+''/''+params[:title])
end
# Actual Controller Actions that makes the date calls (edited for
brevity)
def by_date
@article = Article.find_by_date(params[:year], params[:month],
params[:day])
render :action => ''index''
end
def permalink
@article = Article.get_by_permalink(params[:year], params
[:month], params[:day], params[:title])
render :action => ''post''
end
# Model with Queries related to Controller
def self.find_by_date(yr, mo = nil, day = nil)
date,tom = self.set_date(yr, mo, day)
Article.find(:all, :conditions => [" articles.created_at
BETWEEN ? AND ?", date, tom],
:order =>
"articles.created_at DESC")
end
# set_title() is a temp method to handle the stripping of non-URI
chars (not included)
def self.get_by_permalink(yr, mo, day, pat)
self.find_by_date(yr, mo, day).find { |art| art.set_title
(art.title.downcase) == pat }
end
Thanks Again
J Brien | HybridIndie Productions | hybridindie.com |
iam-QLwuMy0vUAhCl22TKe+ceQ@public.gmane.org
Posted Web Engine - rubyforge.org/projects/posted