From 0e1716827ef5d92e9548d1287640a553c1f35cfa Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Mon, 15 Sep 2014 18:33:34 -0700 Subject: [PATCH] add file-based cache (addresses issue #25) --- cache.rkt | 18 +++++++++++++++--- world.rkt | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/cache.rkt b/cache.rkt index 5f3ac3f..8c04dd8 100644 --- a/cache.rkt +++ b/cache.rkt @@ -1,16 +1,27 @@ #lang racket/base -(require racket/rerequire "world.rkt") +(require racket/rerequire racket/serialize racket/file "world.rkt") ;; The cache is a hash with paths as keys. ;; The cache values are also hashes, with key/value pairs for that path. (provide reset-cache current-cache make-cache cached-require cache-ref) -(define (make-cache) (make-hash)) +(define (get-cache-file-path) + (build-path (world:current-project-root) world:cache-filename)) + +(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) (hash-clear! (current-cache))) +(define (reset-cache) + (define cache-path (get-cache-file-path)) + (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))) @@ -28,6 +39,7 @@ (hash-set! cache-hash 'mod-time (file-or-directory-modify-seconds path)) (hash-set! cache-hash world:main-pollen-export (dynamic-require path world:main-pollen-export)) (hash-set! cache-hash world:meta-pollen-export (dynamic-require path world:meta-pollen-export)) + (write-to-file (serialize (current-cache)) (get-cache-file-path) #:exists 'replace) (void)) (define (cached-require path-string key) diff --git a/world.rkt b/world.rkt index 817e929..3fd67a5 100644 --- a/world.rkt +++ b/world.rkt @@ -19,6 +19,8 @@ (define mode-pagetree 'ptree) (define mode-template 'template) +(define cache-filename "pollen.cache") + (define decodable-extensions (list markup-source-ext pagetree-source-ext)) (define default-pagetree "index.ptree")