Skip to content

Commit fa5add6

Browse files
committed
Add benchmarks and improve docs
1 parent 85bd0ce commit fa5add6

7 files changed

Lines changed: 171 additions & 13 deletions

File tree

.formatter.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Used by "mix format"
22
[
3-
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test,benchmarks}/**/*.{ex,exs}"]
44
]

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ jobs:
6464
run: |
6565
mix local.hex --force
6666
mix local.rebar --force
67+
mix nbx.setup
6768
mix deps.get
68-
git clone --depth 1 --branch master https://github.com/cabol/nebulex.git
6969
7070
- name: Run style and code consistency checks
7171
run: |

README.md

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
[![Hex Version](https://img.shields.io/hexpm/v/nebulex_adapters_cachex.svg)](https://hex.pm/packages/nebulex_adapters_cachex)
1010
[![Docs](https://img.shields.io/badge/docs-hexpm-blue.svg)](https://hexdocs.pm/nebulex_adapters_cachex)
1111

12-
See the [docs](https://hexdocs.pm/nebulex_adapters_cachex/)
12+
This adapter allows to use Cachex (a widely used and powerful cache in Elixir)
13+
via Nebulex, which means, you can use Nebulex as usual taking advantage of all
14+
its benefits (like the cache abstraction layer, distributed caching topologies,
15+
declarative caching annotations, and so on), and at the same time using Cachex
16+
as cache backend.
17+
18+
See the [online documentation](https://hexdocs.pm/nebulex_adapters_cachex/)
1319
for more information.
1420

1521
## Installation
@@ -19,7 +25,7 @@ Add `:nebulex_adapters_cachex` to your list of dependencies in `mix.exs`:
1925
```elixir
2026
def deps do
2127
[
22-
{:nebulex_adapters_cachex, "~> 0.1.0"}
28+
{:nebulex_adapters_cachex, "~> 1.0"}
2329
]
2430
end
2531
```
@@ -103,7 +109,8 @@ defp cachex_opts do
103109
end
104110
```
105111

106-
> See [Cachex.start_link/1][cachex_start_link] for more information.
112+
> See [Cachex.start_link/1][cachex_start_link] for more information
113+
about options.
107114

108115
[cachex_start_link]: https://hexdocs.pm/cachex/Cachex.html#start_link/1
109116

@@ -158,3 +165,76 @@ Redis; for all examples you can just replace `Nebulex.Adapters.Local` by
158165
`Nebulex.Adapters.Cachex`.
159166

160167
[nbx_redis_adapter]: https://github.com/cabol/nebulex_redis_adapter
168+
169+
## Testing
170+
171+
Since `Nebulex.Adapters.Cachex` uses the support modules and shared tests
172+
from `Nebulex` and by default its test folder is not included in the Hex
173+
dependency, the following steps are required for running the tests.
174+
175+
First of all, make sure you set the environment variable `NEBULEX_PATH`
176+
to `nebulex`:
177+
178+
```
179+
export NEBULEX_PATH=nebulex
180+
```
181+
182+
Second, make sure you fetch `:nebulex` dependency directly from GtiHub
183+
by running:
184+
185+
```
186+
mix nbx.setup
187+
```
188+
189+
Third, fetch deps:
190+
191+
```
192+
mix deps.get
193+
```
194+
195+
Finally, you can run the tests:
196+
197+
```
198+
mix test
199+
```
200+
201+
Running tests with coverage:
202+
203+
```
204+
mix coveralls.html
205+
```
206+
207+
You will find the coverage report within `cover/excoveralls.html`.
208+
209+
## Benchmarks
210+
211+
Benchmarks were added using [benchee](https://github.com/PragTob/benchee), and
212+
they are located within the directory [benchmarks](./benchmarks).
213+
214+
To run the benchmarks:
215+
216+
```
217+
MIX_ENV=test mix run benchmarks/benchmark.exs
218+
```
219+
220+
## Contributing
221+
222+
Contributions to Nebulex are very welcome and appreciated!
223+
224+
Use the [issue tracker](https://github.com/cabol/nebulex_adapters_cachex/issues)
225+
for bug reports or feature requests. Open a
226+
[pull request](https://github.com/cabol/nebulex_adapters_cachex/pulls)
227+
when you are ready to contribute.
228+
229+
When submitting a pull request you should not update the
230+
[CHANGELOG.md](CHANGELOG.md), and also make sure you test your changes
231+
thoroughly, include unit tests alongside new or changed code.
232+
233+
Before to submit a PR it is highly recommended to run `mix check` and ensure
234+
all checks run successfully.
235+
236+
## Copyright and License
237+
238+
Copyright (c) 2020, Carlos Bolaños.
239+
240+
Nebulex.Adapters.Cachex source code is licensed under the [MIT License](LICENSE).

benchmarks/benchmark.exs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## Benchmarks
2+
3+
defmodule Cache do
4+
@moduledoc false
5+
use Nebulex.Cache,
6+
otp_app: :nebulex,
7+
adapter: Nebulex.Adapters.Cachex
8+
end
9+
10+
benchmarks = %{
11+
"get" => fn input ->
12+
Cache.get(input)
13+
end,
14+
"get_all" => fn input ->
15+
Cache.get_all([input, "foo", "bar"])
16+
end,
17+
"put" => fn input ->
18+
Cache.put(input, input)
19+
end,
20+
"put_new" => fn input ->
21+
Cache.put_new(input, input)
22+
end,
23+
"replace" => fn input ->
24+
Cache.replace(input, input)
25+
end,
26+
"put_all" => fn input ->
27+
Cache.put_all([{input, input}, {"foo", "bar"}])
28+
end,
29+
"delete" => fn input ->
30+
Cache.delete(input)
31+
end,
32+
"take" => fn input ->
33+
Cache.take(input)
34+
end,
35+
"has_key?" => fn input ->
36+
Cache.has_key?(input)
37+
end,
38+
"size" => fn _input ->
39+
Cache.size()
40+
end,
41+
"ttl" => fn input ->
42+
Cache.ttl(input)
43+
end,
44+
"expire" => fn input ->
45+
Cache.expire(input, 1)
46+
end,
47+
"incr" => fn _input ->
48+
Cache.incr(:counter, 1)
49+
end,
50+
"update" => fn input ->
51+
Cache.update(input, 1, &Kernel.+(&1, 1))
52+
end,
53+
"all" => fn _input ->
54+
Cache.all()
55+
end
56+
}
57+
58+
# start local cache
59+
{:ok, local} = Cache.start_link()
60+
61+
Benchee.run(
62+
benchmarks,
63+
inputs: %{"rand" => 100_000},
64+
before_each: fn n -> :rand.uniform(n) end,
65+
formatters: [
66+
{Benchee.Formatters.Console, comparison: false, extended_statistics: true},
67+
{Benchee.Formatters.HTML, extended_statistics: true, auto_open: false}
68+
],
69+
print: [
70+
fast_warning: false
71+
]
72+
)
73+
74+
# stop cache
75+
if Process.alive?(local), do: Supervisor.stop(local)

lib/nebulex/adapters/cachex.ex

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
defmodule Nebulex.Adapters.Cachex do
22
@moduledoc """
3-
Nebulex adapter for Cachex.
3+
Nebulex adapter for [Cachex][Cachex].
44
5-
Cachex is one of the most used cache (maybe the most used one) in Elixir.
6-
For that reason, Nebulex provides this adapter to use Cachex via Nebulex
7-
taking advantage of all their benefits and/or features.
5+
[Cachex]: http://hexdocs.pm/cachex/Cachex.html
86
9-
Cachex is now a very good alternative for local caching aside from the
10-
built-in local adapter provided by Nebulex. Besides, you can setup distributed
11-
topologies by using the distributed adapters Nebulex brings with it and set up
12-
Cachex as local cache.
7+
This adapter allows to use Cachex (a widely used and powerful cache in Elixir)
8+
via Nebulex, which means, you can use Nebulex as usual taking advantage of all
9+
its benefits (e.g.: cache abstraction layer, distributed caching topologies,
10+
declarative caching annotations, and so on), and at the same time using Cachex
11+
as cache backend.
1312
1413
## Options
1514

mix.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ defmodule NebulexAdaptersCachex.MixProject do
7777

7878
defp aliases do
7979
[
80+
"nbx.setup": [
81+
"cmd git clone --depth 1 --branch master https://github.com/cabol/nebulex.git"
82+
],
8083
check: [
8184
"compile --warnings-as-errors",
8285
"format --check-formatted",

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"makeup_elixir": {:hex, :makeup_elixir, "0.15.0", "98312c9f0d3730fde4049985a1105da5155bfe5c11e47bdc7406d88e01e4219b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "75ffa34ab1056b7e24844c90bfc62aaf6f3a37a15faa76b07bc5eba27e4a8b4a"},
2323
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
2424
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
25+
"nebulex": {:hex, :nebulex, "2.0.0-rc.1", "ad9fdc3b2b91b7de9d55dabfaac2994b91be29602b2715daf530bdd54509b4db", [:mix], [{:decorator, "~> 1.3", [hex: :decorator, repo: "hexpm", optional: true]}, {:shards, "~> 1.0", [hex: :shards, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "1d78bd1f9e648e5600cb2a349114957296f824641b57e72545ace6cb6cc22110"},
2526
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
2627
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
2728
"sleeplocks": {:hex, :sleeplocks, "1.1.1", "3d462a0639a6ef36cc75d6038b7393ae537ab394641beb59830a1b8271faeed3", [:rebar3], [], "hexpm", "84ee37aeff4d0d92b290fff986d6a95ac5eedf9b383fadfd1d88e9b84a1c02e1"},

0 commit comments

Comments
 (0)