Skip to content

Instantly share code, notes, and snippets.

@JaceEgg
Last active July 17, 2024 05:31
Show Gist options
  • Select an option

  • Save JaceEgg/a4150b8f360110d1cb56a5744aeca4d4 to your computer and use it in GitHub Desktop.

Select an option

Save JaceEgg/a4150b8f360110d1cb56a5744aeca4d4 to your computer and use it in GitHub Desktop.
XTDB Ring Session Store
(ns com.eelchat.sessionstore
"A session storage engine that stores session data in XTDB."
(:require [com.biffweb :as biff]
[ring.middleware.session.store :refer [SessionStore]]
[xtdb.api :as xt]))
(deftype XtdbStore [ctx]
SessionStore
(read-session [_ key]
(let [{:keys [biff.xtdb/node]} ctx]
(if key (:session/data (xt/entity (xtdb.api/db node) (parse-uuid key))) nil)))
(write-session [_ key data]
(let [key (or (if key (parse-uuid key) nil) (random-uuid))]
(biff/submit-tx ctx
[{:db/doc-type :session
:xt/id key
:session/data data}])
key))
(delete-session [_ key]
(biff/submit-tx ctx
[{:db/op :delete
:db/doc-type :session
:xt/id (parse-uuid key)}])
nil))
(ns-unmap *ns* '->XtdbStore)
(defn xtdb-store
([ctx] (XtdbStore. ctx)))
;;after use-xtdb before use-jetty
(defn use-xtdb-session-store [ctx]
(assoc ctx :biff.middleware/session-store (xtdb-store ctx)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment