Skip to content

Commit

Permalink
fetch_cached_results in compute p.274 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 11, 2019
1 parent f2ba5c0 commit 6ef3c59
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion apps/info_sys/lib/info_sys.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ defmodule InfoSys do
defstruct score: 0, text: nil, backend: nil
end

alias InfoSys.Cache

def compute(query, opts \\ []) do
timeout = opts[:timeout] || 10_000
opts = Keyword.put_new(opts, :limit, 10)
backends = opts[:backends] || @backends

backends
{uncached_backends, cached_results} =
fetch_cached_results(backends, query, opts)

uncached_backends
|> Enum.map(&async_query(&1, query, opts))
|> Task.yield_many(timeout)
|> Enum.map(fn {task, res} -> res || Task.shutdown(task, :brutal_kill) end)
|> Enum.flat_map(fn
{:ok, results} -> results
_ -> []
end)
|> write_results_to_cache(query, opts)
|> Kernel.++(cached_results)
|> Enum.sort(&(&1.score >= &2.score))
|> Enum.take(opts[:limit])
end
Expand Down

0 comments on commit 6ef3c59

Please sign in to comment.