Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

get live requests via workaround and cache them in redis

  • Loading branch information...
commit 28faee2a5c045c4318968ad8a481a5d766ab2fb1 1 parent 100f18e
Mattias Dalkvist authored

Showing 1 changed file with 41 additions and 10 deletions. Show diff stats Hide diff stats

  1. +41 10 src/bf3/core.clj
51 src/bf3/core.clj
@@ -462,20 +462,51 @@
462 462
463 463 ])))))))
464 464
465   -(defpage "/live" []
466   - (response/redirect "/live/" ))
  465 +(defn- get-live [server]
  466 + (let [id (str "live" server)
  467 + data (redis/get-data id)
  468 + save #(redis/set-data
  469 + id
  470 + {:time (+ *tiny-cache-time* (clj-time.coerce/to-long (clj-time.core/now)))
  471 + :data (->> (client/get (str "http://work.dalkvist.se:8081/live/" server))
  472 + :body)})]
  473 +
  474 + (if (or (not data) (nil? (:data data)) (empty? (:data data)))
  475 + (do (save)
  476 + (:data (redis/get-data id)))
  477 + (do (when (and (number? (:time data))
  478 + (> (clj-time.coerce/to-long (clj-time.core/now)) (:time data)))
  479 + (println "live saving " server)
  480 + (future (save)))
  481 + (:data data)))))
  482 +
  483 +(def live (mem/memo-ttl get-live 2000))
  484 +
  485 +(defpage "/live" {:keys [server] :or {server "eu"}}
  486 + (html5 (live server) ))
  487 +
  488 +(def live-js (javascript-tag (str "var runUpdate = true;"
  489 + "var update = function(){$.get(window.location.protocol + \"//\" + window.location.host + window.location.pathname + window.location.href,"
  490 + "function(res){if($('.livecontainer',res).children().length > 1){$('.livecontainer').replaceWith($('.livecontainer', res).first());}});"
  491 + "if(runUpdate){ window.setTimeout(update, 10 * 1000);};};"
  492 + "update();")))
467 493
468 494 (defpage "/live/" {:keys [server]}
469   - (battle-layout (->> (if server server "87989727-762a-420f-8aab-48987c6a4dca")
470   - bf3.bl/get-live-info parse-info show-live-info)))
  495 + (battle-layout (list live-js
  496 + (->> (if server (condp = server
  497 + "eu" :eu
  498 + "na" :na-new-york
  499 + server)
  500 + "87989727-762a-420f-8aab-48987c6a4dca")
  501 + bf3.bl/get-live-info parse-info show-live-info))))
471 502
472 503 (defpage "/live/:server" {:keys [server]}
473   - (battle-layout (list (javascript-tag (str "var runUpdate = true;"
474   - "var update = function(){$.get(window.location.protocol + \"//\" + window.location.host + window.location.pathname,"
475   - "function(res){if($('.livecontainer',res).children().length > 1){$('.livecontainer').replaceWith($('.livecontainer', res).first());}});"
476   - "if(runUpdate){ window.setTimeout(update, 10 * 1000);};};"
477   - "update();"))
478   - (->> (if server server "87989727-762a-420f-8aab-48987c6a4dca")
  504 + (battle-layout (list live-js
  505 + (->> (if server (condp = server
  506 + "eu" :eu
  507 + "na" :na-new-york
  508 + server)
  509 + "87989727-762a-420f-8aab-48987c6a4dca")
479 510 bf3.bl/get-live-info parse-info show-live-info))))
480 511
481 512 (defpage "/live/:server/.json" {:keys [server]}

0 comments on commit 28faee2

Please sign in to comment.
Something went wrong with that request. Please try again.