使用Ruby 1.9.3p0
下面是我的 ruby 脚本,用于从我的 GMail 帐户中显示所有文件夹的名称和每个邮箱中的邮件数。
红宝石脚本
ENV['SSL_CERT_FILE']='/etc/ssl/certs/cacert.pem'
begin_ = Time.now
require 'net/imap'
server='imap.gmail.com'
port = 993
username = '<MY-GMAIL-USER-ID>'
password = '<MY-PASSWORD>'
imap = Net::IMAP.new(server,port,true)
imap.login(username, password)
list = imap.list("", "*")
puts "Gathering Data"
folders_count_hash = Hash[ list.map do |a|
[a.name, imap.status(a.name, ["MESSAGES"])["MESSAGES"]] unless a.name == "[Gmail]"
end ]
total_time = Time.now - begin_
puts "Gathered Data"
puts "Total Folders :#{folders_count_hash.size}"
puts "RUNTIME: #{total_time}"
folders_count_hash.each do |name, count|
puts "#{name} (#{count})"
end
imap.logout()
imap.disconnect()
上述脚本的输出:
Gathering Data
Gathered Data
Total Folders :193
RUNTIME: 189.38250329
从输出的 RUNTIME 值可以看出,收集数据需要 3 分钟。
我需要了解这种缓慢行为背后的原因。是因为我帐户中的文件夹太多,还是因为网络速度或GMail返回响应?
可以减少这种延迟吗?如果是,那么我怎样才能达到同样的效果。
谢谢,
吉涅什