From ff8c6c9dde3a877b51e2fabafa579e2c05dc856e Mon Sep 17 00:00:00 2001 From: unwox Date: Thu, 17 Oct 2024 10:32:35 +0600 Subject: implement caching --- lib/array.fnl | 12 +++++++++++- lib/cache.fnl | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 lib/cache.fnl (limited to 'lib') diff --git a/lib/array.fnl b/lib/array.fnl index 8b81c5c..8af6244 100644 --- a/lib/array.fnl +++ b/lib/array.fnl @@ -53,6 +53,16 @@ (lua "return true"))) false) +(fn list [obj] + (local result []) + (var cursor 1) + (each [_ v (pairs obj)] + (tset result cursor v) + (set cursor (+ 1 cursor))) + ;; sort so we always return the same list for the same object + (table.sort result) + result) + (local join table.concat) -{: sort : concat : diff : unique : flatten : join : contains} +{: sort : concat : diff : unique : flatten : join : contains : list} diff --git a/lib/cache.fnl b/lib/cache.fnl new file mode 100644 index 0000000..b21a05e --- /dev/null +++ b/lib/cache.fnl @@ -0,0 +1,21 @@ +(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] + (must (luna.db.exec-tx tx "DELETE FROM cache" []))) + +{: get :set _set : clear-tx} -- cgit v1.2.3