dg iz updating authors cache on creation of a post
This commit is contained in:
parent
dfd65c4b4b
commit
c2bb483b21
3 changed files with 84 additions and 7 deletions
|
|
@ -32,6 +32,8 @@ class Post < ActiveRecord::Base
|
|||
|
||||
validates :guid, :uniqueness => true
|
||||
|
||||
after_create :cache_for_author
|
||||
|
||||
#scopes
|
||||
scope :all_public, where(:public => true, :pending => false)
|
||||
scope :includes_for_a_stream, includes({:author => :profile}, :mentions => {:person => :profile}) #note should include root and photos, but i think those are both on status_message
|
||||
|
|
@ -156,4 +158,19 @@ class Post < ActiveRecord::Base
|
|||
self.class.where(:id => self.id).
|
||||
update_all(:comments_count => self.comments.count)
|
||||
end
|
||||
|
||||
# @return [Boolean]
|
||||
def cache_for_author
|
||||
if self.should_cache_for_author?
|
||||
cache = RedisCache.new(self.author.owner, 'created_at')
|
||||
cache.add(self.created_at.to_i, self.id)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
# @return [Boolean]
|
||||
def should_cache_for_author?
|
||||
self.triggers_caching? && RedisCache.configured? &&
|
||||
RedisCache.acceptable_types.include?(self.type) && user = self.author.owner
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -10,11 +10,4 @@ describe TagStream do
|
|||
describe 'shared behaviors' do
|
||||
it_should_behave_like 'it is a stream'
|
||||
end
|
||||
|
||||
|
||||
describe 'posts' do
|
||||
it 'explains the query' do
|
||||
puts @stream.posts.to_sql
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -137,4 +137,71 @@ describe Post do
|
|||
Post.new.triggers_caching?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe "after_create" do
|
||||
it "calls cache_for_author only on create" do
|
||||
post = Factory.build(:status_message, :author => bob.person)
|
||||
post.should_receive(:cache_for_author).once
|
||||
post.save
|
||||
post.save
|
||||
end
|
||||
end
|
||||
|
||||
describe '#cache_for_author' do
|
||||
before do
|
||||
@post = Factory.build(:status_message, :author => bob.person)
|
||||
@post.stub(:should_cache_for_author?).and_return(true)
|
||||
end
|
||||
|
||||
it 'caches with valid conditions' do
|
||||
cache = mock.as_null_object
|
||||
RedisCache.should_receive(:new).and_return(cache)
|
||||
cache.should_receive(:add)
|
||||
@post.cache_for_author
|
||||
end
|
||||
|
||||
it 'does nothing if should not cache' do
|
||||
@post.stub(:should_cache_for_author?).and_return(false)
|
||||
RedisCache.should_not_receive(:new)
|
||||
@post.cache_for_author
|
||||
end
|
||||
end
|
||||
|
||||
describe "#should_cache_for_author?" do
|
||||
before do
|
||||
@post = Factory.build(:status_message, :author => bob.person)
|
||||
RedisCache.stub(:configured?).and_return(true)
|
||||
RedisCache.stub(:acceptable_types).and_return(['StatusMessage'])
|
||||
@post.stub(:triggers_caching?).and_return(true)
|
||||
end
|
||||
|
||||
it 'returns true under valid conditions' do
|
||||
@post.should_cache_for_author?.should be_true
|
||||
end
|
||||
|
||||
it 'does not cache if the author is not a local user' do
|
||||
@post.author = Factory(:person)
|
||||
@post.should_cache_for_author?.should be_false
|
||||
end
|
||||
|
||||
it 'does not cache if the cache is not configured' do
|
||||
RedisCache.stub(:configured?).and_return(false)
|
||||
@post.should_cache_for_author?.should be_false
|
||||
end
|
||||
|
||||
it 'does not cache if the object does not triggers caching' do
|
||||
@post.stub(:triggers_caching?).and_return(false)
|
||||
@post.should_cache_for_author?.should be_false
|
||||
end
|
||||
|
||||
it 'does not cache if the object is not of an acceptable cache type' do
|
||||
@post.stub(:type).and_return("Photo")
|
||||
@post.should_cache_for_author?.should be_false
|
||||
end
|
||||
end
|
||||
|
||||
#user exists,
|
||||
# cache configured,
|
||||
# triggers caching,
|
||||
# of the cachable type
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue