मैं एक रेक उपयोगिता बनाने की कोशिश कर रहा हूं जो हर बार मेरे डेटाबेस को अपडेट करेगा।कुशल थोक अद्यतन रेल डेटाबेस
namespace :utils do
# utils:update_ip
# Downloads the file frim <url> to the temp folder then unzips it in <file_path>
# Then updates the database.
desc "Update ip-to-country database"
task :update_ip => :environment do
require 'open-uri'
require 'zip/zipfilesystem'
require 'csv'
file_name = "ip-to-country.csv"
file_path = "#{RAILS_ROOT}/db/" + file_name
url = 'http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip'
#check last time we updated the database.
mod_time = ''
mod_time = File.new(file_path).mtime.httpdate if File.exists? file_path
begin
puts 'Downloading update...'
#send conditional GET to server
zipped_file = open(url, {'If-Modified-Since' => mod_time})
rescue OpenURI::HTTPError => the_error
if the_error.io.status[0] == '304'
puts 'Nothing to update.'
else
puts 'HTTPError: ' + the_error.message
end
else # file was downloaded without error.
Rails.logger.info 'ip-to-coutry: Remote database was last updated: ' + zipped_file.meta['last-modified']
delay = Time.now - zipped_file.last_modified
Rails.logger.info "ip-to-country: Database was outdated for: #{delay} seconds (#{delay/60/60/24 } days)"
puts 'Unzipping...'
File.delete(file_path) if File.exists? file_path
Zip::ZipFile.open(zipped_file.path) do |zipfile|
zipfile.extract(file_name, file_path)
end
Iptocs.delete_all
puts "Importing new database..."
# TODO: way, way too heavy find a better solution.
CSV.open(file_path, 'r') do |row|
ip = Iptocs.new( :ip_from => row.shift,
:ip_to => row.shift,
:country_code2 => row.shift,
:country_code3 => row.shift,
:country_name => row.shift)
ip.save
end #CSV
puts "Complete."
end #begin-resuce
end #task
end #namespace
समस्या मैं आ रही है इस 100 हजार जोड़ प्रविष्टियों में प्रवेश करने के कुछ ही मिनट लगते है जो:
इस कोड को मैं अब तक है। मैं अपना डेटाबेस अपडेट करने का एक और अधिक प्रभावी तरीका खोजना चाहता हूं। आदर्श रूप से यह डेटाबेस प्रकार से स्वतंत्र रहेगा, लेकिन यदि मेरा उत्पादन सर्वर MySQL पर नहीं चल रहा है।
किसी भी अंतर्दृष्टि के लिए धन्यवाद।
यह वही है जो मैं ढूंढ रहा था, धन्यवाद। – codr
मणि सीएसवी से आयात का समर्थन करता है। यह 'ActiveRecord' त्वरण और सत्यापन लागत को समाप्त करता है। अधिक जानकारी के लिए इस आलेख का संदर्भ लें। http://www.rubyinside.com/advent2006/17-extendingar.html –
भी मेरी मदद की - धन्यवाद! – ambertch