On this page:
current-session-manager
current-session-id
session-manager?
make-session-manager-factory
session-ref
session-manager-ref
session-set!
session-manager-set!
session-update!
session-manager-update!
session-remove!
session-manager-remove!
wrap-session
25.1 Session Stores
session-store?
memory-session-store?
make-memory-session-store

25 Sessions

 (require koyo/session) package: koyo-lib

This module exposes a component for storing and retrieving data from a browser session.

This parameter holds a reference to the current session manager.

Functions session-ref, session-set!, session-update! and session-remove! use this parameter under the hood to locate the manager.

The parameter is installed by wrap-session.

parameter

(current-session-id)  (or/c false/c non-empty-string?)

(current-session-id session-id)  void?
  session-id : (or/c false/c non-empty-string?)
This parameter holds the session id for the current request. If the current request handler is wrapped with wrap-session, then this is guaranteed not to be #f.

procedure

(session-manager? v)  boolean?

  v : any/c
Returns #t when v is a session manager.

procedure

(make-session-manager-factory 
  #:cookie-name cookie-name 
  [#:cookie-path cookie-path 
  #:cookie-secure? cookie-secure? 
  #:cookie-http-only? cookie-http-only? 
  #:cookie-same-site cookie-same-site] 
  #:shelf-life shelf-life 
  #:secret-key secret-key 
  #:store store) 
  (-> session-manager?)
  cookie-name : non-empty-string?
  cookie-path : path-string? = "/"
  cookie-secure? : boolean? = #t
  cookie-http-only? : boolean? = #t
  cookie-same-site : (or/c 'lax 'strict) = 'strict
  shelf-life : exact-positive-integer?
  secret-key : bytes?
  store : session-store?
Returns a function that will create a session manager in accordance with the given options.

procedure

(session-ref k)  any/c

  k : symbol?
(session-ref k d)  any/c
  k : symbol?
  d : any/c

procedure

(session-manager-ref sm k)  any/c

  sm : session-manager?
  k : symbol?
(session-manager-ref sm k d)  any/c
  sm : session-manager?
  k : symbol?
  d : any/c
Looks up k in the current session, returning d if the key is not found. If d is not provided, then a user error is raised.

procedure

(session-set! k v)  void?

  k : symbol?
  v : serializable?

procedure

(session-manager-set! sm k v)  void?

  sm : session-manager?
  k : symbol?
  v : serializable?
Stores v under the k key in the current session.

procedure

(session-update! k p)  serializable?

  k : symbol?
  p : (-> any/c serializable?)
(session-update! k p d)  serializable?
  k : symbol?
  p : (-> any/c serializable?)
  d : any/c

procedure

(session-manager-update! sm k p)  serializable?

  sm : session-manager?
  k : symbol?
  p : (-> any/c serializable?)
(session-manager-update! sm k p d)  serializable?
  sm : session-manager?
  k : symbol?
  p : (-> any/c serializable?)
  d : any/c
Updates k in the current session by applying p to it. If k is not set then d is used as the default value. If d is not provided, then a user error is raised.

procedure

(session-remove! k)  void?

  k : symbol?

procedure

(session-manager-remove! sm k)  void?

  sm : session-manager?
  k : symbol?
Removes k from the current session. Does nothing if k is not set in the current session.

procedure

(wrap-session sm)  
(-> (-> request? response?)
    (-> request? response?))
  sm : session-manager?
Wraps a handler such that the appropriate session information for the current visitor is loaded and, eventually, stored.

If the current visitor doesn’t have a session cookie then a new one is generated and added to the response.

Each session’s lifetime is extended with every page load.

25.1 Session Stores

Session stores decide how session data is stored.

procedure

(session-store? v)  boolean?

  v : any/c
Returns #t when v is a session store.

procedure

(memory-session-store? v)  boolean?

  v : any/c

procedure

(make-memory-session-store [#:ttl ttl    
  #:file-path path])  session-store?
  ttl : exact-positive-integer? = (* 7 86400)
  path : path-string? = (make-temporary-file)
A session store that keeps all session data in memory, persisting and loading it to/from disk on shutdown and startup.

ttl controls how many seconds to wait before removing stale sessions.