[plt-scheme] Soft State in the PLT Web Server

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Mon May 18 16:37:00 EDT 2009

From: http://jay-mccarthy.blogspot.com/2009/05/soft-state-in-plt-web-server.html

Many Web applications and network protocols have values in the
continuation that are necessary to complete the computation, but that
may be recomputed if they are not available. This is "soft state".

For example, a Web application may cache a user's preferences from a
database and deliver it to a Web browser as a hidden value; when the
value is returned to the application in subsequent steps, it is used
to customize the display. However, if the preferences were not
available (or were corrupted in some way), the application could
retrieve them from the database.

When using the PLT Web Server's native continuations, this roughly
corresponds to the use of a weak box: a box that the GC is allowed to
erase the contents of. When using the PLT Web Server's serializable
continuations it roughly corresponds to a weak box and a weak hash
table (that holds its keys weakly) to give the box a serializable
value as an identifier.

This programming pattern is a bit difficult to get right. So, a
library that implements it is now provided with PLT Scheme:
web-server/lang/soft [1]

Here's a trivial example:

#lang web-server

(provide interface-version start)
(define interface-version 'stateless)

(define softie
  (soft-state
   (printf "Doing a long computation...~n")
   (sleep 1)))

(define (start req)
  (soft-state-ref softie)
  (printf "Done~n")
  (start
   (send/suspend
    (lambda (k-url)
      `(html (body (a ([href ,k-url]) "Done")))))))

1. http://faculty.cs.byu.edu/~jay/plt-doc/web-server/stateless.html#(part._.Soft_.State)

-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.