Skip to content

Commit eae5e49

Browse files
committed
Swap out norm for nimble_options
1 parent 5fa4fd0 commit eae5e49

File tree

4 files changed

+55
-33
lines changed

4 files changed

+55
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ erl_crash.dump
2222
# Ignore package tarball (built via "mix hex.build").
2323
mentat-*.tar
2424

25+
.expert

lib/mentat.ex

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,63 @@ defmodule Mentat do
88
use Supervisor
99
use Oath
1010

11-
@type cache_opts() :: Keyword.t()
11+
@cache_opts NimbleOptions.new!([
12+
name: [
13+
type: :atom,
14+
doc: "The cache name as an atom",
15+
required: true
16+
],
17+
cleanup_interval: [
18+
type: :pos_integer,
19+
doc: "How often the janitor process will remove old keys.",
20+
default: 5_000
21+
],
22+
ets_args: [
23+
type: :any,
24+
doc: "Additional arguments to pass to `:ets.new/2`",
25+
default: []
26+
],
27+
ttl: [
28+
type: :timeout,
29+
doc: "The default ttl for all keys. Defaults to `:infinity`.",
30+
default: :infinity
31+
],
32+
limit: [
33+
type: :keyword_list,
34+
doc: "Limits to the number of keys a cache will store.",
35+
keys: [
36+
size: [
37+
type: :pos_integer,
38+
required: true,
39+
doc: "The maximum number of values to store in the cache"
40+
],
41+
reclaim: [
42+
type: :float,
43+
default: 0.1,
44+
doc: "The percentage of keys to reclaim if the limit is exceeded."
45+
]
46+
]
47+
],
48+
clock: [
49+
type: :any,
50+
doc: false,
51+
type_doc: false,
52+
default: System
53+
]
54+
])
55+
56+
@default_limit %{reclaim: 0.1}
57+
58+
@type cache_opts() :: unquote(NimbleOptions.option_typespec(@cache_opts))
1259
@type name :: atom()
1360
@type key :: term()
1461
@type value :: term()
1562
@type put_opts :: [
1663
{:ttl, pos_integer() | :infinity},
1764
]
1865

19-
@default_limit %{reclaim: 0.1}
20-
2166
alias Mentat.Janitor
2267

23-
defp cache_opts do
24-
import Norm
25-
26-
coll_of(
27-
one_of([
28-
{:name, spec(is_atom)},
29-
{:cleanup_interval, spec(is_integer and & &1 > 0)},
30-
{:ets_args, spec(is_list)},
31-
{:ttl, one_of([spec(is_integer and & &1 > 0), :infinity])},
32-
{:clock, spec(is_atom)},
33-
{:limit, coll_of(one_of([
34-
{:size, spec(is_integer and & &1 > 0)},
35-
{:reclaim, spec(is_float)},
36-
]))}
37-
])
38-
)
39-
end
40-
4168
@doc false
4269
def child_spec(opts) do
4370
name = opts[:name] || raise ArgumentError, ":name is required"
@@ -53,17 +80,11 @@ defmodule Mentat do
5380
Starts a new cache.
5481
5582
Options:
56-
* `:name` - the cache name as an atom. required.
57-
* `:cleanup_interval` - How often the janitor process will remove old keys. Defaults to 5_000.
58-
* `:ets_args` - Additional arguments to pass to `:ets.new/2`.
59-
* `:ttl` - The default ttl for all keys. Default `:infinity`.
60-
* `:limit` - Limits to the number of keys a cache will store. Defaults to `:none`.
61-
* `:size` - The maximum number of values to store in the cache.
62-
* `:reclaim` - The percentage of keys to reclaim if the limit is exceeded. Defaults to 0.1.
83+
#{NimbleOptions.docs(@cache_opts)}
6384
"""
6485
@spec start_link(cache_opts()) :: Supervisor.on_start()
6586
def start_link(args) do
66-
args = Norm.conform!(args, cache_opts())
87+
args = NimbleOptions.validate!(args, @cache_opts)
6788
name = args[:name]
6889
Supervisor.start_link(__MODULE__, args, name: name)
6990
end
@@ -245,12 +266,12 @@ defmodule Mentat do
245266

246267
def init(args) do
247268
name = args[:name]
248-
interval = args[:cleanup_interval] || 5_000
269+
interval = args[:cleanup_interval]
249270
limit = args[:limit] || :none
250271
limit = if limit != :none, do: Map.merge(@default_limit, Map.new(limit)), else: limit
251272
ets_args = args[:ets_args] || []
252273
clock = args[:clock] || System
253-
ttl = args[:ttl] || :infinity
274+
ttl = args[:ttl]
254275
^name = :ets.new(name, [:set, :named_table, :public] ++ ets_args)
255276

256277
put_config(name, %{limit: limit, ttl: ttl, clock: clock})

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Mentat.MixProject do
3333
defp deps do
3434
[
3535
{:telemetry, "~> 0.4 or ~> 1.0"},
36-
{:norm, "~> 0.12"},
36+
{:nimble_options, "~> 1.1"},
3737
{:oath, "~> 0.1"},
3838

3939
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
1313
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
1414
"makeup_erlang": {:hex, :makeup_erlang, "0.1.4", "29563475afa9b8a2add1b7a9c8fb68d06ca7737648f28398e04461f008b69521", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f4ed47ecda66de70dd817698a703f8816daa91272e7e45812469498614ae8b29"},
15+
"nimble_options": {:hex, :nimble_options, "1.1.1", "e3a492d54d85fc3fd7c5baf411d9d2852922f66e69476317787a7b2bb000a61b", [:mix], [], "hexpm", "821b2470ca9442c4b6984882fe9bb0389371b8ddec4d45a9504f00a66f650b44"},
1516
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
16-
"norm": {:hex, :norm, "0.13.0", "2c562113f3205e3f195ee288d3bd1ab903743e7e9f3282562c56c61c4d95dec4", [:mix], [{:stream_data, "~> 0.5", [hex: :stream_data, repo: "hexpm", optional: true]}], "hexpm", "447cc96dd2d0e19dcb37c84b5fc0d6842aad69386e846af048046f95561d46d7"},
1717
"oath": {:hex, :oath, "0.1.1", "9e96fc0bc94c8e2fe2deb84641f8b4533b7753575ac1829e1db4180f2b5656c4", [:mix], [{:decorator, "~> 1.3", [hex: :decorator, repo: "hexpm", optional: false]}], "hexpm", "d7e32cf0555905d422e5a90e8a7ce49c123710caa0fa2f7840bddbc1a0e93103"},
1818
"propcheck": {:hex, :propcheck, "1.4.1", "c12908dbe6f572032928548089b34ff9d40672d5d70f1562e3a9e9058d226cc9", [:mix], [{:libgraph, "~> 0.13", [hex: :libgraph, repo: "hexpm", optional: false]}, {:proper, "~> 1.4", [hex: :proper, repo: "hexpm", optional: false]}], "hexpm", "e1b088f574785c3c7e864da16f39082d5599b3aaf89086d3f9be6adb54464b19"},
1919
"proper": {:hex, :proper, "1.4.0", "89a44b8c39d28bb9b4be8e4d715d534905b325470f2e0ec5e004d12484a79434", [:rebar3], [], "hexpm", "18285842185bd33efbda97d134a5cb5a0884384db36119fee0e3cfa488568cbb"},

0 commit comments

Comments
 (0)