Skip to content

Commit

Permalink
cache quotes daily
Browse files Browse the repository at this point in the history
  • Loading branch information
justone committed Apr 12, 2019
1 parent 63e0884 commit c43fa05
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/yetibot/commands/cljquotes.clj
@@ -1,5 +1,6 @@
(ns yetibot.commands.cljquotes
(:require
[clojure.core.memoize :as memo]
[clojure.edn :as edn]
[clojure.string :as string]

Expand All @@ -13,10 +14,13 @@
(let [url "https://raw.githubusercontent.com/Azel4231/clojure-quotes/master/quotes.edn"]
(-> (client/get url)
:body
edn/read-string
)))
edn/read-string)))

(def quotes (fetch-quotes))
;; only fetch once per day
(def quote-fetch-delay (* 24 60 60 1000))

(def quotes
(memo/ttl fetch-quotes :ttl/threshold quote-fetch-delay))

(defn format-quote
[quote]
Expand All @@ -29,12 +33,12 @@
(defn quote-cmd
"cljquote # random clojure quote"
[context]
(let [quote (rand-nth quotes)]
(let [quote (rand-nth (quotes))]
{:result/value (format-quote quote)
:result/data quote}))

(cmd-hook #"cljquote"
_ quote-cmd)

#_(format-quote (rand-nth quotes))
#_(string/join "\n\n" (map format-quote quotes))
#_(format-quote (rand-nth (quotes)))
#_(string/join "\n\n" (map format-quote (quotes)))

0 comments on commit c43fa05

Please sign in to comment.