       (assoc :session (dissoc session :flash)))))
 
 (defn- set-session-cookie
-  "Set the session cookie on the response."
-  [request response]
-  (let [new?   (:new-session? request)
-        cookie (session-cookie new? (:session response))
-        update (set-cookie :compojure-session cookie, :path "/")]
+  "Set the session cookie on the response if required."
+  [request response session]
+  (let [new?    (:new-session? request)
+        cookie  (session-cookie new? session)
+        update  (set-cookie :compojure-session cookie, :path "/")]
     (if cookie
       (update-response request response update)
       response)))
+
+(defn- save-handler-session
+  "Save the session for a handler if required."
+  [request response session]
+  (when (or (:session response)
+            (:new-session? request)
+            (not-empty (:flash request)))
+    (write-session session)))
 
 (defn with-session
   "Wrap a handler in a session of the specified type. Session type defaults to

(defn- get-request-session
  "Retrieve the session using the 'session' cookie in the request."
  [request]
  (if-let [session-data (-> request :cookies :session)]
    (read-session session-data)))

(defn- assoc-request-session

        (let [request  (-> request assoc-cookies
                                   assoc-request-session
                                   assoc-request-flash)
              response (handler request)
              session  (or (:session response) (:session request))]
          (when response
            (save-handler-session request response session)
            (set-session-cookie   request response session)))))))

;; User functions for modifying the session

