Class: NicInfo::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/nicinfo/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Cache

Returns a new instance of Cache.



24
25
26
# File 'lib/nicinfo/cache.rb', line 24

def initialize config
  @config = config
end

Instance Method Details

#cleanObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nicinfo/cache.rb', line 67

def clean
  cache_files = Dir::entries(@config.rdap_cache_dir)
  eviction = Time.now - @config.config[ NicInfo::CACHE ][ NicInfo::CACHE_EVICTION ]
  eviction_count = 0
  cache_files.each do |file|
    full_file_name = File.join(@config.rdap_cache_dir, file)
    if !file.start_with?(".") && (File.mtime(full_file_name) < eviction)
      @config.logger.trace("Evicting " + full_file_name)
      File::unlink(full_file_name)
      eviction_count += 1
    end
  end
  @config.logger.trace("Evicted " + eviction_count.to_s + " files from the cache")
  return eviction_count
end

#countObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/nicinfo/cache.rb', line 98

def count
  count = 0
  cache_files = Dir::entries(@config.rdap_cache_dir)
  cache_files.each do |file|
    if !file.start_with?(".")
      count += 1
    end
  end
  return count
end

#create(url, data) ⇒ Object

creates an object in the cache. if the object already exists in the cache, this does nothing.



40
41
42
43
44
45
46
# File 'lib/nicinfo/cache.rb', line 40

def create url, data
  safe = NicInfo::make_safe(url)
  file_name = File.join(@config.rdap_cache_dir, safe)
  expiry = Time.now - @config.config[ NicInfo::CACHE ][ NicInfo::CACHE_EXPIRY ]
  return if (File.exist?(file_name) && File.mtime(file_name) > expiry)
  create_or_update(url, data)
end

#create_or_update(url, data) ⇒ Object

creates or updates an object in the cache



29
30
31
32
33
34
35
36
# File 'lib/nicinfo/cache.rb', line 29

def create_or_update url, data
  return nil if @config.config[ NicInfo::CACHE ][ NicInfo::USE_CACHE ] == false
  safe = NicInfo::make_safe(url)
  @config.logger.trace("Persisting " + url + " as " + safe)
  f = File.open(File.join(@config.rdap_cache_dir, safe), "w")
  f.puts data
  f.close
end

#emptyObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nicinfo/cache.rb', line 83

def empty
  cache_files = Dir::entries(@config.rdap_cache_dir)
  eviction_count = 0
  cache_files.each do |file|
    full_file_name = File.join(@config.rdap_cache_dir, file)
    if !file.start_with?(".")
      @config.logger.trace("Evicting " + full_file_name)
      File::unlink(full_file_name)
      eviction_count += 1
    end
  end
  @config.logger.trace("Evicted " + eviction_count.to_s + " files from the cache")
  return eviction_count
end

#get(url) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nicinfo/cache.rb', line 48

def get url
  return nil if @config.config[ NicInfo::CACHE ][ NicInfo::USE_CACHE ] == false
  safe = NicInfo::make_safe(url)
  file_name = File.join(@config.rdap_cache_dir, safe)
  expiry = Time.now - @config.config[ NicInfo::CACHE ][ NicInfo::CACHE_EXPIRY ]
  if (File.exist?(file_name) && File.mtime(file_name) > expiry)
    @config.logger.trace("Getting " + url + " from cache.")
    f = File.open(file_name, "r")
    data = ''
    f.each_line do |line|
      data += line
    end
    f.close
    return data
  end
  #else
  return nil
end

#get_lastObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/nicinfo/cache.rb', line 109

def get_last
  cache_files = Dir::entries(@config.rdap_cache_dir)
  last_file = nil
  last_file_mtime = nil
  cache_files.each do |file|
    full_file_name = File.join(@config.rdap_cache_dir, file)
    if !file.start_with?(".")
      mtime = File.mtime(full_file_name)
      if last_file == nil
        last_file = full_file_name
        last_file_mtime = mtime
      elsif mtime > last_file_mtime
        last_file = full_file_name
        last_file_mtime = mtime
      end
    end
  end
  if last_file
    f = File.open(last_file, "r")
    data = ''
    f.each_line do |line|
      data += line
    end
    f.close
    return [last_file, last_file_mtime, data]
  end
  #else
  return nil
end