cacheutils

dev-stylish
Matthew Butterick 6 years ago
parent 8f3de0d73c
commit a4c603d039

@ -10,17 +10,17 @@
compiler/cm) compiler/cm)
(provide (all-defined-out)) (provide (all-defined-out))
(define (paths->key source-path [template-path #f] [output-path #f])
;; can't use relative paths for cache keys because source files include `here-path` which is absolute. ;; can't use relative paths for cache keys because source files include `here-path` which is absolute.
;; problem is that cache could appear valid on another filesystem (based on relative pathnames & mod dates) ;; problem is that cache could appear valid on another filesystem (based on relative pathnames & mod dates)
;; but would actually be invalid (because the `here-path` names are wrong). ;; but would actually be invalid (because the `here-path` names are wrong).
;; key is list of file + mod-time pairs, use #f for missing ;; key is list of file + mod-time pairs, use #f for missing
;; we don't include output-path in path-strings-to-track ;; we don't include output-path in path-strings-to-track
;; because we don't want to attach a mod date ;; because we don't want to attach a mod date
;; because cache validity is not sensitive to mod date of output path ;; because cache validity is not sensitive to mod date of output path
;; (in fact we would expect it to be earlier, since we want to rely on an earlier version) ;; (in fact we would expect it to be earlier, since we want to rely on an earlier version)
(define path-strings-to-track (list* source-path (define (paths->key source-path [template-path #false] [output-path #false])
(define path-strings-to-track
(list* source-path
;; if template has a source file, track that instead ;; if template has a source file, track that instead
(and template-path (or (get-source template-path) template-path)) (and template-path (or (get-source template-path) template-path))
;; is either list of files or (list #f) ;; is either list of files or (list #f)
@ -33,40 +33,37 @@
(for/list ([ps (in-list path-strings-to-track)]) (for/list ([ps (in-list path-strings-to-track)])
(cond (cond
[ps (define cp (->complete-path ps)) [ps (define cp (->complete-path ps))
(cons (path->string cp) (file-or-directory-modify-seconds cp #f (λ () 0)))] (cons (path->string cp) (file-or-directory-modify-seconds cp #false (λ () 0)))]
[else #f]))) [else #false])))
(list* pollen-env poly-flag (and output-path (path->string output-path)) path+mod-time-pairs)) (list* pollen-env poly-flag (and output-path (path->string output-path)) path+mod-time-pairs))
(define (key->source-path key) (car (fourth key))) (define (key->source-path key) (car (fourth key)))
(define (key->output-path key) (third key)) (define (key->output-path key) (third key))
(module-test-internal (module-test-internal
(define ps "/users/nobody/project/source.html.pm") (define ps "/users/nobody/project/source.html.pm")
(check-equal? (key->source-path (paths->key ps)) ps)) (check-equal? (key->source-path (paths->key ps)) ps))
(define-namespace-anchor cache-module-ns)
(define-namespace-anchor cache-utils-module-ns)
(define (path->hash path) (define (path->hash path)
(for-each managed-compile-zo (or (get-directory-require-files path) null)) (for-each managed-compile-zo (or (get-directory-require-files path) null))
(define path-dir (dirname path))
(apply hasheq (apply hasheq
(let ([doc-key (setup:main-export)] (let ([doc-key (setup:main-export)] [meta-key (setup:meta-export)])
[meta-key (setup:meta-export)])
(unless (and (symbol? doc-key) (symbol? meta-key)) (unless (and (symbol? doc-key) (symbol? meta-key))
(raise-argument-error 'path->hash "symbols for doc and meta key" (cons doc-key meta-key))) (raise-argument-error 'path->hash "symbols for doc and meta key" (cons doc-key meta-key)))
;; new namespace forces `dynamic-require` to re-instantiate 'path'
;; otherwise it gets cached in current namespace.
(parameterize ([current-namespace (make-base-namespace)]
[current-directory path-dir])
;; I monkeyed around with using the metas submodule to pull out the metas (for speed) ;; I monkeyed around with using the metas submodule to pull out the metas (for speed)
;; but in practice most files get their doc requested too. ;; but in practice most files get their doc requested too.
;; so it's just simpler to get both at once and be done with it. ;; so it's just simpler to get both at once and be done with it.
;; the savings of avoiding two cache fetches at the outset outweighs ;; the savings of avoiding two cache fetches at the outset outweighs
;; the benefit of not reloading doc when you just need metas. ;; the benefit of not reloading doc when you just need metas.
(namespace-attach-module (namespace-anchor->namespace cache-utils-module-ns) 'pollen/setup) ; brings in params ;; new namespace forces `dynamic-require` to re-instantiate `path`
;; otherwise it gets cached in current namespace.
(parameterize ([current-namespace (make-base-namespace)]
[current-directory (dirname path)])
(namespace-attach-module
(namespace-anchor->namespace cache-module-ns)
'pollen/setup (current-namespace)) ; brings in currently instantiated params (unlike namespace-require)
(define doc-missing-thunk (λ () "")) (define doc-missing-thunk (λ () ""))
(define metas-missing-thunk (λ () (hasheq))) (define metas-missing-thunk (λ () (hasheq)))
(list doc-key (dynamic-require path doc-key doc-missing-thunk) (list doc-key (dynamic-require path doc-key doc-missing-thunk)
@ -96,16 +93,16 @@
(define-values (cache-dir private-cache-dir) (make-cache-dirs dest-path)) (define-values (cache-dir private-cache-dir) (make-cache-dirs dest-path))
(define-values (dest-path-dir dest-path-filename _) (split-path dest-path)) (define-values (dest-path-dir dest-path-filename _) (split-path dest-path))
(define dest-file (build-path cache-dir (format "~a.rktd" dest-path-filename))) (define dest-file (build-path cache-dir (format "~a.rktd" dest-path-filename)))
(define (fetch-dest-file) (write-to-file (path-hash-thunk) dest-file #:exists 'replace)) (define (fetch-dest-file)
#| (write-to-file (path-hash-thunk) dest-file #:exists 'replace))
`cache-file` looks for a file in private-cache-dir previously cached with key
(which in this case carries modification dates and POLLEN env). ;; `cache-file` looks for a file in private-cache-dir previously cached with key
If a cached file is found, copies it to dest-file (which must not exist already, unless exists-ok? is true) ;; (which in this case carries modification dates and POLLEN env).
Otherwise, fetch-dest-file is called; if dest-file exists after calling fetch-dest-file, ;; If a cached file is found, copies it to dest-file (which must not exist already, unless exists-ok? is true)
it is copied to private-cache-dir and recorded with key. ;; Otherwise, fetch-dest-file is called; if dest-file exists after calling fetch-dest-file,
|# ;; it is copied to private-cache-dir and recorded with key.
(cache-file dest-file (cache-file dest-file
#:exists-ok? #t #:exists-ok? #true
key key
private-cache-dir private-cache-dir
fetch-dest-file fetch-dest-file

@ -1 +1 @@
1540858414 1540858418

Loading…
Cancel
Save