add ram cache for project-server sessions

pull/84/head
Matthew Butterick 9 years ago
parent 07279e5d67
commit 81fde08658

@ -1,5 +1,5 @@
#lang racket/base
(require racket/file file/cache sugar/coerce "project.rkt" "world.rkt" "rerequire.rkt")
(require racket/path racket/file file/cache sugar/coerce "project.rkt" "world.rkt" "rerequire.rkt")
;; The cache is a hash with paths as keys.
;; The cache values are also hashes, with key/value pairs for that path.
@ -7,21 +7,22 @@
(provide reset-cache cached-require paths->key)
(define (get-cache-dir)
(define cache-dir
(build-path (world:current-project-root) (world:current-cache-dir-name)))
(define (reset-cache)
(cache-remove #f (get-cache-dir)))
(cache-remove #f cache-dir))
(define (paths->key source-path [template-path #f])
;; 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 path-strings (append (list source-path)
(if template-path (list template-path) null)
(or (get-directory-require-files source-path) null)))
(define project-root (world:current-project-root))
(map (λ(ps) (define cp (->complete-path ps))
(cons (path->string (find-relative-path project-root cp)) (file-or-directory-modify-seconds cp))) path-strings))
@ -40,6 +41,8 @@
(world:current-meta-export) (dynamic-require path (world:current-meta-export)))))
(define ram-cache (make-hash))
(define (cached-require path-string subkey)
(define path (with-handlers ([exn:fail? (λ _ (error 'cached-require (format "~a is not a valid path" path-string)))])
(->complete-path path-string)))
@ -49,14 +52,15 @@
(cond
[(world:current-compile-cache-active)
(define pickup-file (build-path (get-cache-dir) "pickup.rktd"))
(let ([key (paths->key path)])
(cache-file pickup-file
#:exists-ok? #t
key
(get-cache-dir)
(λ _ (write-to-file (path->hash path) pickup-file #:exists 'replace))
#:max-cache-size (world:current-compile-cache-max-size)))
(hash-ref (file->value pickup-file) subkey)]
(define key (paths->key path))
(define pickup-file (build-path cache-dir "pickup.rktd"))
(hash-ref (hash-ref! ram-cache key (λ _
(cache-file pickup-file
#:exists-ok? #t
key
cache-dir
(λ _ (write-to-file (path->hash path) pickup-file #:exists 'replace))
#:max-cache-size (world:current-compile-cache-max-size))
(file->value pickup-file))) subkey)]
[else (parameterize ([current-namespace (make-base-namespace)])
(dynamic-require path subkey))]))
Loading…
Cancel
Save