Merb Wiki
Home
All Pages
New Page
Editing Caching
Textile Enabled
| [[Page Name]] for internal links
_merb-cache_ plugin is part of the _merb-more_ package Much of this info compiled from "merb-cache rdoc":http://merbivore.com/documentation/merb-more/0.9.2/merb-cache/index.html -- contribute!! merb-cache currently supports: * page caching * action caching * fragment caching * object caching Implemented cache stores: * memory * memcache * file * database (sequel, datamapper, activerecord) h2. Example configuration <pre><code class="ruby"> Merb::Plugins.config[:merb_cache] = { :cache_html_directory => Merb.dir_for(:public), #:store => "database", #:table_name => "merb_cache", #:disable => "development", # disable merb-cache in development #:disable => true, # disable merb-cache in all environments :store => "file", :cache_directory => Merb.root_path("tmp/cache"), #:store => "memcache", #:host => "127.0.0.1:11211", #:namespace => "merb_cache", #:no_tracking => "false", #:store => "memory", # store could be: file, memcache, memory, database, dummy, ... } </code></pre> h2. Example Code h3. controller <pre><code class="ruby"> class Users < Merb::Controller cache_page :list # this will cache the action in public/cache/list.html # by default or to whatever you pointed :cache_html_directory # to in plugin configuration # this cache entry will never expire (no expiration provided) # for permanent caching you could set your lighty/nginx so as to handle # the .html file directly # for multiple page caching: # cache_pages :archives, [:list, 5] cache_action :show, 10 # this will cache the action using the cache store # this cache entry will expire in 10 minutes # for multiple action caching: # cache_actions :archives, [:list, 5], :some_action # example of persisted entities caching using some keys convention def list unless @users = cache_get("active_users") @users = User.all(:active => true) cache_set("active_users", @users) # object caching can be used to avoid pulling huge amounts of data # from the database. # you could have called cache_set with an expiration time as well: # cache_set("active_users", @users, 10) end render end def create # do validation and creation... expire_page(:list) render end def update # do update... expire_action(:action => "archives", :controller => "users") end def destroy # destroy object... expire("active_users") # see fragment caching examples above expire("user_archives") # redirect end def archives @archives = User.archives unless cached?("users_archives") render end def index render end end </code></pre> h3. views/users/index.html.erb <pre><code class="html"> <!-- this entry will expire in 10 minutes --> <%- cache "online_users_count", 10 do %> <div>some expensive queries or calculations</div> <% end -%> </code></pre> h3. views/users/archive.html.erb <pre><code class="html"> <%- cache "users_archives" do %> <div>cache invalidated somewhere in controller when necessary</div> <% end -%> </code></pre>
Submit
Recent Activity
Home
was updated
Home
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
Merb Developers available for work
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
deploying-a-merb-application-to-a-jee-container-us
was updated
Home
was updated