background preloader

Caching things

Facebook Twitter

Ruby on rails - How does RoR cache work. Everyone should be using low level caching in Rails - robotmay. Low level caching is very easy to get started with in Rails 3, but it seems to be missing from the official guides . I personally use it all the time to help reduce the number of queries or API calls I’m making. Heroku has a pretty good explanation which, if you’re so far unfamiliar with low-level caching, is a good place to start.

Why should you use low level caching, and what should you use it for? Maybe you have some data which you need regularly over multiple pages, e.g. a list of categories for your blog. You might want to display them on every page but they’re not going to need to be entirely up to date for every request. If you haven’t used this style of caching before, note that I’m calling .all on the Category query. The mainstay of low level caching in Rails is Rails.cache.fetch . Config.cache_store = :null_store Rails.cache.fetch takes 3 arguments; the cache key, an options hash, and a block. I use low level caching most for API calls. Caching works best in layers. Caching Strategies for Rails. Action caching caching page caching rails Table of Contents Web applications will typically have a small handful of pages that take an exceptionally long time to load.

On Heroku, long running requests can tie up your dynos and seriously effect application performance. Use New Relic to determine which pages and database requests are running slowly (see the Web Transactions and Database tabs). Heroku recommends against using ‘automagic’ caching libraries such as cache-money or cache_fu. Once you have configured your application to use memcached , Rails will automatically use it for both action and fragment caching. HTTP caching Caching using HTTP headers in Rails is a technique that can be easily applied to applications with little code modification and is covered in this separate article.

Page caching Rails’s built-in page-caching works by creating a file on the file system. Action caching Simply add caches_action :<action_name> to your controller to turn on caching for specific actions. How key-based cache expiration works by David of 37signals. There are only two hard things in Computer Science: cache invalidation and naming things — Phil Karlton Doing cache invalidation by hand is an incredibly frustrating and error-prone process. You’re very likely to forget a spot and let stale data get served. That’s enough to turn most people off russian-doll caching structures, like the one we’re using for Basecamp Next. Thankfully there’s a better way. A much better way. It’s called key-based cache expiration and it works like this: The cache key is the fluid part and the cache content is the fixed part.

The caching itself then happens in the views based on partials rendering the objects in question. This process makes it trivial to implement caching schemes and trust that you’re never going to serve stale data. Caching - Rails 3 in a Nutshell. Seven Advanced Rails Fragment Cache Techniques | CRAZ8 Blog. The Rails Fragment Cache is the easiest way of making your view template code run fast.

A fragment is a piece of generated output, usually HTML, that is a part of the total output, usually the HTML page, that is returned to a user. The documentation on caching with Rails explains the basics, but doesn’t go into any depth, especially for new use cases and techniques that have been developed and expanded over the last year or so. Here are six techniques and tools that can make your fragment caching easy to manage yet provide the benefits of turbo-charging your view rendering. Don’t Expire Fragments using expire_fragment Frankly, don’t use this. Expire by time If you are using a memcache store for your Rails cache store - and your really should be - then you have the option of expiring fragments by time. <% cache @post, :expire_in => 2.hours %><%= render @post %><% end %> For some use cases, this technique is very useful.

Generational (or Key) Expiry Advanced Generational Expiry Summary. #90 Fragment Caching (revised) Caching with Rails: An overview. 1 Basic Caching This is an introduction to three types of caching techniques: page, action and fragment caching. Rails provides by default fragment caching. In order to use page and action caching, you will need to add actionpack-page_caching and actionpack-action_caching to your Gemfile. To start playing with caching you'll want to ensure that config.action_controller.perform_caching is set to true, if you're running in development mode. 1.1 Page Caching Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. 1.2 Action Caching Page Caching cannot be used for actions that have before filters - for example, pages that require authentication. 1.3 Fragment Caching Life would be perfect if we could get away with caching the entire contents of a page or action and serving it out to the world.

And you can expire it using the expire_fragment method, like so: You can also use an Active Record model as the cache key: 1.4 SQL Caching. Caching Tutorial for Web Authors and Webmasters. For Web Authors and Webmasters This is an informational document. Although technical in nature, it attempts to make the concepts involved understandable and applicable in real-world situations. Because of this, some aspects of the material are simplified or omitted, for the sake of clarity. If you are interested in the minutia of the subject, please explore the References and Further Information at the end. What’s a Web Cache? Why do people use them? A Web cache sits between one or more Web servers (also known as origin servers) and a client or many clients, and watches requests come by, saving copies of the responses — like HTML pages, images and files (collectively known as representations) — for itself.

There are two main reasons that Web caches are used: To reduce latency — Because the request is satisfied from the cache (which is closer to the client) instead of the origin server, it takes less time for it to get the representation and display it. Kinds of Web Caches Browser Caches. Rack::Cache. Rack::Cache is suitable as a quick drop-in component to enable HTTP caching for Rack-based applications that produce freshness (Expires, Cache-Control) and/or validation (Last-Modified, ETag) information. Standards-based (see RFC 2616 / Section 13).Freshness/expiration based cachingValidationVary supportPortable: 100% Ruby / works with any Rack-enabled framework.Disk, memcached, and heap memory storage backends. News Rack::Cache 1.0 was released on December 24, 2010. See the CHANGES file for details.How to use Rack::Cache with Rails 2.3 – it’s really easy.RailsLab’s Advanced HTTP Caching Screencast is a really great review of HTTP caching concepts and shows how to use Rack::Cache with Rails.

Installation $ sudo gem install rack-cache Or, from a local working copy: $ git clone $ rake package && sudo rake install Basic Usage Rack::Cache is implemented as a piece of Rack middleware and can be used with any Rack-based application. More See Also License.