Class: XmlSimple::Cache

### Home Classes Methods

### In Files

- lib/xmlsimple.rb

### Parent

Object

### Methods

- ::new
- #get_cache_filename
- #get_from_memory_cache
- #restore_mem_copy
- #restore_mem_share
- #restore_storable
- #save_mem_copy
- #save_mem_share
- #save_storable

### Class Index

Quicksearch

- XmlSimple
- XmlSimple::Cache

No matching classes.

# XmlSimple::Cache

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

### Public Class Methods

new() click to toggle source

Creates and initializes a new Cache object.

        # File lib/xmlsimple.rb, line 20
    20:     def initialize
    21:       @mem_share_cache = {}
    22:       @mem_copy_cache  = {}
    23:     end

### Public Instance Methods

restore_mem_copy(filename) click to toggle source

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
    87:     def restore_mem_copy(filename)
    88:       data = get_from_memory_cache(filename, @mem_share_cache)
    89:       data = Marshal.load(data) unless data.nil?
    90:       data
    91:     end

restore_mem_share(filename) click to toggle source

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.

        # File lib/xmlsimple.rb, line 67
    67:     def restore_mem_share(filename)
    68:       get_from_memory_cache(filename, @mem_share_cache)
    69:     end

restore_storable(filename) click to toggle source

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
    41:     def restore_storable(filename)
    42:       cache_file = get_cache_filename(filename)
    43:       return nil unless File::exist?(cache_file)
    44:       return nil unless File::mtime(cache_file).to_i > File::mtime(filename).to_i
    45:       data = nil
    46:       File.open(cache_file) { |f| data = Marshal.load(f) }
    47:       data
    48:     end

save_mem_copy(data, filename) click to toggle source

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
    77:     def save_mem_copy(data, filename)
    78:       @mem_share_cache[filename] = [Time::now.to_i, Marshal.dump(data)]
    79:     end

save_mem_share(data, filename) click to toggle source

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.

        # File lib/xmlsimple.rb, line 56
    56:     def save_mem_share(data, filename)
    57:       @mem_share_cache[filename] = [Time::now.to_i, data]
    58:     end

save_storable(data, filename) click to toggle source

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
    31:     def save_storable(data, filename)
    32:       cache_file = get_cache_filename(filename)
    33:       File.open(cache_file, "w+") { |f| Marshal.dump(data, f) }
    34:     end

### Private Instance Methods

get_cache_filename(filename) click to toggle source

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
    102:     def get_cache_filename(filename)
    103:       filename.sub(/(\.xml)?$/, '.stor')
    104:     end

get_from_memory_cache(filename, cache) click to toggle source

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
    114:     def get_from_memory_cache(filename, cache)
    115:       return nil unless cache[filename]
    116:       return nil unless cache[filename][0] > File::mtime(filename).to_i
    117:       return cache[filename][1]
    118:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.