4

セッションとフラッシュを Google App Engine で動作させる方法を考え出そうとしています。Ring または Sandbar のいずれかを使用して、誰かが明確な例を提供できますか? 私はサンドバーが機能していると思いますが、具体的にはそれを教えてくれませんVar sandbar.stateful-session/sandbar-flash is unbound.ハンドラーをダンプする:flash:session、それがサンドバーセッションなのかリングセッションなのかわかりません. 完全を期すために、appengine-magic、ring、hiccup、sandbar の最新バージョンを使用していることに言及します。非互換性や問題はないようです。

したがって、できれば を使用した明確な例ですflash-put!, flash-get, session-put! and session-get

4

2 に答える 2

4

私は通常、自分の質問に答えるのが好きではありませんが、この場合は例外を設けます。理由は次のとおりです。

a) 簡単に理解できる例はあまりありません。

b) 他の人が使用できる簡単な実例があるとよいでしょう。

注: ここでは appengine-magic は必要ありません。これは通常のリング セッションでも機能します。

コード

;; Place in a file called session.clj in the example project
(ns example.session
    "Works with the normal ring sessions 
     allowing you to use side-effects to manage them")

(declare current-session)

(defn wrap-session! [handler]
  (fn [request]
    (binding [current-session (atom (or (:session request) {}))]
      (let [response (handler request)]
        (assoc response :session @current-session)))))

(defn session-get
  ([k] (session-get k nil))
  ([k default] (if (vector? k)
                 (get-in @current-session k)
                 (get @current-session k default))))

(defn session-put!
  ([m]
     (swap! current-session (fn [a b] (merge a m)) m))
  ([k v]
     (swap! current-session (fn [a b] (merge a {k b})) v)))

(defn session-pop! [k]
  (let [res (get @current-session k)]
    (swap! current-session (fn [a b] (dissoc a b)) k)
    res))

(defn session-delete-key! [k]
  (swap! current-session (fn [a b] (dissoc a b)) k))

(defn session-destroy! []
  (swap! current-session (constantly nil)))


;; Place in a file called core.clj in the example project
(ns example.core
  (:use compojure.core
        [ring.middleware.keyword-params :only [wrap-keyword-params]]
        [ring.middleware.session :only [wrap-session]]
        [ring.middleware.session.cookie :only [cookie-store]]
        [example session])
  (:require [appengine-magic.core :as ae]))

(declare current-session)

(defroutes example-app-routes
  (GET "/" _
       (fn [req]
         (let [counter (session-get :counter 0)]
           {:status 200
            :headers {"Content-Type" "text/plain"}
            :body (str "started: " counter)})))
  (GET "/inc" _
       (fn [req]
         (let [counter (do 
                        (session-put! :counter (inc (session-get :counter 0)))
                        (session-get :counter))]
           {:status 200
            :headers {"Content-Type" "text/plain"}
            :body (str "incremented: " counter)}))))

(def example-app-handler
  (-> #'example-app-routes
      wrap-keyword-params
      wrap-session!
      (wrap-session {:cookie-name "example-app-session"
                     :store (cookie-store)})))

(ae/def-appengine-app example-app #'example-app-handler)

それの使い方

http://127.0.0.1:8080/incに移動すると、セッションのカウンターが増加し、 http: //127.0.0.1 : 8080 / はセッションのカウンターの値を表示します。

ラップセッション!セッションが機能するために必要ではなく、使用するだけです

(wrap-session {:cookie-name "example-app-session"
                     :store (cookie-store)})

作業機能セッションを提供します。ただし、副作用とラップセッションでセッションを管理したかったのです。その機能を提供します。フラッシュのような機能を使用するには、session-put を使用するだけです! セッションに値を入れてから、session-pop! を使用します。削除します。

お役に立てば幸いです。

于 2011-06-03T13:17:28.260 に答える
1

GAE が提供するセッションを使用する場合は、次を使用できます。

https://gist.github.com/1095841

リクエストにリングのようなセッションを含めますが、GAE セッションのサポートに基づいています。

その上でステートフル セッションが必要な場合は、Sandbar が提供するステートフル セッション API を使用できます。

https://github.com/brentonashworth/sandbar

于 2011-07-23T13:09:14.023 に答える