Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cache battles and battle html in redis

  • Loading branch information...
commit 7f26af80aa74e4119a3baa40cada193645ddf6bf 1 parent e0a82a2
@dalkvist authored
Showing with 47 additions and 8 deletions.
  1. +2 −0  project.clj
  2. +28 −8 src/bf3/core.clj
  3. +17 −0 src/bf3/redis.clj
View
2  project.clj
@@ -1,6 +1,7 @@
(defproject bf3 "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.3.0"]
+ [org.clojure/data.json "0.2.1"]
[cheshire "2.0.6"]
[clj-http "0.2.7"]
[noir "1.2.2"]
@@ -9,6 +10,7 @@
[org.clojure/core.cache "0.5.0"]
[paddleguru/enlive "1.2.0-alpha1"]
[clj-time "0.3.7"]
+ [com.taoensso/carmine "1.7.0-beta2"]
[org.clojars.crimeminister/gaka "0.2.2"]
[clj-stacktrace "0.2.4"]]
:main bf3.core
View
36 src/bf3/core.clj
@@ -16,6 +16,7 @@
[clojure.string :as s]
[gaka [core :as gaka]]
[bf3.bl :as bl]
+ [bf3.redis :as redis]
[clj-http.client :as client]
[clojure.core.memoize :as mem]
[clj-time.core :as time]
@@ -354,11 +355,23 @@
(defpage "/gc/" [] (layout "GC stuff"))
(defn- get-battle-page [gameid & {:keys [start end] :or {start false end false}}]
- (->> (client/get (str "http://work.dalkvist.se:8081/get-battle/" gameid
- "?" (encode-params
- (merge {:host ((:headers (req/ring-request)) "host")}
- (if start {:start start} {})
- (if end {:end end} {}))))) :body))
+ (let [id (str "gc-battle" gameid start end)
+ data (redis/get-data id)
+ save (fn [] (redis/set-data
+ id
+ {:time (+ *short-cache-time* (clj-time.coerce/to-long (clj-time.core/now)))
+ :data (->> (client/get (str "http://work.dalkvist.se:8081/get-battle/" gameid
+ "?" (encode-params
+ (merge {:host ((:headers (req/ring-request)) "host")}
+ (if start {:start start} {})
+ (if end {:end end} {}))))) :body)}))]
+ (if (or (not data) (nil? (:data data)) (empty? (:data data)))
+ (do (save)
+ (:data (redis/get-data id)))
+ (do (when (and (number? (:time data))
+ (> (clj-time.coerce/to-long (clj-time.core/now)) (:time data)))
+ (future (save)))
+ (:data data)))))
(def battle (mem/memo-ttl get-battle-page *short-cache-time*))
@@ -376,10 +389,17 @@
[:a {:href (str "http://" host "/live/" (:server battle))}
"go live"]))
(show-live-info (dissoc battle :live)))))))
-
(defn- get-battles []
- (->> (client/get (str "http://work.dalkvist.se:8081/gc/battles/"
- "?" (encode-params {:host ((:headers (req/ring-request)) "host")}))) :body))
+ (let [data (redis/get-data "gc-battles")]
+ (when (or (not data) (and (number? (:time data))
+ (> (clj-time.coerce/to-long (clj-time.core/now)) (:time data))))
+ (future (redis/set-data
+ "gc-battles"
+ {:time (+ *short-cache-time* (clj-time.coerce/to-long (clj-time.core/now)))
+ :data (->> (client/get (str "http://work.dalkvist.se:8081/gc/battles/"
+ "?" (encode-params {:host (get (:headers (req/ring-request)) "host" "")})))
+ :body)})))
+ (:data data)))
(def battles (mem/memo-ttl get-battles *short-cache-time*))
View
17 src/bf3/redis.clj
@@ -0,0 +1,17 @@
+(ns bf3.redis
+ (:require [taoensso.carmine :as car]))
+
+(defn- get-redis-uri []
+ (get (System/getenv) "REDISCLOUD_URL" "redis://"))
+
+(def pool (car/make-conn-pool))
+(def spec1 (car/make-conn-spec :uri (get-redis-uri)))
+
+(defmacro wcar [& body] `(car/with-conn pool spec1 ~@body))
+
+(defn set-data [key data]
+ (do (wcar (car/ping)
+ (car/set key data))))
+
+(defn get-data [key]
+ (wcar (car/get key)))
Please sign in to comment.
Something went wrong with that request. Please try again.