lib/nitro_pg_cache/viewer_ext.rb
def db_cache_collection(options)

      if options[:collection].is_a?(ActiveRecord::Relation)

        whole_cache_key = options[:collection].try(:cache_key)
        cached_result = NitroCache.where( nitro_cache_key: whole_cache_key, nitro_cacheable_id: nil, nitro_cacheable_type: nil).first

        if cached_result
          cached_result.update_attributes(viewed_at: Time.now)
          return cached_result.try(:nitro_cached_value).try(:html_safe)
        end

        render_result = options[:straight_cache] ? db_cache_collection_s( options ) : db_cache_collection_r( options )

        # why not NitroCache.create( nitro_cached_value: render_result, nitro_cache_key: whole_cache_key, nitro_cacheable_id: nil, nitro_cacheable_type: nil) &
        # because on large collections raw insert is 1.2 faster, i.e. same speed as memcache.
        NitroCache.connection.execute( <<INSERT_STR
          INSERT INTO "nitro_caches" ("nitro_cached_value", "nitro_cache_key", "nitro_partial_id", "viewed_at" )
           VALUES ('#{render_result}', '#{whole_cache_key}', '#{@current_cache_aggregator[:nitro_partial_id]}', '#{Time.now}')
INSERT_STR
        )
        render_result
      else
        db_cache_array( options )
      end
    end
# I decided to keep both solutions reverse and straight. reverse is faster,
# but straight is more srtaightforward :) and may be more stable, especially with DB sharding
# also when using prerendering speed is the same and stability is preferable
I decided to keep both solutions reverse and straight. reverse is faster,
but straight is more srtaightforward :) and may be more stable, especially with DB sharding
also when using prerendering speed is the same and stability is preferable
I decided to keep both solutions reverse and straight . reverse is faster but straight is more srtaightforward : ) and may be more stable especially with DB sharding also when using prerendering speed is the same and stability is preferable
def db_cache_collection(options)
      if options[:collection].is_a?(ActiveRecord::Relation)
        whole_cache_key = options[:collection].try(:cache_key)
        cached_result = NitroCache.where( nitro_cache_key: whole_cache_key, nitro_cacheable_id: nil, nitro_cacheable_type: nil).first
        if cached_result
          cached_result.update_attributes(viewed_at: Time.now)
          return cached_result.try(:nitro_cached_value).try(:html_safe)
        end
        render_result = options[:straight_cache] ? db_cache_collection_s( options ) : db_cache_collection_r( options )
        NitroCache.connection.execute( <<INSERT_STR
          INSERT INTO "nitro_caches" ("nitro_cached_value", "nitro_cache_key", "nitro_partial_id", "viewed_at" )
           VALUES ('#{render_result}', '#{whole_cache_key}', '#{@current_cache_aggregator[:nitro_partial_id]}', '#{Time.now}')
INSERT_STR
        )
        render_result
      else
        db_cache_array( options )
      end
    end
def db_cache_collection ( options ) if options [ :collection ] . is_a? ( ActiveRecord :: Relation ) whole_cache_key = options [ :collection ] . try ( :cache_key ) cached_result = NitroCache . where ( nitro_cache_key : whole_cache_key , nitro_cacheable_id : nil , nitro_cacheable_type : nil ) . first if cached_result cached_result . update_attributes ( viewed_at : Time . now ) return cached_result . try ( :nitro_cached_value ) . try ( :html_safe ) end render_result = options [ :straight_cache ] ? db_cache_collection_s ( options ) : db_cache_collection_r ( options ) NitroCache . connection . execute ( <<INSERT_STR 
          INSERT INTO "nitro_caches" ("nitro_cached_value", "nitro_cache_key", "nitro_partial_id", "viewed_at" )
           VALUES (' #{ render_result } ', ' #{ whole_cache_key } ', ' #{ @current_cache_aggregator [ :nitro_partial_id ] } ', ' #{ Time . now } ')
 INSERT_STR ) render_result else db_cache_array ( options ) end end
I decided to keep both solutions reverse and straight.
I decided to keep both solutions reverse and straight .
# why not NitroCache.create( nitro_cached_value: render_result, nitro_cache_key: whole_cache_key, nitro_cacheable_id: nil, nitro_cacheable_type: nil) & # because on large collections raw insert is 1.2 faster, i.e. same speed as memcache.
options