Class: Smeagol::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/smeagol/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki) ⇒ Cache

Creates a cache object for a Gollum wiki.

Parameters:

  • wiki

    The wiki to cache. [Wiki]


12
13
14
15
# File 'lib/smeagol/cache.rb', line 12

def initialize(wiki)
  @wiki = wiki
  @path = "#{Dir.tmpdir}/smeagol/#{File.expand_path(@wiki.path)}"
end

Instance Attribute Details

#pathObject

The path to the smeagol cache for this wiki.


25
26
27
# File 'lib/smeagol/cache.rb', line 25

def path
  @path
end

#wikiObject (readonly)

The cached wiki.


20
21
22
# File 'lib/smeagol/cache.rb', line 20

def wiki
  @wiki
end

Instance Method Details

#cache_hit?(name, version = 'master') ⇒ Boolean

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

Parameters:

  • name

    The name of the page to check. [String]

  • version (defaults to: 'master')

    The version of the page to check. [String]

Returns:

  • (Boolean)

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


42
43
44
45
# 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

#clearObject

Clears the entire cache.


30
31
32
# File 'lib/smeagol/cache.rb', line 30

def clear
  FileUtils.rm_rf(path)
end

#get_page(name, version = 'master') ⇒ String?

Retrieves the content of the cached page.

Parameters:

  • name

    The name of the wiki page. [String]

  • version (defaults to: 'master')

    The version of the page. [String]

Returns:

  • (String, nil)

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


55
56
57
# 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') ⇒ String

Retrieves the path to the cache for a given page.

Parameters:

  • name

    The name of the wiki page. [String]

  • version (defaults to: 'master')

    The version of the page. [String]

Returns:

  • (String)

    Returns the file path to the cached wiki page.


101
102
103
104
105
106
# 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') ⇒ void

This method returns an undefined value.

Removes the cached content for a page.

Parameters:

  • name

    The name of the wiki page. [String]

  • version (defaults to: 'master')

    The version of the page. [String]


88
89
90
91
# 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) ⇒ void

This method returns an undefined value.

Sets the cached content for a page.

Parameters:

  • name

    The name of the wiki page. [String]

  • version

    The version of the page. [String]

  • content

    The content to cache. [String]


68
69
70
71
72
73
74
75
76
77
78
# 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