2020-01-09 19:18:55 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2020-01-09 19:18:55 +00:00
|
|
|
# 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
|