Skip to content

Commit 43a62a1

Browse files
committed
Update Nebulex
1 parent fa5add6 commit 43a62a1

8 files changed

Lines changed: 237 additions & 50 deletions

File tree

.credo.exs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,9 @@
88
},
99
color: true,
1010
checks: [
11-
# Design Checks
12-
{Credo.Check.Design.AliasUsage, priority: :low},
13-
14-
# Deactivate due to they're not compatible with current Elixir version
15-
{Credo.Check.Refactor.MapInto, false},
16-
{Credo.Check.Warning.LazyLogging, false},
17-
1811
# Readability Checks
1912
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 100},
2013

21-
# Refactoring Opportunities
22-
{Credo.Check.Refactor.LongQuoteBlocks, false},
23-
{Credo.Check.Refactor.CyclomaticComplexity, max_complexity: 15},
24-
2514
# TODO and FIXME do not cause the build to fail
2615
{Credo.Check.Design.TagTODO, exit_status: 0},
2716
{Credo.Check.Design.TagFIXME, exit_status: 0}

README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
# Nebulex.Adapters.Cachex
2-
> ### Nebulex adapter for [Cachex][Cachex]
3-
> Cachex via Nebulex out-of-box.
2+
> A [Nebulex][Nebulex] adapter for [Cachex][Cachex].
43
4+
[Nebulex]: https://github.com/cabol/nebulex
55
[Cachex]: https://github.com/whitfin/cachex
66

77
![CI](https://github.com/cabol/nebulex_adapters_cachex/workflows/CI/badge.svg)
8-
[![Coverage Status](https://coveralls.io/repos/github/cabol/nebulex_adapters_cachex/badge.svg?branch=main)](https://coveralls.io/github/cabol/nebulex_adapters_cachex?branch=main)
8+
[![Coverage Status](https://img.shields.io/coveralls/cabol/nebulex_adapters_cachex.svg)](https://coveralls.io/github/cabol/nebulex_adapters_cachex)
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-
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-
1812
See the [online documentation](https://hexdocs.pm/nebulex_adapters_cachex/)
1913
for more information.
2014

@@ -161,8 +155,8 @@ config :my_app, MyApp.NearCache,
161155

162156
See [Nebulex examples](https://github.com/cabol/nebulex_examples). You will
163157
find examples for all different topologies, even using other adapters like
164-
Redis; for all examples you can just replace `Nebulex.Adapters.Local` by
165-
`Nebulex.Adapters.Cachex`.
158+
Redis; for all examples you just have to replace `Nebulex.Adapters.Local`
159+
by `Nebulex.Adapters.Cachex`.
166160

167161
[nbx_redis_adapter]: https://github.com/cabol/nebulex_redis_adapter
168162

lib/nebulex/adapters/cachex.ex

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,22 @@ defmodule Nebulex.Adapters.Cachex do
141141

142142
# Provide Cache Implementation
143143
@behaviour Nebulex.Adapter
144+
@behaviour Nebulex.Adapter.Entry
144145
@behaviour Nebulex.Adapter.Queryable
145146
@behaviour Nebulex.Adapter.Persistence
147+
@behaviour Nebulex.Adapter.Stats
146148

147149
# Inherit default transaction implementation
148150
use Nebulex.Adapter.Transaction
149151

150152
import Nebulex.Helpers
151153

152-
alias Cachex.Query
154+
alias Cachex.{Options, Query}
153155
alias Nebulex.Entry
154156

155157
@compile {:inline, to_ttl: 1}
156158

157-
## Adapter
159+
## Nebulex.Adapter
158160

159161
@impl true
160162
defmacro __before_compile__(_), do: :ok
@@ -167,14 +169,24 @@ defmodule Nebulex.Adapters.Cachex do
167169
Cachex
168170
])
169171

172+
stats =
173+
if opts[:stats_counter] do
174+
true
175+
else
176+
Options.get(opts, :stats, &is_boolean/1, false)
177+
end
178+
170179
child_spec =
171180
opts
172181
|> Keyword.put(:name, name)
182+
|> Keyword.put(:stats, stats)
173183
|> Cachex.child_spec()
174184

175-
{:ok, child_spec, %{name: name}}
185+
{:ok, child_spec, %{name: name, stats: stats}}
176186
end
177187

188+
## Nebulex.Adapter.Entry
189+
178190
@impl true
179191
def get(%{name: name}, key, _opts) do
180192
Cachex.get!(name, key)
@@ -278,41 +290,45 @@ defmodule Nebulex.Adapters.Cachex do
278290
end
279291

280292
@impl true
281-
def incr(%{name: name}, key, incr, :infinity, opts) do
282-
Cachex.incr!(name, key, incr, initial: opts[:default] || 0)
293+
def update_counter(%{name: name}, key, amount, :infinity, default, _opts) do
294+
Cachex.incr!(name, key, amount, initial: default)
283295
end
284296

285-
def incr(%{name: name}, key, incr, ttl, opts) do
297+
def update_counter(%{name: name}, key, incr, ttl, default, _opts) do
286298
# FIXME: This is a workaround since Cachex does not support `:ttl` here.
287299
# Fix it if a better solution comes up.
288300
Cachex.transaction!(name, [key], fn worker ->
289-
counter = Cachex.incr!(worker, key, incr, initial: opts[:default] || 0)
301+
counter = Cachex.incr!(worker, key, incr, initial: default)
290302
if ttl = to_ttl(ttl), do: Cachex.expire!(worker, key, ttl)
291303
counter
292304
end)
293305
end
294306

307+
## Nebulex.Adapter.Queryable
308+
295309
@impl true
296-
def size(%{name: name}) do
310+
def execute(%{name: name}, :count_all, nil, _opts) do
297311
Cachex.size!(name)
298312
end
299313

300-
@impl true
301-
def flush(%{name: name}) do
302-
size = Cachex.size!(name)
303-
true = Cachex.reset!(name)
304-
size
314+
def execute(%{name: name}, :delete_all, nil, _opts) do
315+
Cachex.clear!(name)
305316
end
306317

307-
## Queryable
318+
def execute(%{name: name}, :delete_all, :expired, _opts) do
319+
Cachex.purge!(name)
320+
end
308321

309-
@impl true
310-
def all(adapter_meta, query, opts) do
322+
def execute(adapter_meta, :all, query, opts) do
311323
adapter_meta
312324
|> stream(query, opts)
313325
|> Enum.to_list()
314326
end
315327

328+
def execute(_adapter_meta, operation, query, _opts) do
329+
raise Nebulex.QueryError, message: "unsupported #{operation}", query: query
330+
end
331+
316332
@impl true
317333
def stream(adapter_meta, nil, opts) do
318334
stream(adapter_meta, Query.create(true, :key), opts)
@@ -344,7 +360,7 @@ defmodule Nebulex.Adapters.Cachex do
344360

345361
defp maybe_return_entry(query, _return), do: query
346362

347-
## Persistence
363+
## Nebulex.Adapter.Persistence
348364

349365
@impl true
350366
def dump(%{name: name}, path, opts) do
@@ -362,6 +378,25 @@ defmodule Nebulex.Adapters.Cachex do
362378
end
363379
end
364380

381+
## Nebulex.Adapter.Stats
382+
383+
@impl true
384+
def stats(%{name: name, stats: stats}) do
385+
if stats do
386+
# IO.puts "#=> Stats: #{inspect(Cachex.stats(name))}"
387+
388+
{meta, stats} =
389+
name
390+
|> Cachex.stats!()
391+
|> Map.pop(:meta, %{})
392+
393+
%Nebulex.Stats{
394+
measurements: stats,
395+
metadata: meta
396+
}
397+
end
398+
end
399+
365400
## Private Functions
366401

367402
defp to_ttl(:infinity), do: nil

mix.exs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
defmodule NebulexAdaptersCachex.MixProject do
22
use Mix.Project
33

4-
@version "1.0.0-dev"
54
@source_url "https://github.com/cabol/nebulex_adapters_cachex"
5+
@version "1.0.0-dev"
6+
@nbx_vsn "2.0.0-rc.2"
67

78
def project do
89
[
@@ -28,7 +29,7 @@ defmodule NebulexAdaptersCachex.MixProject do
2829

2930
# Hex
3031
package: package(),
31-
description: "Nebulex adapter for Cachex",
32+
description: "A Nebulex adapter for Cachex",
3233

3334
# Docs
3435
docs: [
@@ -69,16 +70,18 @@ defmodule NebulexAdaptersCachex.MixProject do
6970

7071
defp nebulex_dep do
7172
if path = System.get_env("NEBULEX_PATH") do
72-
{:nebulex, "~> 2.0.0-rc.1", path: path}
73+
{:nebulex, "~> #{@nbx_vsn}", path: path}
7374
else
74-
{:nebulex, "~> 2.0.0-rc.1"}
75+
# {:nebulex, "~> #{@nbx_vsn}"}
76+
{:nebulex, github: "cabol/nebulex"}
7577
end
7678
end
7779

7880
defp aliases do
7981
[
8082
"nbx.setup": [
81-
"cmd git clone --depth 1 --branch master https://github.com/cabol/nebulex.git"
83+
"cmd rm -rf nebulex",
84+
"cmd git clone --depth 1 --branch master https://github.com/cabol/nebulex"
8285
],
8386
check: [
8487
"compile --warnings-as-errors",

mix.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
2020
"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},
2121
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
22-
"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"},
22+
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},
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"},
25+
"nebulex": {:git, "https://github.com/cabol/nebulex.git", "de11a4b0694a860ba4ce8ee42638ad982fe74494", []},
2626
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
27-
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"},
27+
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
2828
"sleeplocks": {:hex, :sleeplocks, "1.1.1", "3d462a0639a6ef36cc75d6038b7393ae537ab394641beb59830a1b8271faeed3", [:rebar3], [], "hexpm", "84ee37aeff4d0d92b290fff986d6a95ac5eedf9b383fadfd1d88e9b84a1c02e1"},
29-
"sobelow": {:hex, :sobelow, "0.10.6", "ac9ebd742035b119eb00a4b1416098b88a4d54d2f262a42602e1e9e3ed4c2afd", [:mix], [], "hexpm", "06e426b8dc0bc80ab1333ce89b904c720474824825b9ceb3dc424eb0f731b6e9"},
29+
"sobelow": {:hex, :sobelow, "0.11.0", "cdc17e3a9f1ea78dc55dbe0a03121cb6767fef737c6d9f1e62ee7e78730abccc", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "c57807bfe6f231338b657781f89ef0320b66a0dbe779aa911d6ed27cfa14ae6e"},
3030
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
3131
"stream_data": {:hex, :stream_data, "0.5.0", "b27641e58941685c75b353577dc602c9d2c12292dd84babf506c2033cd97893e", [:mix], [], "hexpm", "012bd2eec069ada4db3411f9115ccafa38540a3c78c4c0349f151fc761b9e271"},
3232
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},

test/nebulex_adapters_cachex/local_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,20 @@ defmodule NebulexAdaptersCachex.LocalTest do
77
alias NebulexAdaptersCachex.TestCache.Local, as: Cache
88

99
setup_with_dynamic_cache(Cache, :local_with_cachex)
10+
11+
describe "count_all/2 error:" do
12+
test "unsupported query", %{cache: cache} do
13+
assert_raise Nebulex.QueryError, ~r"unsupported count_all in query", fn ->
14+
cache.count_all(:unexpired)
15+
end
16+
end
17+
end
18+
19+
describe "delete_all/2 error:" do
20+
test "unsupported query", %{cache: cache} do
21+
assert_raise Nebulex.QueryError, ~r"unsupported delete_all in query", fn ->
22+
cache.delete_all(:unexpired)
23+
end
24+
end
25+
end
1026
end

0 commit comments

Comments
 (0)