change to file-based compile cache (closes #70)
parent
7bfd403023
commit
21a46b546d
@ -1,58 +1,50 @@
|
|||||||
#lang racket/base
|
#lang racket/base
|
||||||
(require racket/rerequire racket/serialize racket/file "world.rkt")
|
(require racket/path racket/function racket/file file/cache sugar/coerce "project.rkt" "world.rkt" racket/rerequire "debug.rkt")
|
||||||
|
|
||||||
;; The cache is a hash with paths as keys.
|
;; The cache is a hash with paths as keys.
|
||||||
;; The cache values are also hashes, with key/value pairs for that path.
|
;; The cache values are also hashes, with key/value pairs for that path.
|
||||||
|
|
||||||
(provide reset-cache current-cache make-cache cached-require cache-ref)
|
(provide reset-cache cached-require path->key path->hash)
|
||||||
|
(provide (all-from-out racket/rerequire))
|
||||||
|
|
||||||
(define (get-cache-file-path)
|
(define (get-cache-dir)
|
||||||
(build-path (world:current-project-root) (world:current-cache-filename)))
|
(build-path (world:current-project-root) (world:current-cache-dir-name)))
|
||||||
|
|
||||||
(define (make-cache)
|
|
||||||
(define cache-file-path (get-cache-file-path))
|
|
||||||
(if (file-exists? cache-file-path)
|
|
||||||
(deserialize (file->value cache-file-path))
|
|
||||||
(make-hash)))
|
|
||||||
|
|
||||||
(define current-cache (make-parameter (make-cache)))
|
|
||||||
|
|
||||||
(define (reset-cache)
|
(define (reset-cache)
|
||||||
(define cache-path (get-cache-file-path))
|
(cache-remove #f (get-cache-dir)))
|
||||||
(when (file-exists? cache-path)
|
|
||||||
(delete-file cache-path))
|
|
||||||
(current-cache (make-cache)))
|
|
||||||
|
|
||||||
(define (->complete-path path-string)
|
|
||||||
(path->complete-path (if (string? path-string) (string->path path-string) path-string)))
|
|
||||||
|
|
||||||
(define (cache-ref path-string)
|
(define (path->key source-path [template-path #f])
|
||||||
(hash-ref (current-cache) (->complete-path path-string)))
|
;; key is list of file + mod-time pairs
|
||||||
|
(define path-strings (map (compose1 ->string ->complete-path)
|
||||||
|
(append (list source-path)
|
||||||
|
(if template-path (list template-path) null)
|
||||||
|
(or (get-directory-require-files source-path) null))))
|
||||||
|
(map cons path-strings (map file-or-directory-modify-seconds path-strings)))
|
||||||
|
|
||||||
(define (cache-has-key? path)
|
|
||||||
(hash-has-key? (current-cache) path))
|
|
||||||
|
|
||||||
(define (cache path)
|
(define (path->hash path)
|
||||||
(dynamic-rerequire path)
|
(dynamic-rerequire path)
|
||||||
(hash-set! (current-cache) path (make-hash))
|
(hash (world:current-main-export) (dynamic-require path (world:current-main-export))
|
||||||
(define cache-hash (cache-ref path))
|
(world:current-meta-export) (dynamic-require path (world:current-meta-export))))
|
||||||
(hash-set! cache-hash 'mod-time (file-or-directory-modify-seconds path))
|
|
||||||
(hash-set! cache-hash (world:current-main-export) (dynamic-require path (world:current-main-export)))
|
|
||||||
(hash-set! cache-hash (world:current-meta-export) (dynamic-require path (world:current-meta-export)))
|
(define (cached-require path-string subkey)
|
||||||
(write-to-file (serialize (current-cache)) (get-cache-file-path) #:exists 'replace)
|
(define path (with-handlers ([exn:fail? (λ _ (error 'cached-require (format "~a is not a valid path" path-string)))])
|
||||||
(void))
|
(->complete-path path-string)))
|
||||||
|
|
||||||
(define (cached-require path-string key)
|
(when (not (file-exists? path))
|
||||||
(when (not (current-cache)) (error 'cached-require "No cache set up."))
|
(error (format "cached-require: ~a does not exist" path)))
|
||||||
|
|
||||||
(define path
|
(cond
|
||||||
(with-handlers ([exn:fail? (λ(exn) (error 'cached-require (format "~a is not a valid path" path-string)))])
|
[(world:current-compile-cache-active)
|
||||||
(->complete-path path-string)))
|
(define pickup-file (build-path (get-cache-dir) "pickup.rktd"))
|
||||||
|
(cache-file pickup-file #:exists-ok? #t
|
||||||
(when (not (file-exists? path)) (error (format "cached-require: ~a does not exist" (path->string path))))
|
(path->key path)
|
||||||
|
(get-cache-dir)
|
||||||
(when (or (not (cache-has-key? path))
|
(λ _ (write-to-file (path->hash path) pickup-file #:exists 'replace))
|
||||||
(> (file-or-directory-modify-seconds path) (hash-ref (cache-ref path) 'mod-time)))
|
#:max-cache-size (world:current-compile-cache-max-size))
|
||||||
(cache path))
|
(hash-ref (file->value pickup-file) subkey)]
|
||||||
|
[else ; cache inactive
|
||||||
(hash-ref (cache-ref path) key))
|
(dynamic-require path subkey)]))
|
||||||
|
@ -1 +0,0 @@
|
|||||||
test
|
|
Loading…
Reference in New Issue