cache.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2022 Simon Tournier <zimon.toutoune@gmail.com>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix cache)
  20. #:use-module ((guix utils) #:select (with-atomic-file-output))
  21. #:use-module (srfi srfi-19)
  22. #:use-module (srfi srfi-26)
  23. #:use-module (ice-9 match)
  24. #:use-module ((ice-9 textual-ports) #:select (get-string-all))
  25. #:export (obsolete?
  26. delete-file*
  27. file-expiration-time
  28. remove-expired-cache-entries
  29. maybe-remove-expired-cache-entries))
  30. ;;; Commentary:
  31. ;;;
  32. ;;; This module provides tools to manage a simple on-disk cache consisting of
  33. ;;; individual files.
  34. ;;;
  35. ;;; Code:
  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)))
  40. (define (delete-file* file)
  41. "Like 'delete-file', but does not raise an error when FILE does not exist."
  42. (catch 'system-error
  43. (lambda ()
  44. (delete-file file))
  45. (lambda args
  46. (unless (= ENOENT (system-error-errno args))
  47. (apply throw args)))))
  48. (define* (file-expiration-time ttl #:optional (timestamp stat:atime))
  49. "Return a procedure that, when passed a file, returns its \"expiration
  50. time\" computed as its timestamp + TTL seconds. Call TIMESTAMP to obtain the
  51. relevant timestamp from the result of 'stat'."
  52. (lambda (file)
  53. (match (stat file #f)
  54. (#f 0) ;FILE may have been deleted in the meantime
  55. (st (+ (timestamp st) ttl)))))
  56. (define* (remove-expired-cache-entries entries
  57. #:key
  58. (now (current-time time-monotonic))
  59. (entry-expiration
  60. (file-expiration-time 3600))
  61. (delete-entry delete-file*))
  62. "Given ENTRIES, a list of file names, remove those whose expiration time,
  63. as returned by ENTRY-EXPIRATION, has passed. Use DELETE-ENTRY to delete
  64. them."
  65. (for-each (lambda (entry)
  66. (when (<= (entry-expiration entry) (time-second now))
  67. (delete-entry entry)))
  68. entries))
  69. (define* (maybe-remove-expired-cache-entries cache
  70. cache-entries
  71. #:key
  72. (entry-expiration
  73. (file-expiration-time 3600))
  74. (delete-entry delete-file*)
  75. (cleanup-period (* 24 3600)))
  76. "Remove expired narinfo entries from the cache if deemed necessary. Call
  77. CACHE-ENTRIES with CACHE to retrieve the list of cache entries.
  78. ENTRY-EXPIRATION must be a procedure that, when passed an entry, returns the
  79. expiration time of that entry in seconds since the Epoch. DELETE-ENTRY is a
  80. procedure that removes the entry passed as an argument. Finally,
  81. CLEANUP-PERIOD denotes the minimum time between two cache cleanups."
  82. (define now
  83. (current-time time-monotonic))
  84. (define expiry-file
  85. (string-append cache "/last-expiry-cleanup"))
  86. (define last-expiry-date
  87. (catch 'system-error
  88. (lambda ()
  89. (or (string->number
  90. (call-with-input-file expiry-file get-string-all))
  91. 0))
  92. (const 0)))
  93. (when (obsolete? last-expiry-date now cleanup-period)
  94. (remove-expired-cache-entries (cache-entries cache)
  95. #:now now
  96. #:entry-expiration entry-expiration
  97. #:delete-entry delete-entry)
  98. (catch 'system-error
  99. (lambda ()
  100. (with-atomic-file-output expiry-file
  101. (cute write (time-second now) <>)))
  102. (lambda args
  103. ;; ENOENT means CACHE does not exist.
  104. (unless (= ENOENT (system-error-errno args))
  105. (apply throw args))))))
  106. ;;; cache.scm ends here