-
-
Save JaceEgg/a4150b8f360110d1cb56a5744aeca4d4 to your computer and use it in GitHub Desktop.
XTDB Ring Session Store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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