 (deftype CassandraStore [cache ttl]
   SessionStore
   (read-session [_ key]
+    ;; If the key is non-nil, try the cache or if that fails, try Cassandra.
+    ;; If both fail, or the key was nil, an empty session is returned.
     (or (and key
              (or (get (deref cache) key)
                  (read-session-data key)))
         {}))
+
   (write-session [this key data]
+    ;; Create a key, if necessary.
+    ;; Update the data in the database, except if it already contains a _last_db_write entry,
+    ;; and the time it specifies is younger than the current time minus TTL,
+    ;; and the session data has not changed within the handling of the request.
     (let [new-key (or key (str (UUID/randomUUID)))
           new-data (if-not (and (get data '_last_db_write)
-                                (< (- (System/currentTimeMillis) (* ttl 60000)) (data '_last_db_write))
+                                (< (- (System/currentTimeMillis) (* ttl 60000))
+                                   (data '_last_db_write))
                                 (= data (read-session this key)))
                      (assoc data '_last_db_write (System/currentTimeMillis))
                      data)]

;; file, You can obtain one at http://mozilla.org/MPL/2.0/.

(ns containium.ring-session-cassandra
  (:require [ring.middleware.session.store :refer (SessionStore read-session)]
            [taoensso.nippy :refer (freeze thaw)]
            [clojure.core.cache :refer (ttl-cache-factory)]



(defn- do-prepared
  "Execute a prepared CQLStatement, using the specified consistency (a
  keyword) and the positional arguments for the prepared query. If
  rows are returned by the query, an UntypedResultSet is returned,
  otherwise nil."
  [pp-query consistency & args]
  (let [consistency (case consistency
                      :any ConsistencyLevel/ANY
