summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-10-17 14:32:27 +0600
committerunwox <me@unwox.com>2024-10-17 14:32:27 +0600
commit359790c402d2731b7dd1df8a629726e69922a035 (patch)
tree6b6fc8110cefc76fccea51069a17e54bf22bd30c
parent08370b6f302f93cae01b0521f29a62c20e97bdab (diff)
cache fixes for spelling mistakes
-rw-r--r--bin/serve.fnl11
-rw-r--r--lib/cache.fnl9
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}