changed the default order in prep_opts, passing in opts to cache population

This commit is contained in:
Ilya Zhitomirskiy 2011-10-06 17:04:39 -07:00
parent eed0db177d
commit 7572eaaf0d
4 changed files with 34 additions and 22 deletions

View file

@ -34,17 +34,23 @@ class RedisCache
post_ids[0...limit]
end
def ensure_populated!
self.repopulate! unless cache_exists?
def ensure_populated!(opts = {})
self.repopulate!(opts) unless cache_exists?
end
def repopulate!
self.populate! && self.trim!
def repopulate!(opts = {})
self.populate!(opts) && self.trim!
end
def populate!
def populate!(opts = {})
# user executes query and gets back hashes
sql = @user.visible_posts_sql(:type => RedisCache.acceptable_types, :limit => CACHE_LIMIT, :order => self.order)
opts.merge!({
:type => RedisCache.acceptable_types,
:limit => CACHE_LIMIT,
:order => self.order
})
sql = @user.visible_posts_sql(opts)
hashes = Post.connection.select_all(sql)
# hashes are inserted into set in a single transaction

View file

@ -27,7 +27,7 @@ module Diaspora
if RedisCache.configured? && RedisCache.supported_order?(opts[:order_field]) && opts[:all_aspects?].present?
cache = RedisCache.new(self, opts[:order_field])
cache.ensure_populated!
cache.ensure_populated!(opts)
post_ids = cache.post_ids(opts[:max_time], opts[:limit])
end
@ -126,8 +126,8 @@ module Diaspora
# @return [Hash]
def prep_opts(opts)
defaults = {
:type => ['StatusMessage', 'Photo'],
:order => 'updated_at DESC',
:type => AspectStream::TYPES_OF_POST_IN_STREAM,
:order => 'created_at DESC',
:limit => 15,
:hidden => false
}

View file

@ -67,18 +67,20 @@ describe RedisCache do
end
it 'clears and poplulates if the cache is not populated' do
opts = {:here_is => "something"}
@cache.stub(:cache_exists?).and_return(false)
@cache.should_receive(:repopulate!)
@cache.should_receive(:repopulate!).with(opts)
@cache.ensure_populated!
@cache.ensure_populated!(opts)
end
end
describe "#repopulate!" do
it 'populates' do
opts = {:here_is => "something"}
@cache.stub(:trim!).and_return(true)
@cache.should_receive(:populate!).and_return(true)
@cache.repopulate!
@cache.should_receive(:populate!).with(opts).and_return(true)
@cache.repopulate!(opts)
end
it 'trims' do
@ -94,7 +96,8 @@ describe RedisCache do
order = "created_at DESC"
@cache.should_receive(:order).and_return(order)
bob.should_receive(:visible_posts_sql).with(hash_including(
:limit => 100,
:type => RedisCache.acceptable_types,
:limit => RedisCache::CACHE_LIMIT,
:order => order)).
and_return(sql)

View file

@ -53,7 +53,7 @@ describe User do
it "respects the :type option" do
photo = bob.post(:photo, :pending => false, :user_file=> File.open(photo_fixture_name), :to => @bobs_aspect)
alice.visible_post_ids.should include(photo.id)
alice.visible_post_ids(:type => "Photo").should include(photo.id)
alice.visible_post_ids(:type => 'StatusMessage').should_not include(photo.id)
end
@ -96,9 +96,10 @@ describe User do
end
it "gets populated with latest 100 posts" do
cache = mock(:cache_exists? => true, :ensure_populated! => mock, :post_ids => [])
cache = mock(:cache_exists? => true, :supported_order? => true, :ensure_populated! => mock, :post_ids => [])
RedisCache.stub(:new).and_return(cache)
cache.should_receive(:ensure_populated!)
@opts = alice.send(:prep_opts, @opts)
cache.should_receive(:ensure_populated!).with(hash_including(@opts))
alice.visible_post_ids(@opts)
end
@ -160,7 +161,9 @@ describe User do
it 'works' do # The set up takes a looong time, so to save time we do several tests in one
bob.visible_posts.length.should == 15 #it returns 15 by default
bob.visible_posts.should == bob.visible_posts(:by_members_of => bob.aspects.map { |a| a.id }) # it is the same when joining through aspects
bob.visible_posts.sort_by { |p| p.updated_at }.map { |p| p.id }.should == bob.visible_posts.map { |p| p.id }.reverse #it is sorted updated_at desc by default
# checks the default sort order
bob.visible_posts.sort_by { |p| p.created_at }.map { |p| p.id }.should == bob.visible_posts.map { |p| p.id }.reverse #it is sorted updated_at desc by default
# It should respect the order option
opts = {:order => 'created_at DESC'}
@ -174,21 +177,21 @@ describe User do
opts = {:limit => 40}
bob.visible_posts(opts).length.should == 40
bob.visible_posts(opts).should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map { |a| a.id }))
bob.visible_posts(opts).sort_by { |p| p.updated_at }.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse
bob.visible_posts(opts).sort_by { |p| p.created_at }.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse
# It should paginate using a datetime timestamp
last_time_of_last_page = bob.visible_posts.last.updated_at
last_time_of_last_page = bob.visible_posts.last.created_at
opts = {:max_time => last_time_of_last_page}
bob.visible_posts(opts).length.should == 15
bob.visible_posts(opts).map { |p| p.id }.should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map { |a| a.id })).map { |p| p.id }
bob.visible_posts(opts).sort_by { |p| p.updated_at }.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse
bob.visible_posts(opts).sort_by { |p| p.created_at}.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse
bob.visible_posts(opts).map { |p| p.id }.should == bob.visible_posts(:limit => 40)[15...30].map { |p| p.id } #pagination should return the right posts
# It should paginate using an integer timestamp
opts = {:max_time => last_time_of_last_page.to_i}
bob.visible_posts(opts).length.should == 15
bob.visible_posts(opts).map { |p| p.id }.should == bob.visible_posts(opts.merge(:by_members_of => bob.aspects.map { |a| a.id })).map { |p| p.id }
bob.visible_posts(opts).sort_by { |p| p.updated_at }.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse
bob.visible_posts(opts).sort_by { |p| p.created_at}.map { |p| p.id }.should == bob.visible_posts(opts).map { |p| p.id }.reverse
bob.visible_posts(opts).map { |p| p.id }.should == bob.visible_posts(:limit => 40)[15...30].map { |p| p.id } #pagination should return the right posts
end
end