(module write-through-cache mzscheme
(provide
(rename create-write-through-cache make-write-through-cache)
cache-get
cache-set!
cache-clear!)
(define-struct write-through-cache (load store hash))
(define (create-write-through-cache load store)
(make-write-through-cache load store (make-hash-table 'weak)))
(define (cache-get cache key)
(let ([hash (write-through-cache-hash cache)]
[load (write-through-cache-load cache)])
(hash-table-get hash
key
(lambda ()
(let ([value (load key)])
(hash-table-put! hash key value)
value)))))
(define (cache-set! cache key value)
(let ([hash (write-through-cache-hash cache)]
[store (write-through-cache-store cache)])
(hash-table-put! hash key value)
(store key value)))
(define (cache-clear! cache)
(let ([hash (write-through-cache-hash cache)])
(hash-table-for-each
hash
(lambda (k v) (hash-table-remove! hash k)))))
)