Sha256: 4ee47a0551646e4fe6b942249b894f8712069f70f946b49f579b58909e21eb61

Contents?: true

Size: 797 Bytes

Versions: 3

Compression:

Stored size: 797 Bytes

Contents

module AppCache
  class LocalFileCache
    def initialize(path = '')
      if ! path.empty?
        @file_path = path
      else
        @file_path = '/tmp'
      end
    end

    def set(key, value)
      FileUtils.mkdir_p("#{@file_path}")
      #File.write("#{@file_path}/#{key}", value)
      file = File.open("#{@file_path}/#{key}", "w")
      file.write(value)
      file.close
    end

    def get key
      has?(key) ? File.read("#{@file_path}/#{key}") : nil
    end

    def has? key
      File.exists?("#{@file_path}/#{key}")
    end

    def del key
      File.delete("#{@file_path}/#{key}") if has? key
    end

    def flush
      Dir.foreach(@file_path) do |item|
        next if item == '.' || item == '..'
        delete item
      end
      Dir.rmdir(@file_path)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
app_cache-0.1.2 lib/app_cache/local_file_cache.rb
app_cache-0.1.1 lib/app_cache/local_file_cache.rb
app_cache-0.1.0 lib/app_cache/local_file_cache.rb