# class CacheFileStore - RDoc Documentation

# class CacheFileStore

### Attributes

serializer[R]
storage_path[R]

### Public Class Methods

new(storage_path, serializer = Marshal) click to toggle source

The serializer must have the 2 methods .load and .dump

    (Marshal and YAML have them)

YAML is Human Readable, contrary to Marshal which store in a binary format Marshal does not need any “require”

    # File lib/common/cache_file_store.rb, line 19
    def initialize(storage_path, serializer = Marshal)
      @storage_path = File.expand_path(storage_path)
      @serializer = serializer

      # File.directory? for ruby <= 1.9 otherwise,
      # it makes more sense to do Dir.exist? :/
      unless File.directory?(@storage_path)
        Dir.mkdir(@storage_path)
      end
    end

### Public Instance Methods

clean() click to toggle source

    # File lib/common/cache_file_store.rb, line 30
    def clean
      Dir[File.join(@storage_path, '*')].each do |f|
        File.delete(f) unless File.symlink?(f)
      end
    end

get_entry_file_path(key) click to toggle source

    # File lib/common/cache_file_store.rb, line 52
    def get_entry_file_path(key)
      File::join(@storage_path, key)
    end

read_entry(key) click to toggle source

    # File lib/common/cache_file_store.rb, line 36
    def read_entry(key)
      entry_file_path = get_entry_file_path(key)

      if File.exists?(entry_file_path)
        return @serializer.load(File.read(entry_file_path))
      end
    end

write_entry(key, data_to_store, cache_ttl) click to toggle source

    # File lib/common/cache_file_store.rb, line 44
    def write_entry(key, data_to_store, cache_ttl)
      if cache_ttl > 0
        File.open(get_entry_file_path(key), 'w') do |f|
          f.write(@serializer.dump(data_to_store))
        end
      end
    end