cache.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  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.
  10. ;;;
  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.
  15. ;;;
  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/>.
  18. (define-module (guix cache)
  19. #:use-module (srfi srfi-19)
  20. #:use-module (srfi srfi-26)
  21. #:use-module (ice-9 match)
  22. #:export (obsolete?
  23. delete-file*
  24. file-expiration-time
  25. remove-expired-cache-entries
  26. maybe-remove-expired-cache-entries))
  27. ;;; Commentary:
  28. ;;;
  29. ;;; This module provides tools to manage a simple on-disk cache consisting of
  30. ;;; individual files.
  31. ;;;
  32. ;;; Code:
  33. (define (obsolete? date now ttl)
  34. "Return #t if DATE is obsolete compared to NOW + TTL seconds."
  35. (time>? (subtract-duration now (make-time time-duration 0 ttl))
  36. (make-time time-monotonic 0 date)))
  37. (define (delete-file* file)
  38. "Like 'delete-file', but does not raise an error when FILE does not exist."
  39. (catch 'system-error
  40. (lambda ()
  41. (delete-file file))
  42. (lambda args
  43. (unless (= ENOENT (system-error-errno args))
  44. (apply throw args)))))
  45. (define* (file-expiration-time ttl #:optional (timestamp stat:atime))
  46. "Return a procedure that, when passed a file, returns its \"expiration
  47. time\" computed as its timestamp + TTL seconds. Call TIMESTAMP to obtain the
  48. relevant timestamp from the result of 'stat'."
  49. (lambda (file)
  50. (match (stat file #f)
  51. (#f 0) ;FILE may have been deleted in the meantime
  52. (st (+ (timestamp st) ttl)))))
  53. (define* (remove-expired-cache-entries entries
  54. #:key
  55. (now (current-time time-monotonic))
  56. (entry-expiration
  57. (file-expiration-time 3600))
  58. (delete-entry delete-file*))
  59. "Given ENTRIES, a list of file names, remove those whose expiration time,
  60. as returned by ENTRY-EXPIRATION, has passed. Use DELETE-ENTRY to delete
  61. them."
  62. (for-each (lambda (entry)
  63. (when (<= (entry-expiration entry) (time-second now))
  64. (delete-entry entry)))
  65. entries))
  66. (define* (maybe-remove-expired-cache-entries cache
  67. cache-entries
  68. #:key
  69. (entry-expiration
  70. (file-expiration-time 3600))
  71. (delete-entry delete-file*)
  72. (cleanup-period (* 24 3600)))
  73. "Remove expired narinfo entries from the cache if deemed necessary. Call
  74. CACHE-ENTRIES with CACHE to retrieve the list of cache entries.
  75. ENTRY-EXPIRATION must be a procedure that, when passed an entry, returns the
  76. expiration time of that entry in seconds since the Epoch. DELETE-ENTRY is a
  77. procedure that removes the entry passed as an argument. Finally,
  78. CLEANUP-PERIOD denotes the minimum time between two cache cleanups."
  79. (define now
  80. (current-time time-monotonic))
  81. (define expiry-file
  82. (string-append cache "/last-expiry-cleanup"))
  83. (define last-expiry-date
  84. (catch 'system-error
  85. (lambda ()
  86. (call-with-input-file expiry-file read))
  87. (const 0)))
  88. (when (obsolete? last-expiry-date now cleanup-period)
  89. (remove-expired-cache-entries (cache-entries cache)
  90. #:now now
  91. #:entry-expiration entry-expiration
  92. #:delete-entry delete-entry)
  93. (catch 'system-error
  94. (lambda ()
  95. (call-with-output-file expiry-file
  96. (cute write (time-second now) <>)))
  97. (lambda args
  98. ;; ENOENT means CACHE does not exist.
  99. (unless (= ENOENT (system-error-errno args))
  100. (apply throw args))))))
  101. ;;; cache.scm ends here