diff options
| author | unwox <me@unwox.com> | 2024-10-17 10:32:35 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2024-10-17 10:53:30 +0600 |
| commit | ff8c6c9dde3a877b51e2fabafa579e2c05dc856e (patch) | |
| tree | 3b6c9d8638ee78f913540e25ac7fc17433b906b6 /lib/cache.fnl | |
| parent | 43c7b5de358e8a158e9ed3c396d3866845682515 (diff) | |
implement caching
Diffstat (limited to 'lib/cache.fnl')
| -rw-r--r-- | lib/cache.fnl | 21 |
1 files changed, 21 insertions, 0 deletions
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} |
