unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Alex Bochannek <alex@bochannek.com>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: 49033@debbugs.gnu.org
Subject: bug#49033: 28.0.50; [PATCH] Feature suggestion, url-cache-expiry-alist to override expire time for cache pruning
Date: Tue, 15 Jun 2021 15:55:54 -0700	[thread overview]
Message-ID: <m2czsmagat.fsf@bochannek.com> (raw)
In-Reply-To: <87r1h3tdys.fsf@gnus.org> (Lars Ingebrigtsen's message of "Tue, 15 Jun 2021 16:11:23 +0200")

[-- Attachment #1: Type: text/plain, Size: 1165 bytes --]

Lars Ingebrigtsen <larsi@gnus.org> writes:

>> -                 ;; Twelve hours.
>> -                 (* 12 60 60))))
>> +                 gravatar-cache-ttl)))
>
> I don't mind that -- but is this really something that somebody would
> want to control?  It just seemed unlikely to me.

I tend to find it difficult to reason about functionality if constants
like this are in the code and not in variables. It may be unlikely that
many people will want to customize it, but I'd rather expose this as a
configuration variable than hide a static value in the code.


As far as the URL caching code is concerned, I cleaned it up a bit and
added some simple tests and documentation.

Support URL-specific cache expiration

	* test/lisp/url/url-cache-tests.el: Test URL-to-filename and
	filename-to-URL mappings used by URL caching.

	* lisp/url/url-cache.el (url-cache-expiry-alist)
	(url-cache-create-url-from-file, url-cache-expired)
	(url-cache-prune-cache): Expire cache entries based on regular
	expressions matching URLs defined in new customizable variable
	url-cache-expire-alist.

	* doc/misc/url.texi (Disk Caching): Mention
	url-cache-expiry-alist variable.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 5934 bytes --]

diff --git a/doc/misc/url.texi b/doc/misc/url.texi
index 8f15e11007..2ea34e0d03 100644
--- a/doc/misc/url.texi
+++ b/doc/misc/url.texi
@@ -923,6 +923,12 @@ Disk Caching
 expire-time argument of the function @code{url-cache-expired}.
 @end defopt
 
+@defopt url-cache-expiry-alist
+This variable is an alist of regular expressions matching @var{url}'s
+and their associated expiration delay in seconds.  It is used by the
+functions @code{url-cache-expired} and @code{url-cache-prune-cache}.
+@end defopt
+
 @defun url-fetch-from-cache
 This function takes a URL as its argument and returns a buffer
 containing the data cached for that URL.
diff --git a/lisp/url/url-cache.el b/lisp/url/url-cache.el
index 830e6ba9dc..48f315a5cc 100644
--- a/lisp/url/url-cache.el
+++ b/lisp/url/url-cache.el
@@ -38,6 +38,15 @@ url-cache-expire-time
   :type 'integer
   :group 'url-cache)
 
+(defcustom url-cache-expiry-alist nil
+  "Alist of URL regular expressions to override the `url-cache-expire-time'.
+The key is a string to be matched against the URL of the cached entry and the
+value is the expire time in seconds.  Only the protocol and hostname of the URL
+are available for matching."
+  :version "28.1"
+  :type 'alist
+  :group 'url-cache)
+
 ;; Cache manager
 (defun url-cache-file-writable-p (file)
   "Follows the documentation of `file-writable-p', unlike `file-writable-p'."
@@ -186,6 +195,31 @@ url-cache-create-filename
             (if (url-p url) url
               (url-generic-parse-url url)))))
 
+(defun url-cache-create-url-from-file (file)
+  (let* ((url-path-list
+	  (split-string
+	   (file-name-directory
+	    (string-trim-left file (concat "^.*/" (user-real-login-name))))
+	    "/" t))
+	 (protocol (pop url-path-list))
+	 (hostname
+	  (string-join (reverse url-path-list) "."))
+	 (url (string-join (list protocol hostname) "://")))
+    url))
+
+(defun url-cache-expiry-by-url (url)
+  (let ((expire-time
+	 (remove nil
+		 (mapcar
+		  (lambda (alist)
+		    (let ((key (car alist))
+			  (value (cdr alist)))
+		      (if (string-match
+			   key url)
+			  value)))
+		  url-cache-expiry-alist))))
+    (if (consp expire-time) (apply 'min expire-time) nil)))
+
 ;;;###autoload
 (defun url-cache-extract (fnam)
   "Extract FNAM from the local disk cache."
@@ -204,7 +238,9 @@ url-cache-expired
 	  (time-less-p
 	   (time-add
 	    cache-time
-	    (or expire-time url-cache-expire-time))
+	    (or expire-time
+		(url-cache-expiry-by-url url)
+		url-cache-expire-time))
 	   nil)))))
 
 (defun url-cache-prune-cache (&optional directory)
@@ -226,8 +262,10 @@ url-cache-prune-cache
 	   ((time-less-p
 	     (time-add
 	      (file-attribute-modification-time (file-attributes file))
-	      url-cache-expire-time)
-	     now)
+	      (or (url-cache-expiry-by-url
+		   (url-cache-create-url-from-file file))
+		  url-cache-expire-time))
+	      now)
 	    (delete-file file)
 	    (setq deleted-files (1+ deleted-files))))))
       (if (< deleted-files total-files)
diff --git a/test/lisp/url/url-cache-tests.el b/test/lisp/url/url-cache-tests.el
new file mode 100644
index 0000000000..f4e49ce3b9
--- /dev/null
+++ b/test/lisp/url/url-cache-tests.el
@@ -0,0 +1,76 @@
+;;; url-cache-tests.el --- Test suite for url-cache.  -*- lexical-binding:t -*-
+
+;; Copyright (C) 2021 Free Software Foundation, Inc.
+
+;; Author: Alex Bochannek <alex@bochannek.com>
+;; Keywords: data
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'url-cache)
+
+(ert-deftest url-cache-url-to-filename-tests ()
+  "Test the URL to filename resolution for the URL cache"
+  (should (equal (file-name-directory
+		  (url-cache-create-filename "http://www.fsf.co.uk"))
+		 (string-join
+		  (list url-cache-directory (user-real-login-name)
+			"http/uk/co/fsf/www/") "/")))
+  (should (equal (file-name-directory
+		  (url-cache-create-filename "https://www.fsf.co.uk"))
+		 (string-join
+		  (list url-cache-directory (user-real-login-name)
+			"https/uk/co/fsf/www/") "/")))
+  (should (equal (file-name-directory
+		  (url-cache-create-filename "http://host"))
+		 (string-join
+		  (list url-cache-directory (user-real-login-name)
+			"http/host/") "/")))
+  (should (equal (file-name-directory
+		  (url-cache-create-filename "http://host:80"))
+		 (string-join
+		  (list url-cache-directory (user-real-login-name)
+			"http/host/") "/")))
+  (should (equal (file-name-directory
+		  (url-cache-create-filename "http://host#fragment"))
+		 (string-join
+		  (list url-cache-directory (user-real-login-name)
+			"http/host/") "/"))))
+
+(ert-deftest url-cache-filename-to-url-tests ()
+  "Test the filename to URL resolution for the URL cache"
+  (should (equal (url-cache-create-url-from-file
+		  (string-join
+		   (list url-cache-directory (user-real-login-name)
+			 "http/uk/co/fsf/www/") "/"))
+		 "http://www.fsf.co.uk"))
+  (should (equal (url-cache-create-url-from-file
+		  (string-join
+		   (list url-cache-directory (user-real-login-name)
+			 "https/uk/co/fsf/www/") "/"))
+		 "https://www.fsf.co.uk"))
+  (should (equal (url-cache-create-url-from-file
+		  (string-join
+		   (list url-cache-directory (user-real-login-name)
+			 "http/host/") "/"))
+		 "http://host")))
+
+(provide 'url-cache-tests)
+
+;;; url-cache-tests.el ends here

[-- Attachment #3: Type: text/plain, Size: 10 bytes --]

-- 
Alex.

  reply	other threads:[~2021-06-15 22:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-15  5:40 bug#49033: 28.0.50; [PATCH] Feature suggestion, url-cache-expiry-alist to override expire time for cache pruning Alex Bochannek
2021-06-15 14:11 ` Lars Ingebrigtsen
2021-06-15 22:55   ` Alex Bochannek [this message]
2021-06-19 12:14     ` Lars Ingebrigtsen
2021-06-19 19:32       ` Alex Bochannek
2021-06-21 12:21         ` Lars Ingebrigtsen
2021-06-21 18:25           ` Alex Bochannek
2021-10-24  7:27             ` Stefan Kangas
2021-10-27 16:36               ` Alex Bochannek
2021-10-27 16:50                 ` Stefan Kangas
2021-06-19 12:56     ` Benjamin Riefenstahl
2021-06-19 19:24       ` Alex Bochannek

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=m2czsmagat.fsf@bochannek.com \
    --to=alex@bochannek.com \
    --cc=49033@debbugs.gnu.org \
    --cc=larsi@gnus.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).