Module: Sinatra::Cache::Helpers
- Defined in:
- lib/nesta/cache.rb
Instance Method Summary collapse
-
#cache(content, opts = {}) ⇒ Object
Caches the given URI to a html file in /public.
-
#cache_expire(path = nil, opts = {}) ⇒ Object
Expires the cached URI (as .html file) in /public.
-
#page_cached_timestamp ⇒ Object
Prints a basic HTML comment with a timestamp in it, so that you can see when a file was cached last.
Instance Method Details
#cache(content, opts = {}) ⇒ Object
Caches the given URI to a html file in /public
Usage:
>> cache( erb(:contact, :layout => :layout))
=> returns the HTML output written to /public/<CACHE_DIR_PATH>/contact.html
Also accepts an Options Hash, with the following options:
* :extension => in case you need to change the file extension
TODO:: implement the opts={} hash functionality. What other options are needed?
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/nesta/cache.rb', line 29 def cache(content, opts={}) return content unless .cache_enabled unless content.nil? content = "#{content}\n#{}\n" path = cache_page_path(request.path_info,opts) FileUtils.makedirs(File.dirname(path)) open(path, 'wb+') { |f| f << content } log("Cached Page: [#{path}]",:info) content end end |
#cache_expire(path = nil, opts = {}) ⇒ Object
Expires the cached URI (as .html file) in /public
Usage:
>> cache_expire('/contact')
=> deletes the /public/<CACHE_DIR_PATH>contact.html page
get '/contact' do
cache_expire # deletes the /public/<CACHE_DIR_PATH>contact.html page as well
end
TODO:: implement the options={} hash functionality. What options are really needed ?
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/nesta/cache.rb', line 53 def cache_expire(path = nil, opts={}) return unless .cache_enabled path = (path.nil?) ? cache_page_path(request.path_info) : cache_page_path(path) if File.exist?(path) File.delete(path) log("Expired Page deleted at: [#{path}]",:info) else log("No Expired Page was found at the path: [#{path}]",:info) end end |
#page_cached_timestamp ⇒ Object
Prints a basic HTML comment with a timestamp in it, so that you can see when a file was cached last.
NB! IE6 does NOT like this to be the first line of a HTML document, so output inside the <head> tag. Many hours wasted on that lesson ;-)
Usage:
>> <%= page_cached_timestamp %>
=> <!-- page cached: 2009-02-24 12:00:00 -->
74 75 76 |
# File 'lib/nesta/cache.rb', line 74 def "<!-- page cached: #{Time.now.strftime("%Y-%d-%m %H:%M:%S")} -->\n" if .cache_enabled end |