aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/counter_cache.ex
blob: 4d348a4134915a783d08547ed8fffae233721d5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.CounterCache do
  alias Pleroma.CounterCache
  alias Pleroma.Repo
  use Ecto.Schema
  import Ecto.Changeset
  import Ecto.Query

  schema "counter_cache" do
    field(:name, :string)
    field(:count, :integer)
  end

  def changeset(struct, params) do
    struct
    |> cast(params, [:name, :count])
    |> validate_required([:name])
    |> unique_constraint(:name)
  end

  def get_as_map(names) when is_list(names) do
    CounterCache
    |> where([cc], cc.name in ^names)
    |> Repo.all()
    |> Enum.group_by(& &1.name, & &1.count)
    |> Map.new(fn {k, v} -> {k, hd(v)} end)
  end

  def set(name, count) do
    %CounterCache{}
    |> changeset(%{"name" => name, "count" => count})
    |> Repo.insert(
      on_conflict: [set: [count: count]],
      returning: true,
      conflict_target: :name
    )
  end
end