Skip to content

Commit dc9c00e

Browse files
authored
Merge pull request #24 from kimmyg/master
Selective cache flushing
2 parents 898195b + 3eafe90 commit dc9c00e

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

web-server-doc/web-server/scribblings/cache-table.scrbl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ functions.
2222
called to construct the value and add it to @racket[ct].
2323
}
2424

25-
@defproc[(cache-table-clear! [ct cache-table?])
25+
@defproc[(cache-table-clear! [ct cache-table?]
26+
[entry-ids (or/c false/c (listof symbol?)) #f])
2627
void?]{
27-
Clears all entries in @racket[ct].
28+
If @racket[entry-ids] is @racket[#f], clears all entries in @racket[ct].
29+
Otherwise, clears only the entries with keys in @racket[entry-ids].
30+
31+
@history[#:changed "6.9.0.1" "Added optional argument."]
2832
}
2933

3034
@defproc[(cache-table? [v any/c])

web-server-doc/web-server/scribblings/dispatch-servlets.scrbl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
@defproc[(make-cached-url->servlet
1919
[url->path url->path/c]
2020
[path->serlvet path->servlet/c])
21-
(values (-> void)
21+
(values (->* () ((or/c false/c (listof url?))) void?)
2222
url->servlet/c)]{
23-
The first return value flushes the cache. The second is a procedure
24-
that uses @racket[url->path] to resolve the URL to a path, then uses
23+
The first return value flushes the cache. If its optional argument is
24+
@racket[#f] (the default), all servlet caches are flushed. Otherwise,
25+
only those servlet caches to which @racket[url->path] maps the given
26+
URLs are flushed. The second return value is a procedure that uses
27+
@racket[url->path] to resolve the URL to a path, then uses
2528
@racket[path->servlet] to resolve that path to a servlet, caching the
2629
results in an internal table.
30+
31+
@history[#:changed "6.9.0.1" "Added optional argument to first return value."]
2732
}
2833

2934
@defproc[(make [url->servlet url->servlet/c]

web-server-lib/web-server/dispatchers/dispatch-servlets.rkt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323
[make-cached-url->servlet
2424
(-> url->path/c
2525
path->servlet/c
26-
(values (-> void)
26+
(values (() ((or/c false/c (listof url?))) . ->* . void?)
2727
url->servlet/c))])
2828

2929
(define (make-cached-url->servlet
3030
url->path
3131
path->servlet)
3232
(define config:scripts (make-cache-table))
33-
(values (lambda ()
33+
(values (lambda ([uris #f])
3434
;; This is broken - only out of date or specifically mentioned scripts should be flushed. This destroys persistent state!
35-
(cache-table-clear! config:scripts))
35+
(cache-table-clear!
36+
config:scripts
37+
(and uris
38+
(for/list ([uri (in-list uris)])
39+
(let-values ([(servlet-path _) (url->path uri)])
40+
(string->symbol (path->string servlet-path)))))))
3641
(lambda (uri)
3742
(define-values (servlet-path _)
3843
(with-handlers

web-server-lib/web-server/private/cache-table.rkt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
(make-cache-table (make-hasheq)
99
(make-semaphore 1)))
1010

11-
(define (cache-table-clear! ct)
11+
(define (cache-table-clear! ct [entry-ids #f])
1212
(call-with-semaphore
1313
(cache-table-semaphore ct)
1414
(lambda ()
15-
(set-cache-table-hash! ct (make-hasheq)))))
15+
(if entry-ids
16+
(let ([cache-hash (cache-table-hash ct)])
17+
(for ([entry-id (in-list entry-ids)])
18+
(hash-remove! cache-hash entry-id)))
19+
(set-cache-table-hash! ct (make-hasheq))))))
1620

1721
(define (cache-table-lookup! ct entry-id entry-thunk)
1822
(define ht (cache-table-hash ct))
@@ -31,5 +35,5 @@
3135
[rename new-cache-table make-cache-table
3236
(-> cache-table?)]
3337
[cache-table-lookup! (cache-table? symbol? (-> any/c) . -> . any/c)]
34-
[cache-table-clear! (cache-table? . -> . void?)]
38+
[cache-table-clear! ((cache-table?) ((or/c false/c (listof symbol?))) . ->* . void?)]
3539
[cache-table? (any/c . -> . boolean?)])

web-server-test/tests/web-server/private/cache-table-test.rkt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,33 @@
2828
(check-true (let ([ct (make-cache-table)])
2929
(cache-table-lookup! ct 'foo (lambda () #t))
3030
(cache-table-lookup! ct 'foo (lambda () #f)))))
31-
31+
3232
(test-case
3333
"cache-table-clear! is effective"
3434
(check-false (let ([ct (make-cache-table)])
3535
(cache-table-lookup! ct 'foo (lambda () #t))
3636
(cache-table-clear! ct)
37-
(cache-table-lookup! ct 'foo (lambda () #f)))))))
37+
(cache-table-lookup! ct 'foo (lambda () #f)))))
38+
39+
(test-case
40+
"cache-table-clear! is selective (1)"
41+
(check-true (let ([ct (make-cache-table)])
42+
(cache-table-lookup! ct 'foo (lambda () #t))
43+
(cache-table-lookup! ct 'bar (lambda () #t))
44+
(cache-table-clear! ct (list 'bar))
45+
(cache-table-lookup! ct 'foo (lambda () #f)))))
46+
47+
(test-case
48+
"cache-table-clear! is selective (2)"
49+
(check-false (let ([ct (make-cache-table)])
50+
(cache-table-lookup! ct 'foo (lambda () #t))
51+
(cache-table-lookup! ct 'bar (lambda () #t))
52+
(cache-table-clear! ct (list 'bar))
53+
(cache-table-lookup! ct 'bar (lambda () #f)))))
54+
55+
(test-case
56+
"cache-table-clear! is robust"
57+
(check-true (let ([ct (make-cache-table)])
58+
(cache-table-lookup! ct 'foo (lambda () #t))
59+
(cache-table-clear! ct (list 'bar 'baz))
60+
(cache-table-lookup! ct 'foo (lambda () #f)))))))

0 commit comments

Comments
 (0)