1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.CounterCache do
6 alias Pleroma.CounterCache
12 schema "counter_cache" do
14 field(:count, :integer)
17 def changeset(struct, params) do
19 |> cast(params, [:name, :count])
20 |> validate_required([:name])
21 |> unique_constraint(:name)
24 def get_as_map(names) when is_list(names) do
26 |> where([cc], cc.name in ^names)
28 |> Enum.group_by(& &1.name, & &1.count)
29 |> Map.new(fn {k, v} -> {k, hd(v)} end)
32 def set(name, count) do
34 |> changeset(%{"name" => name, "count" => count})
36 on_conflict: [set: [count: count]],
38 conflict_target: :name