2019-09-09 09:31:08 +02:00
|
|
|
defmodule Mobilizon.Service.Statistics do
|
|
|
|
@moduledoc """
|
|
|
|
A module that provides cached statistics
|
|
|
|
"""
|
2020-01-23 00:55:07 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.{Conversations, Events, Users}
|
2019-09-09 09:31:08 +02:00
|
|
|
|
|
|
|
def get_cached_value(key) do
|
|
|
|
case Cachex.fetch(:statistics, key, fn key ->
|
|
|
|
case create_cache(key) do
|
|
|
|
value when not is_nil(value) -> {:commit, value}
|
|
|
|
err -> {:ignore, err}
|
|
|
|
end
|
|
|
|
end) do
|
|
|
|
{status, value} when status in [:ok, :commit] -> value
|
|
|
|
_err -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:local_users) do
|
|
|
|
Users.count_users()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:local_events) do
|
|
|
|
Events.count_local_events()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp create_cache(:local_comments) do
|
2020-02-18 08:57:00 +01:00
|
|
|
Conversations.count_local_comments()
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|
|
|
|
end
|