ruby - Rails Caching in Agile Web Development w/ Rails4 -


i'm working way through book agile web development rails4, , read (first) part caching parts of view avoid overwhelming database. i've of course set caching option true in config development environment.

the problem caching doesn't seem working. here app/views/store/index.html.erb file, 1 given in book, enable caching :

<% if notice %>   <p id="notice"><%= notice %></p> <% end %>  <h1>your pragmatic catalog</h1>  <% cache ['store', product.latest] %>   <% @products.each |product| %>     <% cache ['entry', product] %>       <div class="entry">         <%= image_tag(product.image_url) %>         <h3><%= product.title %></h3>         <%= sanitize(product.description) %>         <div class="price_line">           <span class="price"><%= number_to_currency(product.price) %></span>         </div>       </div>     <% end %>   <% end %> <% end %> 

and here rails server log, showing databse accessed multiple times (although line mentions caching) : http://pastebin.com/v2jgihkl

here app/views/store/index.html.erb file, tried else caching :

<% if notice %>   <p id="notice"><%= notice %></p> <% end %>  <h1>your pragmatic catalog</h1>  <% cache('caching') %>   <% @products.each |product| %>       <div class="entry">         <%= image_tag(product.image_url) %>         <h3><%= product.title %></h3>         <%= sanitize(product.description) %>         <div class="price_line">           <span class="price"><%= number_to_currency(product.price) %></span>         </div>       </div>   <% end %> <% end %> 

and here corresponding log, showing caching successful (as database not queried) : http://pastebin.com/ztk9a9ra

can explain why 1 seems work , not other, or how first 1 should work ? thank :)

note in book, says caching enabled, reloading store page shouldn't show new parts of store/index.html.erb if changes made inside cached block ; yet in both case. idea ?

the parameter pass cache api caching key. hashing function of key cached fragment stored, , looked for.

in code (cache('caching')) caching key hard-coded, , never changes, so, unless cache invalidated (either manually in code, or when ttl has passed) - same fragment sent client.

in code book (cache ['store', product.latest]) key depends on latest product. means if latest product changes (another product added, or updated) - next call automatically 'know' not take page cache, recreate cache.

what shown in logs:

[1m[35mproduct load (0.3ms)[0m  select  "products".* "products"   order "products"."updated_at" desc limit 1 

is result of product.latest, not of product.all.

to sum - both code fragments cached, in example code, there is (small) hit database verify validity of cache, whether page cached or not.

if invalidating cache yourself, can keep hard-coded version of code, if not, better think of invalidation scheme, whether example code suggests or else.


Comments

Popular posts from this blog

My HTML document is not linking to my CSS stylesheet properly -

php array slice every 2th rule -

node.js - Sending sockets to client side, Error: Converting circular structure to JSON -