class Smeagol::Cache

Attributes

path[RW]

The path to the smeagol cache for this wiki.

wiki[R]

The cached wiki.

Public Class Methods

new(wiki) click to toggle source

Creates a cache object for a Gollum wiki.

wiki - The wiki to cache. [Wiki]

Returns cache. [Cache]

# File lib/smeagol/cache.rb, line 12
def initialize(wiki)
  @wiki = wiki
  @path = "#{Dir.tmpdir}/smeagol/#{File.expand_path(@wiki.path)}"
end

Public Instance Methods

cache_hit?(name, version='master') click to toggle source

Checks if a cache hit is found for a given gollum page.

name - The name of the page to check. [String] version - The version of the page to check. [String]

Returns true if the page has been cached, otherwise false. [Boolean]

# File lib/smeagol/cache.rb, line 42
def cache_hit?(name, version='master')
  page = wiki.page(name, version)
  File.exists?(page_path(name, version)) unless page.nil?
end
clear() click to toggle source

Clears the entire cache.

# File lib/smeagol/cache.rb, line 30
def clear
  FileUtils.rm_rf(path)
end
get_page(name, version='master') click to toggle source

Retrieves the content of the cached page.

name - The name of the wiki page. [String] version - The version of the page. [String]

Returns the contents of the HTML page if cached, otherwise nil. [String,nil]

# File lib/smeagol/cache.rb, line 55
def get_page(name, version='master')
  IO.read(page_path(name, version)) if cache_hit?(name, version)
end
page_path(name, version='master') click to toggle source

Retrieves the path to the cache for a given page.

name - The name of the wiki page. [String] version - The version of the page. [String]

Returns the file path to the cached wiki page. [String]

# File lib/smeagol/cache.rb, line 101
def page_path(name, version='master')
  page = wiki.page(name, version)
  if !page.nil?
    "#{path}/#{page.path}/#{page.version.id}"
  end
end
remove_page(name, version='master') click to toggle source

Removes the cached content for a page.

name - The name of the wiki page. [String] version - The version of the page. [String]

Returns nothing.

# File lib/smeagol/cache.rb, line 88
def remove_page(name, version='master')
  page = wiki.page(name, version)
  File.delete(page_path(name, version)) if !page.nil? && File.exists?(page_path(name, version))
end
set_page(name, version, content) click to toggle source

Sets the cached content for a page.

name - The name of the wiki page. [String] version - The version of the page. [String] content - The content to cache. [String]

Returns nothing.

# File lib/smeagol/cache.rb, line 68
def set_page(name, version, content)
  $stderr.puts "set page: #{name} : #{version.class}" unless $QUIET
  page = wiki.page(name, version)
  if !page.nil?
    path = page_path(name, version)
    FileUtils.mkdir_p(File.dirname(path))
    File.open(path, 'w') do |f|
      f.write(content)
    end
  end
end