On 8/13/2019 11:47 AM, 'Wayne Harris' via Racket Users wrote:
I'd like to save database results in memory because my database only
changes between long time intervals. By building a minimum
application, I see my cache-strategy seems to work per servlet: by
opening a new browser, the first request yields a cache-miss.
Looking through the documentation, I got the idea that perhaps I
should serve/servlet using
#:servlet-namespace '("shared-model.rkt")
where shared-model.rkt is the module that talks to the database and
implements the caching-strategy. But adding this keyword to
serve/servlet did not make any perceived difference.
What should I do to save results in memory across all requests?
AFAIK #:servlet-namespace isn't necessary - you can share data (via
access functions) among different instances of request handlers just by
requiring the modules (files) that define the common objects wherever
you need them.
If I'm not mistaken about the serve/servlet call in your code below, you
are relying on the application to open the new browser window ...
terminating and restarting the application each time. That clears your
"cache", guaranteeing that it misses the first time. You should set
#:launch-browser? #f , start the application and connect to it *from*
your browser with the URL http://localhost:1111.
That said:
What DBMS are you using? Server based DBMS like Oracle, Postgresql,
MySQL, SQLServer, etc. automatically cache query results in case the
same query is run again. If the server is well provisioned memory-wise,
it can take a long time for a popular query to age out the cache. If
your application is co-resident (on the same machine) with the server,
caching results yourself would be duplicating effort.
Hope this helps,
George
--- server.rkt
--------------------------------------------------------------------------
#lang racket/base
(require
web-server/servlet
web-server/servlet-env
(prefix-in model: "shared-model.rkt"))
(define-values (dispatch url)
(dispatch-rules
(("main") db->response)
(("update" (string-arg)) update->response)))
(define (update->response r s)
(define str (model:in-memory-db-set-and-get s))
(displayln str)
(string->response str))
(define (db->response r)
(string->response (model:get-in-memory-results)))
;; no more api endpoints ==/==
(define (string->response s)
(response/full 200 #"Okay" (current-seconds)
TEXT/HTML-MIME-TYPE '() (list (string->bytes/utf-8 s))))
(define (file-not-found r)
(response/xexpr "File not found."))
(module+ main
(file-stream-buffer-mode (current-output-port) 'line)
(define (main)
(displayln "Now serving...")
(serve/servlet dispatch
;; #:servlet-namespace '("shared-model.rkt")
#:stateless? #t
#:log-file (build-path "logs/httpd.log")
#:port 1111
#:listen-ip "127.0.0.1"
#:servlet-path "/"
#:servlet-regexp #rx""
#:extra-files-paths (list (build-path "pub/"))
#:server-root-path (build-path "/")
#:file-not-found-responder file-not-found))
(main))
---------------------------------------------------------------------------
--- shared-model.rkt
---------------------------------------------------------------------------
#lang racket/base
(define in-memory-database (make-parameter #f))
(define (in-memory-db-set-and-get x)
(in-memory-database (format "~a: data to be shared across servlets" x))
(in-memory-database))
(define (get-in-memory-results [refresh? #f])
(if refresh?
(begin (displayln "database: force refresh")
(in-memory-db-set-and-get "forced"))
(let ([in-memory-db (in-memory-database)])
(if in-memory-db
(begin (displayln "database: cache hit")
in-memory-db)
(begin (displayln "database: cache miss")
(in-memory-db-set-and-get "miss"))))))
(provide (all-defined-out))
---------------------------------------------------------------------------
--
You received this message because you are subscribed to the Google Groups "Racket
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to racket-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/73404053-1ed7-92c5-f9ad-16eed1af8e2c%40comcast.net.