summaryrefslogtreecommitdiff
path: root/lib/cache.fnl
blob: 8cf732389297352b01c08988e88503ae649757f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(local {: must} (require :lib.utils))

(fn get [db key else]
  (local result
    (must (luna.db.query db "SELECT value FROM cache WHERE key = ?"
                         [key])))
  (or (if (and result (< 0 (# result)))
        (. (. result 1) 1)
        nil)
      else))

(fn _set [db key value]
  value
  (must (luna.db.exec db "INSERT OR REPLACE INTO cache VALUES (?, ?)"
                      [key value]))
  value)

(fn clear-tx [tx prefix]
  (if (= "string" (type prefix))
    (must (luna.db.exec-tx tx "DELETE FROM cache WHERE key LIKE ?"
                           [(.. prefix "%")]))
    (must (luna.db.exec-tx tx "DELETE FROM cache" []))))

{: get :set _set : clear-tx}