123456789_123456789_123456789_123456789_123456789_

Module: YARD::Server::StaticCaching

Overview

Implements static caching for requests.

See Also:

Since:

  • 0.6.0

Instance Method Summary

Instance Method Details

#cache(data) ⇒ void

This method returns an undefined value.

Caches rendered HTML response data to disk.

Parameters:

  • data (String)

    the data to cache

Since:

  • 0.9.44

[ GitHub ]

  
# 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

#cache_path(request_path) (private)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/static_caching.rb', line 65

def cache_path(request_path)
  return nil if request_path.split(/[\/\\]/).include?('..')

  path = request_path.sub(/\.html$/, '') + '.html'
  path = path.sub(%r{\A/+}, '')
  return nil if path =~ /\A[A-Za-z]:/

  path = File.cleanpath(path)
  File.join(adapter.document_root, path)
end

#check_static_cacheArray(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.

Examples:

Implementing In-Memory Cache Checking

module MemoryCaching
  def check_static_cache
    # $memory_cache is filled by {Commands::Base#cache}
    cached_data = $memory_cache[request.path]
    if cached_data
      [200, {'Content-Type' => 'text/html'}, [cached_data]]
    else
      nil
    end
  end
end

class YARD::Server::Router; include MemoryCaching; end

Returns:

  • (Array(Numeric,Hash,Array))

    the Rack-style response

  • (nil)

    if no cache is available and routing should continue

See Also:

  • Commands::Base#cache

Since:

  • 0.6.0

[ GitHub ]

  
# 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