The caching plugin adds methods related to HTTP caching.
For proper caching, you should use either the
etag request methods.
r.get 'albums', Integer do |album_id| @album = Album[album_id] r.last_modified @album.updated_at view('album') end # or r.get 'albums', Integer do |album_id| @album = Album[album_id] r.etag @album.sha1 view('album') end
etag will immediately halt processing if there have been no modifications since the last time the client requested the resource, assuming the client uses the appropriate HTTP 1.1 request headers.
This plugin also includes the
expires response methods. The
cache_control method sets the Cache-Control header using the given hash:
response.cache_control public: true, max_age: 60 # Cache-Control: public, max-age=60
expires method is similar, but in addition to setting the HTTP 1.1 Cache-Control header, it also sets the HTTP 1.0 Expires header:
response.expires 60, public: true # Cache-Control: public, max-age=60 # Expires: Mon, 29 Sep 2014 21:25:47 GMT
The implementation was originally taken from Sinatra, which is also released under the MIT License:
Copyright © 2007, 2008, 2009 Blake Mizerany Copyright © 2010, 2011, 2012, 2013, 2014 Konstantin Haase
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.