1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2013, 2014, 2015, 2016, 2017 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
38 ;; Guile 2.2.2 has a bug whereby 'time-monotonic' objects have seconds and
39 ;; nanoseconds swapped (fixed in Guile commit 886ac3e). Work around it.
40 (define time-monotonic time-tai))
43 (define (obsolete? date now ttl)
44 "Return #t if DATE is obsolete compared to NOW + TTL seconds."
45 (time>? (subtract-duration now (make-time time-duration 0 ttl))
46 (make-time time-monotonic 0 date)))
48 (define (delete-file* file)
49 "Like 'delete-file', but does not raise an error when FILE does not exist."
54 (unless (= ENOENT (system-error-errno args))
55 (apply throw args)))))
57 (define (file-expiration-time ttl)
58 "Return a procedure that, when passed a file, returns its \"expiration
59 time\" computed as its last-access time + TTL seconds."
62 (#f 0) ;FILE may have been deleted in the meantime
63 (st (+ (stat:atime st) ttl)))))
65 (define* (remove-expired-cache-entries entries
67 (now (current-time time-monotonic))
69 (file-expiration-time 3600))
70 (delete-entry delete-file*))
71 "Given ENTRIES, a list of file names, remove those whose expiration time,
72 as returned by ENTRY-EXPIRATION, has passed. Use DELETE-ENTRY to delete
74 (for-each (lambda (entry)
75 (when (<= (entry-expiration entry) (time-second now))
76 (delete-entry entry)))
79 (define* (maybe-remove-expired-cache-entries cache
83 (file-expiration-time 3600))
84 (delete-entry delete-file*)
85 (cleanup-period (* 24 3600)))
86 "Remove expired narinfo entries from the cache if deemed necessary. Call
87 CACHE-ENTRIES with CACHE to retrieve the list of cache entries.
89 ENTRY-EXPIRATION must be a procedure that, when passed an entry, returns the
90 expiration time of that entry in seconds since the Epoch. DELETE-ENTRY is a
91 procedure that removes the entry passed as an argument. Finally,
92 CLEANUP-PERIOD denotes the minimum time between two cache cleanups."
94 (current-time time-monotonic))
97 (string-append cache "/last-expiry-cleanup"))
99 (define last-expiry-date
102 (call-with-input-file expiry-file read))
105 (when (obsolete? last-expiry-date now cleanup-period)
106 (remove-expired-cache-entries (cache-entries cache)
108 #:entry-expiration entry-expiration
109 #:delete-entry delete-entry)
110 (call-with-output-file expiry-file
111 (cute write (time-second now) <>))))
113 ;;; cache.scm ends here