Sha256: ffe818a2a7643e01bd304e98ae23cf36e098383d7c5698c94f31273ed1a2e115

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

class PublicSuffixList

  class CacheFile

    def initialize(config)
      @config = config
    end

    def cache?
      @config.cache_dir && File.directory?(@config.cache_dir) && true
    end

    def file
      File.join(@config.cache_dir, URI.parse(@config.url).path.split("/").last + ".cache") if cache?
    end

    def exist?
      File.exist?(file) if cache?
    end

    def delete
      File.delete(file) if exist?
    end

    def load_data
      open(file, "r") { |f| @data = Marshal.load(f) } if exist?
    end

    def dump_data
      open(file, "w") { |f| Marshal.dump(@data, f) } if cache?
    end

    def data
      @data or (load_data and @data) or @data = {}
    end

    def [](key)
      data[key]
    end

    def []=(key, value)
      data.merge!({key => value, created_at: Time.now, tag: rand(36**8).to_s(36)}) and dump_data
    end

    def expired?
      !cache? or !([0, nil].include?(@config.cache_expiry_period) or data[:created_at] + @config.cache_expiry_period > Time.now)
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
public-suffix-list-0.2.1 lib/public_suffix_list/cache_file.rb
public-suffix-list-0.2.0 lib/public_suffix_list/cache_file.rb
public-suffix-list-0.1.6 lib/public_suffix_list/cache_file.rb