Sha256: 4c92ff5a9f1a843ab02f1cc824a30fc116a3c68cf7bab98639c412edfcf75297

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

module YARD
  module Server
    # Implements static caching for requests.
    # 
    # @see Router Router documentation for "Caching"
    module StaticCaching
      # 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.
      # 
      # Note that caching does not occur here. This method simply checks for
      # the existence of cached data. To actually cache a response, see
      # {Commands::Base#cache}.
      # 
      # @example 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
      # @return [Array(Number,Hash,Array)] the Rack-style response
      # @return [nil] if no cache is available and routing should continue
      # @see Commands::Base#cache
      def check_static_cache
        return nil unless adapter.document_root
        cache_path = File.join(adapter.document_root, request.path.sub(/\.html$/, '') + '.html')
        cache_path = cache_path.sub(%r{/\.html$}, '.html')
        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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
yard-0.6.8 lib/yard/server/static_caching.rb
yard-0.6.7 lib/yard/server/static_caching.rb
yard-0.6.6 lib/yard/server/static_caching.rb
yard-0.6.5 lib/yard/server/static_caching.rb
yard-0.6.4 lib/yard/server/static_caching.rb