1

(キャッシングdalli)は静的ページのパフォーマンスを向上させる非常に強力なプラグインです。
しかし、頻繁に更新されるページネーションのある動的ページはどうでしょうか?
セットアップする正しい方法は何dalliですか?

たとえば、私が遭遇した1つの問題dalli:ページネーションを使用するときに、異なるparams [:page]を同じページとして認識します:(

dalli両方を使用する場合、システムをどのように設計しますか

  • 頻繁に更新されるページ
  • あまり更新しないページ

例えば。UserCommunityTopic、などの 4 つのモデルがあります。Comment(defined Polymorphic to both Community, and Topic. They can be created or shown in #show pages)

みたいな

  • コミュニティ > (has_many) > トピック > (has_many) > コメント
  • コミュニティ > (has_many) > コメント
  • すべてユーザー(作成者)に属します

私の現在のコードはこのようなものです(これはかなり長いです。申し訳ありません)
キャッシュを使用するときにページネーションの問題に直面しています...

--------------------------------------設定------------ ----------------------------

config/environments/development.rb

config.consider_all_requests_local       = true
config.action_controller.perform_caching = true
config.cache_store = :dalli_store

ルート.rb

resources :communities do
    resources :topics do
        resources :comments do
    end
end

--------------------------------------コミュニティ------------ ----------------------------

controllers/communities_controller.rb#index&show

caches_page :show
caches_action :edit, :new

def index
    ....
    if params[:sort] == 'new'
        @communities = Community.scoped.page(params[:page]).order("created_at DESC")
    elsif params[:sort] = 'popular'
        @communities = Community.scoped.page(params[:page]).order("follow_count DESC")
    elsif params[:sort] = 'reputation'
        @communities = Community.scoped.page(params[:page]).order("reputation_count DESC")
    else
        @communities = Community.scoped.page(params[:page]).order("created_at DESC")
    end
    ....
end

def show
    @community = Community.find(params[:community_id])
    @comments @community.comments.page(params[:page]
    @topics = @community.topics.limit(10)
end

views/communities/index.html.erb 注: ただし、次の params[:page]:( キャッシングは別のページを同じコンテンツとして認識しているようです...

#here, the url will could be something like this example.com/communities?utf8=✓&location=14&genre=3&search=strawberry
 But this is going to create gigantic petterns of the caches:(  
 So I want to make it work only when params[:sort] was not empty. Because, no other parameters come with when params[:sort] is not empty. 
 It could be like, example.com/communities?sort=new, example.com/communities?sort=popular, example.com/communities?sort=reputation


...
<% if params[:sort] %>
    <% @key = "community_index_" + params[:sort] + params[:page] %>

    <% cache(:controller => "communities", :action => "index", :action_suffix => @key) do %>
        <%= page_entries_info(@communities, :entry_name => 'community').html_safe %>
        <%= paginate @communities, :window => 4 %>

        <% @communities.each do |community| %>  
            <%= render 'communities/community', :community => community %>
        <% end %>
    <% end %>
<% end %>
...

ビュー/コミュニティ/show.html.erb

...
<% @key = params[:community_name] + "_community_show_information" %>
<% cache(:controller => "communities", :action => "show", :action_suffix => @key) do %>
    <h2>Information</h2>
    <div class="CommunityInformation">
        <%= render 'communities/information'%>
    </div>
<% end %>


<% @key = params[:community_name] + "_community_show_wall_" + params[:page] + %>
<% cache(:controller => "communities", :action => "show", :action_suffix => @key) do %>
<%= paginate @comments, :window => 4 %>
    <h2>Topic</h2>
    <div class="WallInformation">
        <%  @comments.eager.recent.each do |comment| %>
            <%= render 'communities/comment', :comment => comment %>
        <% end %>
    </div>
<% end %>

<% @key = params[:community_name] + "_community_show_topics" %>
<% cache(:controller => "communities", :action => "show", :action_suffix => @key) do %>
    <h2>Topic</h2>
    <div class="TopicInformation">
        <%  @topics.eager.recent.each do |topic| %>
            <%= render 'communities/topic', :topic => topic %>
        <% end %>
    </div>
<% end %>
...

models/community_sweeper.rb

class CommunitySweeper < ActionController::Caching::Sweeper
  observe Community


  def after_save(record)
    expire_fragment(url_for(:action => 'index', :only_path => true) + '?????(all the caches related to the community#index)')
    expire_fragment(url_for(:action => 'show', :only_path => true) + '?????(the cache related to the particular community#show)')
  end
end

end

--------------------------------------トピック------------ ----------------------------

controllers/topics_controller.rb#index&show

caches_page :show
caches_action :edit, :new

def index
    ....
    @community = Community.find(params[:community_id])
    @topics = @community.topics.page(params[:page]
    ....
end

def show
    @topic = Topic.find(params[:id])
    @comments = @topic.comments.page(params[:page]
end

ビュー/トピック/index.html.erb

...
<% @key = params[:community_id] + "_topic_index_"  + params[:page] %>
    <% cache(:controller => "topics", :action => "index", :action_suffix => @key) do %>
        <%= page_entries_info(@communities, :entry_name => 'community').html_safe %>
        <%= paginate @communities, :window => 4 %>

        <% @communities.each do |community| %>  
            <%= render 'communities/community', :community => community %>
        <% end %>
    <% end %>
<% end %>
...

ビュー/トピック/show.html.erb

...    
<% @key = params[:community_name] + "_topic_show_" + params[:id] + "_comments" + params[:page] %>
    <% cache(:controller => "topics", :action => "show", :action_suffix => @key) do %>
         <%= paginate @comments, :window => 4 %>
         <%= page_entries_info(@comments, :entry_name => 'comment').html_safe %>

         <h2>Comment</h2>
         <div class="CommentInformation">
         <% @comments.each do |comment| %>
             <%= render 'topics/comment', :comment => comment %>
         <% end %>
    </div>
<% end %>
...

models/topic_sweeper.rb

class TopicSweeper < ActionController::Caching::Sweeper
  observe Topic


  def after_save(record)
    expire_fragment(url_for(:action => 'index', :only_path => true) + '?????(all the caches related to the topic#index)')
    expire_fragment(url_for(:action => 'show', :only_path => true) + '?????(the cache related to the particular topic#show)')
  end
end

--------------------------------------コメント------------ ----------------------------

models/comment_sweeper.rb

class CommentSweeper < ActionController::Caching::Sweeper
  observe Comment


  def after_save(record)
    expire_fragment(url_for(:action => 'index', :only_path => true) + '?????(all the caches related to the topic#index)')
    expire_fragment(url_for(:action => 'show', :only_path => true) + '?????(the cache related to the particular topic#show)')
  end
end
4

1 に答える 1

2

私は私の答えに続いています

コンテンツの更新が多い動的なページに dalli を設定するにはどうすればよいですか?

私は専門家ではありませんが、あなたのアプローチを試してみましたが、アプリケーションが成長するにつれてますます複雑になり、最後の質問で述べたキャッシュ キー フラグメント ベースのアプローチを採用したため、放棄しました。注意すべき点がいくつかあります。

  1. キャッシュ スイーパー アプローチでは、アプリケーションに変更を加えるときにスイーパーが最新であることを確認する必要があります。これは、追加のメンテナンスとテスト作業を意味します。

  2. 動的なアプリケーションでは、特にビューが多くのモデルからの情報を表示する場合、ページ全体を簡単にキャッシュすることはおそらくできません。

  3. ページ パラメータがキャッシュ キーの一部にならない限り、ページングの問題に対処することはできません。

  4. キャッシュがいっぱいになると、memcached は、新しいキャッシュ フラグメントを挿入するときに、使用頻度の最も低い/最も古いキャッシュ アイテムを単純に削除します。

  5. したがって、モデルが変更されたときに新しいキャッシュ フラグメントをプッシュするだけで、memcached が古くなったキャッシュ アイテムをクリアしても問題はありません。

したがって、ページングを処理し、手動のキャッシュ無効化に対処する必要がないため、ビューでのフラグメント キャッシュがおそらく最も簡単な解決策になります。

あなたの見解の1つを見て、私ができることは

<% if params[:sort] %>

        <%= page_entries_info(@communities, :entry_name => 'community').html_safe %>
        <%= paginate @communities, :window => 4 %>

        <% @communities.each do |community| %>  
            <% cache(community, :suffix => "community_index") do 
                <%= render 'communities/community', :community => community %>
            <% end %>
        <% end %>
<% end %>

私が行ったことは、各コミュニティ レコードのレンダリングをキャッシュし、ページングが無関係になることです

于 2013-01-27T11:53:12.657 に答える