2012-10-26 23 views
0

我有一個來自用戶的事件的動態反饋,當前用戶正在跟蹤這些事件,這些partials都是memcached,但我也想爲它自己做這件事。我擔心的是,如果我使用memcache,它會根據第一次寫入緩存而不是爲每個唯一用戶動態顯示每個用戶的相同內容。Memcached動態提要?

我該如何memcache飼料和@追加?如果當前用戶關注其他用戶,我該如何更新它?我假設我不能只用<%緩存包起來「喂」辦%>

注意:在使用Memcached和達利

<% @following = current_user.following.collect {|f| f["id"]} %> 
<div class='content'> 
<% unless Rsvp.where(:voter_id => @following, :status => 'going').where("start_time > ? AND start_time < ?", Time.now, Time.now.strftime('%Y-%m-%d 23:59:59.000000')).order("count_all desc").count(:group => :event_id).collect { |f| f[0] }.count == 0 %> 
      <div class='date_bar_container'><div class='date_bar'>Today</div></div> 
      <div class='content_wrapper'> 
    <%= render :partial => 'events/event', :collection => Event.where(:id => Rsvp.where(:event_id => Rsvp.where(:voter_id => @following, :status => 'going').where("start_time > ? AND start_time < ?", Time.now.strftime('%Y-%m-%d 00:00:00.000000'), Time.now.strftime('%Y-%m-%d 23:59:59.000000')).order("count_all desc").count(:group => :event_id).collect { |f| f[0] }).select("DISTINCT event_id").collect(&:event_id)).order('start_time asc')%> 
      </div> 
<% end %> 

<% unless Rsvp.where(:voter_id => @following, :status => 'going').where("start_time > ? AND start_time < ?", (Time.now + 1.day).strftime('%Y-%m-%d 00:00:00.000000'), (Time.now + 1.day).strftime('%Y-%m-%d 23:59:59.000000')).order("count_all desc").count(:group => :event_id).collect { |f| f[0] }.count == 0%> 
      <div class='date_bar_container'><div class='date_bar'>Tomorrow</div></div> 
      <div class='content_wrapper'> 
    <%= render :partial => 'events/event', :collection => Event.where(:id => Rsvp.where(:event_id => Rsvp.where(:voter_id => @following, :status => 'going').where("start_time > ? AND start_time < ?", (Time.now + 1.day).strftime('%Y-%m-%d 00:00:00.000000'), (Time.now + 1.day).strftime('%Y-%m-%d 23:59:59.000000')).order("count_all desc").count(:group => :event_id).collect { |f| f[0] }).select("DISTINCT event_id").collect(&:event_id)).order('start_time asc')%> 
    </div> 
    <% end %> 

<% (2..15).each do |f| %> 
    <% unless Rsvp.where(:voter_id => @following, :status => 'going').where("start_time > ? AND start_time < ?", (Time.now + f.day).strftime('%Y-%m-%d 00:00:00.000000'), (Time.now + f.day).strftime('%Y-%m-%d 23:59:59.000000')).order("count_all desc").count(:group => :event_id).collect { |f| f[0] }.count == 0 %> 
       <div class='date_bar_container'><div class='date_bar'><%= dayofweek((Time.now + f.day).wday) %>, <%= month((Time.now + f.day).month) %> <%= (Time.now + f.day).day %>, 2012</div></div> 
       <div class='content_wrapper'> 
     <%= render :partial => 'events/event', :collection => Event.where(:id => Rsvp.where(:event_id => Rsvp.where(:voter_id => @following, :status => 'going').where("start_time > ? AND start_time < ?", (Time.now + f.day).strftime('%Y-%m-%d 00:00:00.000000'), (Time.now + f.day).strftime('%Y-%m-%d 23:59:59.000000')).order("count_all desc").count(:group => :event_id).collect { |f| f[0] }).select("DISTINCT event_id").collect(&:event_id)).order('start_time asc')%> 
     </div> 
     <% end %> 
<% end %> 
</div> 

回答

0

無關,但這是太多型號代碼/ SQL在您的意見。將它們包裝在類方法或範圍中。

關於緩存,如果你不希望每個用戶都看到相同的東西,那麼你需要使每個用戶的緩存鍵不同。這樣做的一種方式是通過action_suffix選項,該選項增加了用於生成緩存鍵的數據(默認情況下它只是控制器和操作)。如果您包含當前用戶ID作爲其中的一部分,則緩存將以每個用戶爲基礎完成。您也可以將一個任意緩存鍵的字符串傳遞給用戶,而不是通過控制器,動作和動作後綴構建。

緩存失效據說是計算機科學中兩個難題之一。有至少3個選項:

  • 當您緩存某些內容時,請將expires_in設置爲適當的時間範圍。當底層數據變化什麼都不做時:您接受用戶看到的內容可能會過期,直到您設置的時間範圍內。將其設置爲高,他們會看到很長一段時間的陳舊數據,將其設置得太低,並且會丟棄本來可以保留的緩存數據。

例如:

<% cache({:action_suffix => 'blah'}, :expires_in => 20.minutes do %> 
    .. 
<% end %> 
  • 明確到期響應所述數據經由一個清掃器模型的變化,例如。記住所有這些可能發生的地方並記住增加清掃器可能會非常棘手

  • 使用世代緩存鍵:構造您的緩存鍵以便當底層數據更改緩存鍵時也改變。一個非常簡單的例子就是如果你緩存了關於用戶的一頁信息。您可以將緩存密鑰設置爲user_#{user.id}_#{user.updated_at.to_i}(或等同地,user.cache_key)。當用戶改變,無論怎麼樣,然後你用修改過的緩存鍵(因爲updated_at也會有變化),所以你就不能使用舊的緩存數據

+0

而不是使用'「用戶_#{ user.id} _#{user.updated_at.to_i}「''你可以使用內建的'cache_key'方法。 http://apidock.com/rails/ActiveRecord/Base/cache_key請參閱頁面上的第三個示例。 – James

+0

當然,已經忘記了那一個 –