Module: YARD::Server::StaticCaching
- Included in:
- Commands::Base, Router
- Defined in:
- lib/yard/server/static_caching.rb
Overview
Implements static caching for requests.
Instance Method Summary collapse
-
#cache(data) ⇒ void
Caches rendered HTML response data to disk.
-
#check_static_cache ⇒ Array(Numeric,Hash,Array)?
Called by a router to return the cached object.
Instance Method Details
#cache(data) ⇒ void
This method returns an undefined value.
Caches rendered HTML response data to disk.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/yard/server/static_caching.rb', line 52 def cache(data) return unless adapter.document_root path = cache_path(request.path_info) return unless path FileUtils.mkdir_p(File.dirname(path)) log.debug "Caching data to #{path}" File.open(path, 'wb') {|f| f.write(data) } end |
#check_static_cache ⇒ Array(Numeric,Hash,Array)?
Called by a router to return the cached object. By default, this method performs disk-based caching. To perform other forms of caching, implement your own #check_static_cache method and mix the module into the Router class.
This method checks for the existence of cached data. To actually cache a response, see #cache.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/yard/server/static_caching.rb', line 35 def check_static_cache return nil unless adapter.document_root cache_path = cache_path(request.path) return nil unless cache_path if File.file?(cache_path) log.debug "Loading cache from disk: #{cache_path}" return [200, {'Content-Type' => 'text/html'}, [File.read_binary(cache_path)]] end nil end |