-
Notifications
You must be signed in to change notification settings - Fork 10
/
cache.cljs
85 lines (70 loc) · 2.48 KB
/
cache.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(ns stylefy.impl.cache
(:require [dommy.core :as dommy]
[reagent.core :as r]
[garden.core :refer [css]]
[stylefy.impl.utils :as utils]
[garden.stylesheet :refer [at-media at-keyframes at-font-face]])
(:require-macros [reagent.ratom :refer [run!]]))
(def cache-prefix "stylefy_cache_")
(def cache-styles? (atom false))
(def default-cache-expiration-time-s (* 1 60 60 * 24 * 7))
(defn cache-key-styles [instance-id]
(str cache-prefix "styles"
(when instance-id
(str "_" instance-id))))
(defn- cache-key-created [instance-id]
(str cache-prefix "created"
(when instance-id
(str "_" instance-id))))
;; Utils
(defn now-in-seconds []
(.floor js/Math (/ (.now js/Date) 1000)))
(defn cache-expired? [cache-created expiration-age now]
(if (and cache-created expiration-age)
(< (+ cache-created expiration-age) now)
false))
;; Cache reading
(defn read-cache-value
"Reads the cache if caching is used."
[key]
(when @cache-styles?
(when-let [cache-contents (.getItem (.-localStorage js/window) key)]
(cljs.reader/read-string cache-contents))))
;; Cache manipulation
(defn set-cache-created-time
([time-created]
(set-cache-created-time time-created nil))
([time-created instance-id]
(.setItem (.-localStorage js/window)
(cache-key-created instance-id)
time-created)))
(defn clear-styles
([]
(clear-styles nil))
([instance-id]
(.setItem (.-localStorage js/window)
(cache-key-styles instance-id)
"")))
(defn use-caching!
([cache-options]
(use-caching! cache-options nil))
([cache-options instance-id]
(reset! cache-styles? true)
;; If cache is empty, set creation date.
(when-not (read-cache-value (cache-key-created instance-id))
(set-cache-created-time (now-in-seconds) instance-id))
(when (cache-expired? (read-cache-value (cache-key-created instance-id))
(or (:expires cache-options) default-cache-expiration-time-s)
(now-in-seconds))
(clear-styles instance-id)
(set-cache-created-time (now-in-seconds) instance-id))))
(defn cache-styles
"Caches the given style if caching is used.
Throws QUOTA_EXCEEDED_ERR if the storage is full."
([styles]
(cache-styles styles nil))
([styles instance-id]
(when (and @cache-styles? (map? styles))
(.setItem (.-localStorage js/window)
(cache-key-styles instance-id)
styles))))