logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

state.ex (812B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Migrators.HashtagsTableMigrator.State do
  5. use Agent
  6. @init_state %{}
  7. @reg_name {:global, __MODULE__}
  8. def start_link(_) do
  9. Agent.start_link(fn -> @init_state end, name: @reg_name)
  10. end
  11. def clear do
  12. Agent.update(@reg_name, fn _state -> @init_state end)
  13. end
  14. def get do
  15. Agent.get(@reg_name, & &1)
  16. end
  17. def put(key, value) do
  18. Agent.update(@reg_name, fn state ->
  19. Map.put(state, key, value)
  20. end)
  21. end
  22. def increment(key, increment \\ 1) do
  23. Agent.update(@reg_name, fn state ->
  24. updated_value = (state[key] || 0) + increment
  25. Map.put(state, key, updated_value)
  26. end)
  27. end
  28. end