1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix cache)
20 #:use-module (srfi srfi-19)
21 #:use-module (srfi srfi-26)
22 #:use-module (ice-9 match)
26 remove-expired-cache-entries
27 maybe-remove-expired-cache-entries))
31 ;;; This module provides tools to manage a simple on-disk cache consisting of
36 (define (obsolete? date now ttl)
37 "Return #t if DATE is obsolete compared to NOW + TTL seconds."
38 (time>? (subtract-duration now (make-time time-duration 0 ttl))
39 (make-time time-monotonic 0 date)))
41 (define (delete-file* file)
42 "Like 'delete-file', but does not raise an error when FILE does not exist."
47 (unless (= ENOENT (system-error-errno args))
48 (apply throw args)))))
50 (define* (file-expiration-time ttl #:optional (timestamp stat:atime))
51 "Return a procedure that, when passed a file, returns its \"expiration
52 time\" computed as its timestamp + TTL seconds. Call TIMESTAMP to obtain the
53 relevant timestamp from the result of 'stat'."
56 (#f 0) ;FILE may have been deleted in the meantime
57 (st (+ (timestamp st) ttl)))))
59 (define* (remove-expired-cache-entries entries
61 (now (current-time time-monotonic))
63 (file-expiration-time 3600))
64 (delete-entry delete-file*))
65 "Given ENTRIES, a list of file names, remove those whose expiration time,
66 as returned by ENTRY-EXPIRATION, has passed. Use DELETE-ENTRY to delete
68 (for-each (lambda (entry)
69 (when (<= (entry-expiration entry) (time-second now))
70 (delete-entry entry)))
73 (define* (maybe-remove-expired-cache-entries cache
77 (file-expiration-time 3600))
78 (delete-entry delete-file*)
79 (cleanup-period (* 24 3600)))
80 "Remove expired narinfo entries from the cache if deemed necessary. Call
81 CACHE-ENTRIES with CACHE to retrieve the list of cache entries.
83 ENTRY-EXPIRATION must be a procedure that, when passed an entry, returns the
84 expiration time of that entry in seconds since the Epoch. DELETE-ENTRY is a
85 procedure that removes the entry passed as an argument. Finally,
86 CLEANUP-PERIOD denotes the minimum time between two cache cleanups."
88 (current-time time-monotonic))
91 (string-append cache "/last-expiry-cleanup"))
93 (define last-expiry-date
96 (call-with-input-file expiry-file read))
99 (when (obsolete? last-expiry-date now cleanup-period)
100 (remove-expired-cache-entries (cache-entries cache)
102 #:entry-expiration entry-expiration
103 #:delete-entry delete-entry)
104 (call-with-output-file expiry-file
105 (cute write (time-second now) <>))))
107 ;;; cache.scm ends here