-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
191 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule ChannelSpec.Cache do | ||
@moduledoc """ | ||
Cache for ChannelSpec specs. | ||
Settings: | ||
```elixir | ||
config :channel_spec, :cache_adapter, Module | ||
``` | ||
ChannelSpec ships with two cache adapters: | ||
* `ChannelSpec.Cache.PersistentTermCache` - default | ||
* `ChannelSpec.Cache.NoneCache` - none cache | ||
If you are constantly modifying specs during development, you can configure the cache adapter | ||
in `dev.exs` as follows to disable caching: | ||
```elixir | ||
config :channel_spec, :cache_adapter, ChannelSpec.Cache.NoneCache | ||
``` | ||
""" | ||
|
||
@callback get(module) :: nil | map() | ||
@callback put(module, map()) :: :ok | ||
@callback erase(module) :: :ok | ||
|
||
@default_adapter ChannelSpec.Cache.PersistentTermCache | ||
|
||
@doc """ | ||
Get cache adapter | ||
""" | ||
@spec adapter() :: module() | ||
def adapter() do | ||
Application.get_env(:channel_spec, :cache_adapter, @default_adapter) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule ChannelSpec.Cache.NoneCache do | ||
@moduledoc """ | ||
A cache adapter to disable caching. Intended to be used in development. | ||
Configure it with: | ||
```elixir | ||
# config/runtime.exs | ||
config :channel_handler, :cache_adapter, ChannelSpec.Cache.NoneCache | ||
``` | ||
""" | ||
|
||
@behaviour ChannelSpec.Cache | ||
|
||
@impl true | ||
def get(_spec_module), do: nil | ||
|
||
@impl true | ||
def put(_spec_module, _spec), do: :ok | ||
|
||
@impl true | ||
def erase(_spec_module), do: :ok | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule ChannelSpec.Cache.PersistentTermCache do | ||
@moduledoc """ | ||
A cache adapter that stores the specs in memory. | ||
This is the default cache adapter. | ||
""" | ||
|
||
@behaviour ChannelSpec.Cache | ||
|
||
@impl true | ||
def get(spec_module) do | ||
:persistent_term.get(spec_module, nil) | ||
end | ||
|
||
@impl true | ||
def put(spec_module, specs) do | ||
:persistent_term.put(spec_module, specs) | ||
end | ||
|
||
@impl true | ||
def erase(spec_module) do | ||
:persistent_term.erase(spec_module) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule ChannelSpec.Cache.NoneCacheTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias ChannelSpec.Cache.NoneCache | ||
|
||
describe "get/1" do | ||
test "returns nil" do | ||
assert is_nil(NoneCache.get(SomeModule)) | ||
end | ||
end | ||
|
||
describe "put/2" do | ||
test "returns :ok" do | ||
assert :ok = NoneCache.put(SomeModule, %{}) | ||
end | ||
end | ||
|
||
describe "erase/1" do | ||
test "returns :ok" do | ||
assert :ok = NoneCache.erase(SomeModule) | ||
end | ||
end | ||
end |
Empty file.