class Scrapah::Cache

Public Class Methods

new() click to toggle source
# File lib/scrapah/cache.rb, line 14
def initialize()
        Dir.mkdir(@@cache_dir) unless File.exists?(@@cache_dir)
        @Cache = Hash.new
        @keep_time = 1*24*60 # in minutes
end

Public Instance Methods

clear() click to toggle source
# File lib/scrapah/cache.rb, line 32
def clear()
        @Cache = Hash.new
end
get(key) click to toggle source
# File lib/scrapah/cache.rb, line 24
def get(key)
        @Cache[key]
end
get_hash() click to toggle source
# File lib/scrapah/cache.rb, line 53
def get_hash
        @Cache
end
has_key?(key) click to toggle source
# File lib/scrapah/cache.rb, line 28
def has_key?(key)
        @Cache.has_key? key
end
load() click to toggle source
# File lib/scrapah/cache.rb, line 44
def load
        f = get_newest_acceptable
        @Cache = Hash.new
        @Cache = JSON.load(f) unless f.nil?
        f.close

        @Cache
end
save() click to toggle source
# File lib/scrapah/cache.rb, line 36
def save
        # WARNING: Symbols converted to Strings
        f = File.new(@@cache_dir+Time.now.to_i.to_s,'w')
        JSON.dump(@Cache,f)
        f.close
end
store(key,content) click to toggle source
# File lib/scrapah/cache.rb, line 20
def store(key,content)
        @Cache[key] = content
end

Private Instance Methods

get_newest_acceptable() click to toggle source
# File lib/scrapah/cache.rb, line 59
def get_newest_acceptable()
        prev = Dir.glob(@@cache_dir+'*')
        if(!prev.empty?)
                prev.map!{|f| f.delete(@@cache_dir).to_i}
                prev.sort!
                return File.new(@@cache_dir+prev.last.to_s,"r") if(Time.now.to_i-prev.last < @keep_time*60)
        end

        nil
end