Skip to content

Commit

Permalink
add docs for store protocol. add sbcl :synchronized keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
K1D77A committed Nov 20, 2021
1 parent c194fb7 commit ade1774
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/middleware/session/store.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

(defstruct store)

(defgeneric fetch-session (store sid))
(defgeneric store-session (store sid session))
(defgeneric remove-session (store sid))
(defgeneric fetch-session (store sid)
(:documentation "Grab a session from STORE using SID."))

(defgeneric store-session (store sid session)
(:documentation "Store a SESSION in STORE using SID."))

(defgeneric remove-session (store sid)
(:documentation "Remove a session from STORE using SID."))
39 changes: 33 additions & 6 deletions src/middleware/session/store/memory.lisp
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
#||
Implement the protocol for storing, fetching, and removing a session from Clacks session
middleware
||#

(in-package :cl-user)
(defpackage lack.middleware.session.store.memory
(:nicknames :lack.session.store.memory)
(:use :cl
:lack.middleware.session.store)
:lack.middleware.session.store)
(:export :memory-store
:make-memory-store
:make-memory-store
:fetch-session
:store-session
:store-session
:remove-session))
(in-package :lack.middleware.session.store.memory)

;;;for SBCL use the synchronized hash table rather than manually grabbing and releasing
;;;a lock on each call.

#+sbcl
(defstruct (memory-store (:include store))
(stash (make-hash-table :test 'equal :synchronized t)))

#-sbcl
(defstruct (memory-store (:include store))
(stash (make-hash-table :test 'equal))
(lock (bordeaux-threads:make-lock "session store lock")))

#+sbcl
(defmethod fetch-session ((store memory-store) sid)
(gethash sid (memory-store-stash store)))

#-sbcl
(defmethod fetch-session ((store memory-store) sid)
(bordeaux-threads:with-lock-held ((memory-store-lock store))
(gethash sid (memory-store-stash store))))
(gethash sid (memory-store-stash store))))

#+sbcl
(defmethod store-session ((store memory-store) sid session)
(setf (gethash sid (memory-store-stash store)) session))

#-sbcl
(defmethod store-session ((store memory-store) sid session)
(bordeaux-threads:with-lock-held ((memory-store-lock store))
(setf (gethash sid (memory-store-stash store))
session)))
(setf (gethash sid (memory-store-stash store)) session)))

#+sbcl
(defmethod remove-session ((store memory-store) sid)
(remhash sid (memory-store-stash store)))

#-sbcl
(defmethod remove-session ((store memory-store) sid)
(bordeaux-threads:with-lock-held ((memory-store-lock store))
(remhash sid (memory-store-stash store))))

0 comments on commit ade1774

Please sign in to comment.