remove hidden posts from the cache; add unhidden posts to the cache

This commit is contained in:
danielgrippi 2011-10-06 19:13:53 -07:00
parent cf01d03656
commit 2baa1e7c6e
4 changed files with 91 additions and 5 deletions

View file

@ -9,17 +9,36 @@ class PostVisibilitiesController < ApplicationController
def update def update
#note :id references a postvisibility #note :id references a postvisibility
@post = Post.where(:id => params[:post_id]).select("id, guid, author_id").first @post = accessible_post
@contact = current_user.contact_for(@post.author) @contact = current_user.contact_for(@post.author)
if @contact && @vis = PostVisibility.where(:contact_id => @contact.id, if @contact && @vis = PostVisibility.where(:contact_id => @contact.id,
:post_id => params[:post_id]).first :post_id => params[:post_id]).first
@vis.hidden = !@vis.hidden @vis.hidden = !@vis.hidden
if @vis.save if @vis.save
update_cache(@vis)
render 'update' render 'update'
return return
end end
end end
render :nothing => true, :status => 403 render :nothing => true, :status => 403
end end
protected
def update_cache(visibility)
return unless RedisCache.configured?
cache = RedisCache.new(current_user, 'created_at')
if visibility.hidden?
cache.remove(accessible_post.id)
else
cache.add(accessible_post.created_at.to_i, accessible_post.id)
end
end
def accessible_post
@post ||= Post.where(:id => params[:post_id]).select("id, guid, author_id, created_at").first
end
end end

View file

@ -81,6 +81,11 @@ class RedisCache
self.trim! self.trim!
end end
def remove(id)
return unless self.cache_exists?
self.redis.zrem(set_key, id)
end
# exposing the need to tie cache to a stream # exposing the need to tie cache to a stream
# @return [Array<String>] Acceptable Post types for the given cache # @return [Array<String>] Acceptable Post types for the given cache
def self.acceptable_types def self.acceptable_types

View file

@ -8,11 +8,12 @@ describe PostVisibilitiesController do
before do before do
@status = alice.post(:status_message, :text => "hello", :to => alice.aspects.first) @status = alice.post(:status_message, :text => "hello", :to => alice.aspects.first)
@vis = @status.post_visibilities.first @vis = @status.post_visibilities.first
sign_in :user, bob
end end
describe '#update' do describe '#update' do
before do before do
sign_in :user, bob @controller.stub(:update_cache)
end end
context "on a post you can see" do context "on a post you can see" do
@ -21,6 +22,11 @@ describe PostVisibilitiesController do
response.should be_success response.should be_success
end end
it 'calls #update_cache' do
@controller.should_receive(:update_cache).with(an_instance_of(PostVisibility))
put :update, :format => :js, :id => 42, :post_id => @status.id
end
it 'marks hidden if visible' do it 'marks hidden if visible' do
put :update, :format => :js, :id => 42, :post_id => @status.id put :update, :format => :js, :id => 42, :post_id => @status.id
@vis.reload.hidden.should be_true @vis.reload.hidden.should be_true
@ -51,4 +57,42 @@ describe PostVisibilitiesController do
end end
end end
end end
describe '#update_cache' do
before do
@controller.params[:post_id] = @status.id
@cache = RedisCache.new(bob, 'created_at')
RedisCache.stub(:new).and_return(@cache)
RedisCache.stub(:configured?).and_return(true)
end
it 'does nothing if cache is not configured' do
RedisCache.stub(:configured?).and_return(false)
RedisCache.should_not_receive(:new)
@controller.send(:update_cache, @vis)
end
it 'removes the post from the cache if visibility is marked as hidden' do
@vis.hidden = true
@cache.should_receive(:remove).with(@vis.post_id)
@controller.send(:update_cache, @vis)
end
it 'adds the post from the cache if visibility is marked as hidden' do
@vis.hidden = false
@cache.should_receive(:add).with(@status.created_at.to_i, @vis.post_id)
@controller.send(:update_cache, @vis)
end
end
describe "#accessible_post" do
it "memoizes a query for a post given a post_id param" do
id = 1
@controller.params[:post_id] = id
Post.should_receive(:where).with(hash_including(:id => id)).once.and_return(stub.as_null_object)
2.times do |n|
@controller.send(:accessible_post)
end
end
end
end end

View file

@ -160,7 +160,27 @@ describe RedisCache do
@cache.stub(:cache_exists?).and_return(false) @cache.stub(:cache_exists?).and_return(false)
@redis.should_not_receive(:zadd) @redis.should_not_receive(:zadd)
@cache.add(@score, @id).should be_false @cache.add(@score, @id)
end
end
describe "#remove" do
before do
@id = 1
end
it "doesn't add if the cache does not exist" do
@cache.stub(:cache_exists?).and_return(false)
@redis.should_not_receive(:zrem)
@cache.remove(@id).should be_false
end
it "removes a given id" do
@cache.stub(:cache_exists?).and_return(true)
@redis.should_receive(:zrem).with(@cache.send(:set_key), @id)
@cache.remove(@id)
end end
end end
@ -190,6 +210,4 @@ describe RedisCache do
RedisCache.acceptable_types.should =~ AspectStream::TYPES_OF_POST_IN_STREAM RedisCache.acceptable_types.should =~ AspectStream::TYPES_OF_POST_IN_STREAM
end end
end end
describe "#remove"
end end