Best way to combine fragment and object caching for memcached and Rails

前端 未结 5 632
故里飘歌
故里飘歌 2021-01-31 12:11

Lets say you have a fragment of the page which displays the most recent posts, and you expire it in 30 minutes. I\'m using Rails here.

<% cache(\"recent_posts         


        
5条回答
  •  别那么骄傲
    2021-01-31 12:57

    just as a piece of thought:

    in application controller define

    def when_fragment_expired( name, time_options = nil )
            # idea of avoiding race conditions
            # downside: needs 2 cache lookups
            # in view we actually cache indefinetely 
            # but we expire with a 2nd fragment in the controller which is expired time based
            return if ActionController::Base.cache_store.exist?( 'fragments/' + name ) && ActionController::Base.cache_store.exist?( fragment_cache_key( name ) )
    
            # the time_fraqgment_cache uses different time options
            time_options = time_options - Time.now if time_options.is_a?( Time )
    
            # set an artificial fragment which expires after given time
            ActionController::Base.cache_store.write("fragments/" + name, 1, :expires_in => time_options )
    
            ActionController::Base.cache_store.delete( "views/"+name )        
            yield    
      end
    

    then in any action use

        def index
    when_fragment_expired "cache_key", 5.minutes
    @object = YourObject.expensive_operations
    end
    end
    

    in view

    cache "cache_key" do
    view_code
    end
    

提交回复
热议问题