cache.scm 4.4 KB

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