You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020 Ludovic Courtès <email@example.com>
;;; This file is part of GNU Guix.
;;; GNU Guix 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 Guix 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 Guix. If not, see <http://www.gnu.org/licenses/>.
(define-module (guix cache)
#:use-module (srfi srfi-19)
#:use-module (srfi srfi-26)
#:use-module (ice-9 match)
;;; This module provides tools to manage a simple on-disk cache consisting of
;;; individual files.
(define (obsolete? date now ttl)
"Return #t if DATE is obsolete compared to NOW + TTL seconds."
(time>? (subtract-duration now (make-time time-duration 0 ttl))
(make-time time-monotonic 0 date)))
(define (delete-file* file)
"Like 'delete-file', but does not raise an error when FILE does not exist."
(unless (= ENOENT (system-error-errno args))
(apply throw args)))))
(define (file-expiration-time ttl)
"Return a procedure that, when passed a file, returns its \"expiration
time\" computed as its last-access time + TTL seconds."
(match (stat file #f)
(#f 0) ;FILE may have been deleted in the meantime
(st (+ (stat:atime st) ttl)))))
(define* (remove-expired-cache-entries entries
(now (current-time time-monotonic))
"Given ENTRIES, a list of file names, remove those whose expiration time,
as returned by ENTRY-EXPIRATION, has passed. Use DELETE-ENTRY to delete
(for-each (lambda (entry)
(when (<= (entry-expiration entry) (time-second now))
(define* (maybe-remove-expired-cache-entries cache
(cleanup-period (* 24 3600)))
"Remove expired narinfo entries from the cache if deemed necessary. Call
CACHE-ENTRIES with CACHE to retrieve the list of cache entries.
ENTRY-EXPIRATION must be a procedure that, when passed an entry, returns the
expiration time of that entry in seconds since the Epoch. DELETE-ENTRY is a
procedure that removes the entry passed as an argument. Finally,
CLEANUP-PERIOD denotes the minimum time between two cache cleanups."
(string-append cache "/last-expiry-cleanup"))
(call-with-input-file expiry-file read))
(when (obsolete? last-expiry-date now cleanup-period)
(remove-expired-cache-entries (cache-entries cache)
(cute write (time-second now) <>))))
;;; cache.scm ends here