Class: Webgen::Cache

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

Overview

A cache object provides access to various caches to speed up rendering of a website:

permanent

The permanent cache should be used for data that should be available between webgen runs.

volatile

The volatile cache is used for data that can easily be regenerated but might be expensive to do so. This cache is not stored between webgen runs.

standard

The standard cache saves data between webgen runs and returns the cached data (not the newly set data) if it is available. This is useful, for example, to store file modifcation times and check if a file has been changed between runs.

The standard cache should be accessed through the [] method which returns the correct value and the []= method should be used for setting the new value. However, if you really need to access a particular value of the old or new standard cache, you can use the accessors old_data and new_data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Create a new cache object.



39
40
41
42
43
44
# File 'lib/webgen/cache.rb', line 39

def initialize()
  @old_data = {}
  @new_data = {}
  @volatile = {}
  @permanent = {:classes => []}
end

Instance Attribute Details

#new_dataObject (readonly)

The cache data stored in the current webgen run.



36
37
38
# File 'lib/webgen/cache.rb', line 36

def new_data
  @new_data
end

#old_dataObject (readonly)

The cache data stored in the previous webgen run.



33
34
35
# File 'lib/webgen/cache.rb', line 33

def old_data
  @old_data
end

#permanentObject (readonly)

The permanent cache hash.



27
28
29
# File 'lib/webgen/cache.rb', line 27

def permanent
  @permanent
end

#volatileObject (readonly)

The volatile cache hash.



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

def volatile
  @volatile
end

Instance Method Details

#[](key) ⇒ Object

Return the cached data (or, if it is not available, the new data) identified by key from the standard cache.



48
49
50
51
52
53
54
# File 'lib/webgen/cache.rb', line 48

def [](key)
  if @old_data.has_key?(key)
    @old_data[key]
  else
    @new_data[key]
  end
end

#[]=(key, value) ⇒ Object

Store value identified by key in the standard cache.



57
58
59
# File 'lib/webgen/cache.rb', line 57

def []=(key, value)
  @new_data[key] = value
end

#dumpObject

Return all caches that should be available between webgen runs.



68
69
70
# File 'lib/webgen/cache.rb', line 68

def dump
  [@old_data.merge(@new_data), @permanent]
end

#instance(name) ⇒ Object

Return the unique instance of the class name. This method should be used when it is essential that webgen uses only one object of a class or when an object should automatically be recreated upon cache restoration (see #restore).



80
81
82
83
# File 'lib/webgen/cache.rb', line 80

def instance(name)
  @permanent[:classes] << name unless @permanent[:classes].include?(name)
  (@volatile[:classes] ||= {})[name] ||= constant(name).new
end

#reset_volatile_cacheObject

Reset the volatile cache.



73
74
75
# File 'lib/webgen/cache.rb', line 73

def reset_volatile_cache
  @volatile = {:classes => @volatile[:classes]}
end

#restore(data) ⇒ Object

Restore the caches from data and recreate all cached instances (see #instance).



62
63
64
65
# File 'lib/webgen/cache.rb', line 62

def restore(data)
  @old_data, @permanent = *data
  @permanent[:classes].each {|klass| instance(klass)}
end