Skip to content

Commit

Permalink
feat: add provider behaviour, tests, and remove application (#1)
Browse files Browse the repository at this point in the history
* feat: add provider behaviour

* feat: add custom registry

* fix(Default): start the correct supervisor

* fix(Guild.Registry): add from parameter to handle functions

* test: add tests

* test: add more tests to guild

* deps: temporarily use git dep

* chore(.gitignore): add eol eof

* fix: add missing @ spec and @ impl

* fix(Cache): start_link only accepts one parameter
  • Loading branch information
SpaceEEC committed Apr 1, 2019
1 parent 5502332 commit 3298fee
Show file tree
Hide file tree
Showing 19 changed files with 949 additions and 155 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -22,3 +22,5 @@ erl_crash.dump
# Ignore package tarball (built via "mix hex.build").
crux_cache-*.tar

# VSCode extension
.elixir_ls/
44 changes: 1 addition & 43 deletions lib/cache.ex
Expand Up @@ -56,8 +56,6 @@ defmodule Crux.Cache do

@doc """
Deletes data from the cache by key.
Always returns `:ok`, even when the key did not exist.
"""
@callback delete(id :: key()) :: :ok

Expand All @@ -69,47 +67,7 @@ defmodule Crux.Cache do
@doc """
Fetches data from the cache by key, raises if not found.
"""
@callback fetch!(id :: identifier()) :: term() | :error
@callback fetch!(id :: identifier()) :: term() | no_return()

@optional_callbacks start_link: 1

@doc """
Fetches the module handling the guild caching.
Defaults to `Crux.Cache.Guild`.
"""
@spec guild_cache() :: module()
def guild_cache(), do: Application.get_env(:crux_cache, :guild, Crux.Cache.Guild)

@doc """
Fetches the module handling the guild cache.
Defaults to `Crux.Cache.Channel`.
"""
@spec channel_cache() :: module()
def channel_cache(), do: Application.get_env(:crux_cache, :channel, Crux.Cache.Channel)

@doc """
Fetches the module handling the emoji cache.
Defaults to `Crux.Cache.Emoji`.
"""
@spec emoji_cache() :: module()
def emoji_cache(), do: Application.get_env(:crux_cache, :emoji, Crux.Cache.Emoji)

@doc """
Fetches the module handling the presence cache.
Defaults to `Crux.Cache.Presence`.
"""
@spec presence_cache() :: module()
def presence_cache(), do: Application.get_env(:crux_cache, :presence, Crux.Cache.Presence)

@doc """
Fetches the module handling the user cache.
Defaults to `Crux.Cache.User`.
"""
@spec user_cache() :: module()
def user_cache(), do: Application.get_env(:crux_cache, :user, Crux.Cache.User)
end
33 changes: 0 additions & 33 deletions lib/cache/application.ex

This file was deleted.

16 changes: 12 additions & 4 deletions lib/cache/base.ex
Expand Up @@ -43,20 +43,23 @@ defmodule Crux.Cache.Base do
end

@doc false
@impl true
def init(args) do
:ets.new(@name, [:named_table, read_concurrency: true])

{:ok, args}
end

@doc false
@impl true
def handle_cast({:cache, structure}, state) do
{_, _, state} = handle_call({:update, structure}, nil, state)

{:noreply, state}
end

@doc false
@impl true
def handle_call({:update, structure}, _from, state) do
structure =
case Crux.Cache.Base.fetch(@name, structure.id) do
Expand All @@ -76,7 +79,7 @@ defmodule Crux.Cache.Base do
def handle_call({:delete, id}, _from, state) do
:ets.delete(@name, id)

{:reply, :deleted, state}
{:reply, :ok, state}
end

defoverridable start_link: 1,
Expand All @@ -94,15 +97,20 @@ defmodule Crux.Cache.Base do

@doc false
def cache(name, structure) do
structure = Util.atomify(structure)
structure =
Util.atomify(structure)
|> Map.update!(:id, &Util.id_to_int/1)

GenServer.cast(name, {:cache, structure})

structure
end

@doc false
def update(name, structure) do
structure = Util.atomify(structure)
structure =
Util.atomify(structure)
|> Map.update!(:id, &Util.id_to_int/1)

GenServer.call(name, {:update, structure})
end
Expand Down Expand Up @@ -136,7 +144,7 @@ defmodule Crux.Cache.Base do
GenServer.call(name, {:delete, id})

:error ->
:already
:ok
end
end

Expand Down
60 changes: 60 additions & 0 deletions lib/cache/default.ex
@@ -0,0 +1,60 @@
defmodule Crux.Cache.Default do
@moduledoc """
A provider using the default caches:
* `Crux.Cache.Guild`
* `Crux.Cache.Channel`
* `Crux.Cache.Emoji`
* `Crux.Cache.Presence`
* `Crux.Cache.User`
"""
alias Crux.Cache

use Cache.Provider

def init(_) do
children = [
Cache.Guild.Supervisor.Supervisor,
Cache.Channel,
Cache.Emoji,
Cache.Presence,
Cache.User
]

Supervisor.init(children, strategy: :one_for_one)
end

@doc """
The default guild cache.
"""
@impl true
@spec guild_cache() :: module()
def guild_cache(), do: Cache.Guild

@doc """
The default guild cache: `Crux.Cache.Channel`.
"""
@impl true
@spec channel_cache() :: module()
def channel_cache(), do: Cache.Channel

@doc """
The default guild cache: `Crux.Cache.Emoji`.
"""
@impl true
@spec emoji_cache() :: module()
def emoji_cache(), do: Cache.Emoji

@doc """
The default guild cache: `Crux.Cache.Presence`.
"""
@impl true
@spec presence_cache() :: module()
def presence_cache(), do: Cache.Presence

@doc """
The default guild cache: `Crux.Cache.User`.
"""
@impl true
@spec user_cache() :: module()
def user_cache(), do: Cache.User
end

0 comments on commit 3298fee

Please sign in to comment.