This is an automated email from the ASF dual-hosted git repository.
sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git
The following commit(s) were added to refs/heads/master by this push:
new d01cd5f Add cache for parsed responses
d01cd5f is described below
commit d01cd5fb4fda479fe70d5452935ad07f4e65ad64
Author: Sebb <[email protected]>
AuthorDate: Fri Mar 8 00:41:04 2019 +0000
Add cache for parsed responses
---
lib/whimsy/asf/mlist.rb | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/lib/whimsy/asf/mlist.rb b/lib/whimsy/asf/mlist.rb
index f9f7ac6..3aff819 100644
--- a/lib/whimsy/asf/mlist.rb
+++ b/lib/whimsy/asf/mlist.rb
@@ -1,3 +1,5 @@
+require 'weakref'
+
module ASF
module MLIST
@@ -15,6 +17,9 @@ module ASF
# Note that the data files don't provide information on whether a list is
# public or private.
+ @@file_times = Hash.new # Key=type, value = modtime
+ @@file_parsed = Hash.new # Key=type, value = cache hash
+
# Return an array of board subscribers followed by the file update time
def self.board_subscribers
return list_filter('sub', 'apache.org', 'board'), (File.mtime(LIST_TIME)
rescue File.mtime(LIST_SUBS))
@@ -248,6 +253,24 @@ module ASF
else
raise ArgumentError.new('type: expecting dig, mod or sub')
end
+ ctime = @@file_times[type] || 0
+ mtime = File.mtime(path).to_i
+ if mtime <= ctime
+ cached = @@file_parsed[type]
+ if cached
+ begin
+ cached.each do |d,l,m|
+ yield d, l, m
+ end
+ return
+ rescue WeakRef::RefError
+ @@file_times[type] = 0
+ end
+ end
+ else
+ @@file_parsed[type] = nil
+ end
+ cache = Array.new # see if this preserves mod cache
# split file into paragraphs
File.read(path).split(/\n\n/).each do |stanza|
# domain may start in column 1 or following a '/'
@@ -260,13 +283,17 @@ module ASF
list = match[2].downcase # just in case
# Keep original case of email addresses
# TODO: a bit slow for subs file, implement cache of parsed file?
- yield dom, list, stanza.split(/\n/).select{|x| x =~ /@/}
+ mails = stanza.split(/\n/).select{|x| x =~ /@/}
+ cache << [dom, list, mails]
+ yield dom, list, mails
else
# don't allow mismatches as that means the RE is wrong
line=stanza[0..(stanza.index("\n")|| -1)]
raise ArgumentError.new("Unexpected section header #{line}")
end
end
+ @@file_parsed[type] = WeakRef.new(cache)
+ @@file_times[type] = mtime
nil # don't return file contents
end