class XmlSimple::Cache
A simple cache for XML documents that were already transformed by xml_in.
Public Class Methods
Creates and initializes a new Cache
object.
# File lib/xmlsimple.rb, line 20 def initialize @mem_share_cache = {} @mem_copy_cache = {} end
Public Instance Methods
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.
# File lib/xmlsimple.rb, line 87 def restore_mem_copy(filename) data = get_from_memory_cache(filename, @mem_share_cache) data = Marshal.load(data) unless data.nil? data end
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.
# File lib/xmlsimple.rb, line 41 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
Copies a data structure to a memory cache.
- data
-
Data structure to be copied.
- filename
-
Name of the file belonging to the data structure.
# File lib/xmlsimple.rb, line 77 def save_mem_copy(data, filename) @mem_share_cache[filename] = [Time::now.to_i, Marshal.dump(data)] end
Saves a data structure into a file.
- data
-
Data structure to be saved.
- filename
-
Name of the file belonging to the data structure.
# File lib/xmlsimple.rb, line 31 def save_storable(data, filename) cache_file = get_cache_filename(filename) File.open(cache_file, "w+") { |f| Marshal.dump(data, f) } end
Private Instance Methods
Returns the “cache filename” belonging to a filename, i.e. the extension ‘.xml’ in the original filename will be replaced by ‘.stor’. If filename does not have this extension, ‘.stor’ will be appended.
- filename
-
Filename to get “cache filename” for.
# File lib/xmlsimple.rb, line 102 def get_cache_filename(filename) filename.sub(/(\.xml)?$/, '.stor') end
Returns a cache entry from a memory cache belonging to a certain filename. If no entry could be found for any reason, nil will be returned.
- filename
-
Name of the file the cache entry belongs to.
- cache
-
Memory cache to get entry from.
# File lib/xmlsimple.rb, line 114 def get_from_memory_cache(filename, cache) return nil unless cache[filename] return nil unless cache[filename][0] > File::mtime(filename).to_i return cache[filename][1] end