From: Philip K <philip@warpmail.net>
To: 40355@debbugs.gnu.org
Subject: bug#40355: [PATCH] Implement caching for libravatar lookup
Date: Tue, 31 Mar 2020 20:03:36 +0200 [thread overview]
Message-ID: <20200331180336.2999-1-philip@warpmail.net> (raw)
Checking if a domain has an avatar server associated with it requires
at most two (synchronous) DNS queries, at least to ultimately default
to "libravatar.org". Caching the results of domain lookups reduced the
evaluation time (on my machine) from ~0.3s to an instantaneous
evaluation.
* lisp/image/gravatar.el (gravatar--service-libravatar): Check if
domain has already been resolved before staring DNS queries
(gravatar-libravatar-cache): New variable.
---
lisp/image/gravatar.el | 40 +++++++++++++++++++++++++++++++---------
1 file changed, 31 insertions(+), 9 deletions(-)
diff --git a/lisp/image/gravatar.el b/lisp/image/gravatar.el
index a9cd540aa4..2572f9136f 100644
--- a/lisp/image/gravatar.el
+++ b/lisp/image/gravatar.el
@@ -138,14 +138,24 @@ gravatar-service
:link '(url-link "https://gravatar.com/")
:group 'gravatar)
-(defun gravatar--service-libravatar (addr)
- "Find domain that hosts avatars for email address ADDR."
+(defvar gravatar-libravatar-cache (make-hash-table :test 'equal)
+ "Cache for `gravatar--service-libravatar'.")
+
+(defun gravatar--service-libravatar (addr &optional cache)
+ "Find domain that hosts avatars for email address ADDR.
+The optional argument CACHE must either be a hash table to
+memorise avatar server resolutions in, or nil, in which case it
+will default to `gravatar-libravatar-cache'."
;; implements https://wiki.libravatar.org/api/
(save-match-data
(if (not (string-match ".+@\\(.+\\)" addr))
"https://seccdn.libravatar.org/avatar"
- (let ((domain (match-string 1 addr)))
+ (let ((domain (downcase (match-string 1 addr))))
(catch 'found
+ (setq cache (or cache gravatar-libravatar-cache))
+ (let ((cache (gethash domain cache)))
+ (when (and cache (time-less-p (current-time) (cdr cache)))
+ (throw 'found (car cache))))
(dolist (record '(("_avatars-sec" . "https")
("_avatars" . "http")))
(let* ((query (concat (car record) "._tcp." domain))
@@ -173,12 +183,24 @@ gravatar--service-libravatar
(<= 1 (dns-get 'port rec) 65535)
(string-match-p "\\`[-.0-9A-Za-z]+\\'"
(dns-get 'target rec)))
- (throw 'found (format "%s://%s:%s/avatar"
- (cdr record)
- (dns-get 'target rec)
- (dns-get 'port rec))))
- (setq sum (- sum (dns-get 'weight rec)))))))
- "https://seccdn.libravatar.org/avatar"))))))
+ (let ((url (format "%s://%s:%s/avatar"
+ (cdr record)
+ (dns-get 'target rec)
+ (dns-get 'port rec)))
+ (timeout (if (time-less-p
+ (seconds-to-time (dns-get 'ttl rec))
+ (days-to-time 1))
+ (days-to-time 1)
+ (seconds-to-time (dns-get 'ttl rec)))))
+ (puthash domain
+ (cons url (time-add (current-time) timeout))
+ cache)
+ (throw 'found url)))
+ (setq sum (- sum (dns-get 'weight rec))))))))
+ (car (puthash domain
+ (cons "https://seccdn.libravatar.org/avatar"
+ (time-add (current-time) (days-to-time 30)))
+ cache)))))))
(defun gravatar-hash (mail-address)
"Return the Gravatar hash for MAIL-ADDRESS."
--
2.20.1
next reply other threads:[~2020-03-31 18:03 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-31 18:03 Philip K [this message]
2020-03-31 19:58 ` bug#40355: [PATCH] Implement caching for libravatar lookup Robert Pluim
2020-03-31 21:30 ` Philip K.
2020-04-01 7:27 ` Robert Pluim
2020-04-01 10:34 ` Philip K.
2020-08-08 13:03 ` Lars Ingebrigtsen
2020-08-18 14:46 ` Lars Ingebrigtsen
2020-08-24 14:09 ` Robert Pluim
2020-08-24 14:13 ` Lars Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200331180336.2999-1-philip@warpmail.net \
--to=philip@warpmail.net \
--cc=40355@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).