我希望不同的处理程序在会话中设置不同的密钥而不会相互影响。我在this wiki article工作,建议使用assoc
。我以为我可以使用assoc-in
来更新会话中的路径。
(defn handler-one
[request]
(prn "Session before one" (:session request))
(-> (response "ONE")
(content-type "text/plain")
(#(assoc-in % [:session :key-one] "one"))))
(defn handler-two
[request]
(prn "Session before two" (:session request))
(-> (response "TWO")
(content-type "text/plain")
(#(assoc-in % [:session :key-two] "two"))))
如果我反复拨打handler-one
,则会打印Session before one {:key-one "one"}
,同样handler-two
会打印以前的会话值。
通过使用assoc-in
设置会话密钥,我希望设置两个密钥,即{:key-one "one" :key-two "two"}
。但似乎替换了整个会话字典。
我这样做错了吗?
答案 0 :(得分:3)
您正在请求中打印会话,但是您正在关联(不存在)会话作为响应,因此您最终只会使用最后添加的属性进行会话。您应该将会话从请求中删除,并将其关联到该会话中,然后将新会话作为响应的一部分返回。