From 359790c402d2731b7dd1df8a629726e69922a035 Mon Sep 17 00:00:00 2001 From: unwox Date: Thu, 17 Oct 2024 14:32:27 +0600 Subject: cache fixes for spelling mistakes --- bin/serve.fnl | 11 +++++++---- lib/cache.fnl | 9 ++++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bin/serve.fnl b/bin/serve.fnl index e574238..9d83b10 100644 --- a/bin/serve.fnl +++ b/bin/serve.fnl @@ -335,9 +335,12 @@ (category-menu-path (. form.tags 1)) nil)) (local spellfix-suggestion - (if (and (not (str.empty? form.query)) items (< 0 (# items))) - nil - (spellfix.guess form.query))) + (if (and (not (str.empty? form.query)) items (= 0 (# items))) + (cache.wrap + db (.. "spellfix:" form.query) + #(spellfix.guess form.query)) + nil)) + [:html {:lang "ru-RU"} [:head {} [:meta {:charset "utf-8"}] @@ -372,7 +375,7 @@ [:footer {} paginator]]]]]]) (fn root-handler [{: path : query}] - (local cache-key (.. path "?" (serialize-query query))) + (local cache-key (.. "page:" path "?" (serialize-query query))) (local cached (cache.get db cache-key)) (if cached (values 200 {:content-type "text/html"} cached) diff --git a/lib/cache.fnl b/lib/cache.fnl index 8cf7323..76ede13 100644 --- a/lib/cache.fnl +++ b/lib/cache.fnl @@ -21,4 +21,11 @@ [(.. prefix "%")])) (must (luna.db.exec-tx tx "DELETE FROM cache" [])))) -{: get :set _set : clear-tx} +(fn wrap [db key f] + (local cached (get db key)) + (if cached + cached + (let [res (f)] + (if res (_set db key res) nil)))) + +{: get :set _set : clear-tx : wrap} -- cgit v1.2.3