Class: Smeagol::Cache
- Inherits:
-
Object
- Object
- Smeagol::Cache
- Defined in:
- lib/smeagol/cache.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
The path to the smeagol cache for this wiki.
-
#wiki ⇒ Object
readonly
The cached wiki.
Instance Method Summary collapse
-
#cache_hit?(name, version = 'master') ⇒ Boolean
Checks if a cache hit is found for a given gollum page.
-
#clear ⇒ Object
Clears the entire cache.
-
#get_page(name, version = 'master') ⇒ String?
Retrieves the content of the cached page.
-
#initialize(wiki) ⇒ Cache
constructor
Creates a cache object for a Gollum wiki.
-
#page_path(name, version = 'master') ⇒ String
Retrieves the path to the cache for a given page.
-
#remove_page(name, version = 'master') ⇒ void
Removes the cached content for a page.
-
#set_page(name, version, content) ⇒ void
Sets the cached content for a page.
Constructor Details
#initialize(wiki) ⇒ Cache
Creates a cache object for a Gollum wiki.
12 13 14 15 |
# File 'lib/smeagol/cache.rb', line 12 def initialize(wiki) @wiki = wiki @path = "#{Dir.tmpdir}/smeagol/#{File.(@wiki.path)}" end |
Instance Attribute Details
#path ⇒ Object
The path to the smeagol cache for this wiki.
25 26 27 |
# File 'lib/smeagol/cache.rb', line 25 def path @path end |
#wiki ⇒ Object (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.
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 |
#clear ⇒ Object
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.
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.
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.
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.
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 |