Skip to content

Commit

Permalink
tune weight and cache
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee committed Oct 29, 2024
1 parent 6125b19 commit cf31223
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions core/lib/canary/interface/search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ defmodule Canary.Interface.Search do
def run(project, query, opts) do
{cache, opts} = Keyword.pop(opts, :cache, false)

if cache do
with {:error, _} <- get_cache(project, query, opts),
{:ok, result} <- impl().run(project, query, opts) do
set_cache(project, query, opts, result)
{:ok, result}
end
else
if not cache do
impl().run(project, query, opts)
else
case get_cache(project, query, opts) do
{:ok, result} ->
{:ok, result}

_ ->
{:ok, result} = impl().run(project, query, opts)
set_cache(project, query, opts, result)
{:ok, result}
end
end
end

defp set_cache(project, query, opts, result) do
Cachex.put(:cache, key(project, query, opts), result, ttl: :timer.minutes(1))
Cachex.put(:cache, key(project, query, opts), result, ttl: :timer.seconds(30))
end

defp get_cache(project, query, opts) do
Expand Down
4 changes: 2 additions & 2 deletions core/lib/canary/sources/document_create.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ defmodule Canary.Sources.Document.Create do
title:
if(index == 0, do: item.title, else: data.root.title <> "\n" <> data.root.content),
created_at: item.created_at,
weight: clamp(1, 5, item.num_reactions),
weight: 1 + clamp(0, 3, item.num_reactions / 2) / 10,
meta: %{index: index}
}
end)
Expand Down Expand Up @@ -153,7 +153,7 @@ defmodule Canary.Sources.Document.Create do
title:
if(index == 0, do: item.title, else: data.root.title <> "\n" <> data.root.content),
created_at: item.created_at,
weight: clamp(1, 5, item.num_reactions),
weight: 1 + clamp(0, 3, item.num_reactions / 2) / 10,
meta: %{index: index}
}
end)
Expand Down

0 comments on commit cf31223

Please sign in to comment.