Class: NicInfo::Cache
- Inherits:
-
Object
- Object
- NicInfo::Cache
- Defined in:
- lib/nicinfo/cache.rb
Instance Method Summary collapse
- #clean ⇒ Object
- #count ⇒ Object
-
#create(url, data) ⇒ Object
creates an object in the cache.
-
#create_or_update(url, data) ⇒ Object
creates or updates an object in the cache.
- #empty ⇒ Object
- #get(url) ⇒ Object
-
#initialize(config) ⇒ Cache
constructor
A new instance of Cache.
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
#clean ⇒ Object
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 |
#count ⇒ Object
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 |
#empty ⇒ Object
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 |