Skip to content

Instantly share code, notes, and snippets.

@omarqureshi
Created April 5, 2012 23:10
Show Gist options
  • Save omarqureshi/2314933 to your computer and use it in GitHub Desktop.
Save omarqureshi/2314933 to your computer and use it in GitHub Desktop.
diff --git a/app/models/topic.rb b/app/models/topic.rb
index c27a174..bcd9d5a 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -117,15 +117,55 @@ class Topic < ActiveRecord::Base
replies.active.count
end
+ # Public: Used by views to determine whether the index page needs to
+ # be updated.
+ #
+ # When any topic or post is updated the old cache will just be
+ # unused and eventually be removed by memcached.
+ #
+ # This will also handle users specific needs with regards to the
+ # number of topics to display per page and what page the user is
+ # currently visiting.
+ #
+ # current_site - a symbol to say whether the site is :default or
+ # :medical
+ # current_page - params[:page] from the controller, this should be
+ # an integer or a string representing an integer.
+ # per_page - params[:per_page] from the controller, this should
+
def self.index_cache_path(current_site, current_page, per_page)
"topics/?page=#{current_page}&per_page=#{per_page}&current_site=#{current_site}##{cache_timestamp}"
end
+ # Public: Used by views to determine whether the show page needs to
+ # be updated.
+ #
+ # When the topic or any of its posts are updated the old cache will
+ # just be unused and eventually be removed by memcached.
+ #
+ # This will also handle users specific needs with regards to the
+ # order of posts, how many posts to display on the page and the
+ # current page that the user is visiting.
+ #
+ # current_page - params[:page] from the controller, this should be
+ # an integer or a string representing an integer.
+ # per_page - params[:per_page] from the controller, this should
+ # be an integer or a string representing an integer.
+ # posts_order - whether the order of post retrieval is ascending or
+ # descending. Should be a string which is either
+ # 'ASC' or 'DESC'
+ # Returns a unique string for each topic / per page / current page /
+ # order combination
+
def show_cache_path(current_page, per_page, posts_order)
# TODO: check if we need is_monitored?
"forums/#{forum.id}/topics/#{id}?page=#{current_page}&per_page=#{per_page}&order=#{posts_order}##{updated_at.to_i}"
end
+ # Public: Changed every time a topic or post is updated
+ #
+ # Returns an integer
+
def self.cache_timestamp
first(:order => "updated_at desc").updated_at.to_i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment