Class: XmlSimple::Cache

Inherits:
Object show all
Defined in:
lib/action_controller/vendor/xml_simple.rb

Overview

A simple cache for XML documents that were already transformed by xml_in.

Instance Method Summary collapse

Constructor Details

#initializeCache

Creates and initializes a new Cache object.



19
20
21
22
# File 'lib/action_controller/vendor/xml_simple.rb', line 19

def initialize
  @mem_share_cache = {}
  @mem_copy_cache  = {}
end

Instance Method Details

#restore_mem_copy(filename) ⇒ Object

Restores a data structure from a memory cache. If restoring the data structure failed for any reason, nil will be returned.

filename

Name of the file belonging to the data structure.



86
87
88
89
90
# File 'lib/action_controller/vendor/xml_simple.rb', line 86

def restore_mem_copy(filename)
  data = get_from_memory_cache(filename, @mem_share_cache)
  data = Marshal.load(data) unless data.nil?
  data
end

#restore_mem_share(filename) ⇒ Object

Restores a data structure from a shared memory cache. You should consider these elements as “read only”. If restoring the data structure failed for any reason, nil will be returned.

filename

Name of the file belonging to the data structure.



66
67
68
# File 'lib/action_controller/vendor/xml_simple.rb', line 66

def restore_mem_share(filename)
  get_from_memory_cache(filename, @mem_share_cache)
end

#restore_storable(filename) ⇒ Object

Restores a data structure from a file. If restoring the data structure failed for any reason, nil will be returned.

filename

Name of the file belonging to the data structure.



40
41
42
43
44
45
46
47
# File 'lib/action_controller/vendor/xml_simple.rb', line 40

def restore_storable(filename)
  cache_file = get_cache_filename(filename)
  return nil unless File::exist?(cache_file)
  return nil unless File::mtime(cache_file).to_i > File::mtime(filename).to_i
  data = nil
  File.open(cache_file) { |f| data = Marshal.load(f) }
  data
end

#save_mem_copy(data, filename) ⇒ Object

Copies a data structure to a memory cache.

data

Data structure to be copied.

filename

Name of the file belonging to the data structure.



76
77
78
# File 'lib/action_controller/vendor/xml_simple.rb', line 76

def save_mem_copy(data, filename)
  @mem_share_cache[filename] = [Time::now.to_i, Marshal.dump(data)]
end

#save_mem_share(data, filename) ⇒ Object

Saves a data structure in a shared memory cache.

data

Data structure to be saved.

filename

Name of the file belonging to the data structure.



55
56
57
# File 'lib/action_controller/vendor/xml_simple.rb', line 55

def save_mem_share(data, filename)
  @mem_share_cache[filename] = [Time::now.to_i, data]
end

#save_storable(data, filename) ⇒ Object

Saves a data structure into a file.

data

Data structure to be saved.

filename

Name of the file belonging to the data structure.



30
31
32
33
# File 'lib/action_controller/vendor/xml_simple.rb', line 30

def save_storable(data, filename)
  cache_file = get_cache_filename(filename)
  File.open(cache_file, "w+") { |f| Marshal.dump(data, f) }
end