Skip to content

Commit

Permalink
Fix and improve adapters
Browse files Browse the repository at this point in the history
- Refactor replicated, partitioned, and multilevel adapters to call a cache instead of an adapter directly
- This should fix issue #77
  • Loading branch information
cabol committed Sep 24, 2020
1 parent b48b1fe commit 5bfa880
Show file tree
Hide file tree
Showing 29 changed files with 734 additions and 481 deletions.
10 changes: 10 additions & 0 deletions .check.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
parallel: false,

tools: [
{:credo, "mix credo --strict --format oneline", order: 1, env: %{"MIX_ENV" => "test"}},
{:excoveralls, "mix coveralls.github", order: 2},
{:sobelow, "mix sobelow --exit --skip", order: 3},
{:dialyzer, "mix dialyzer --format short", order: 4, env: %{"MIX_ENV" => "test"}}
]
]
4 changes: 4 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
## Design Checks
{Credo.Check.Design.AliasUsage, priority: :low},

# Deactivate due to they're not compatible with current Elixir version
{Credo.Check.Refactor.MapInto, false},
{Credo.Check.Warning.LazyLogging, false},

## Readability Checks
{Credo.Check.Readability.MaxLineLength, priority: :low, max_length: 100},

Expand Down
17 changes: 4 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,17 @@ jobs:
- name: Compile Code
run: mix compile --warnings-as-errors

- name: Check Format
run: mix format --check-formatted

- name: Check Style
run: mix credo --strict

- name: Tests and Coverage
run: |
epmd -daemon
mix coveralls.github
- uses: actions/cache@v1
with:
path: priv/plts
key: ${{ runner.os }}-plt-v3-${{ env.MIX_ENV }}
restore-keys: |
${{ runner.os }}-plt-v3
- name: Dialyzer
run: mix dialyzer --format short
- name: Checks
run: |
epmd -daemon
mix check --except ex_unit
- name: Report Doc Coverage
run: MIX_ENV=docs mix inch.report
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ erl_crash.dump
.elixir*
.vs*
/priv
.sobelow*
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ some functions:
# In the config/config.exs file
config :my_app, MyApp.PartitionedCache,
primary: [
gc_interval: 86_400_000, #=> 1 day
gc_interval: :timer.seconds(3600),
backend: :shards,
partitions: 2
]
Expand All @@ -36,7 +36,8 @@ config :my_app, MyApp.PartitionedCache,
defmodule MyApp.PartitionedCache do
use Nebulex.Cache,
otp_app: :my_app,
adapter: Nebulex.Adapters.Partitioned
adapter: Nebulex.Adapters.Partitioned,
primary_storage_adapter: Nebulex.Adapters.Local
end

# Some Ecto schema
Expand Down
37 changes: 24 additions & 13 deletions guides/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Could be configured in `config/config.exs` like so:

```elixir
config :blog, Blog.Cache,
gc_interval: 86_400_000, #=> 1 day
gc_interval: :timer.seconds(3600),
backend: :shards,
partitions: System.schedulers_online() #=> The default
```
Expand Down Expand Up @@ -411,12 +411,14 @@ defmodule Blog.PartitionedCache do
end
```

> By default, the `primary_storage_adapter:` is set to `Nebulex.Adapters.Local`.
Could be configured with (`config/config.exs`):

```elixir
config :blog, Blog.PartitionedCache,
primary: [
gc_interval: 86_400_000,
gc_interval: :timer.seconds(3600),
backend: :shards,
partitions: 2
]
Expand Down Expand Up @@ -483,6 +485,18 @@ defmodule Blog.MultilevelCache do
use Nebulex.Cache,
otp_app: :blog,
adapter: Nebulex.Adapters.Multilevel

defmodule L1 do
use Nebulex.Cache,
otp_app: :nebulex,
adapter: Nebulex.Adapters.Local
end

defmodule L2 do
use Nebulex.Cache,
otp_app: :nebulex,
adapter: Nebulex.Adapters.Partitioned
end
end
```

Expand All @@ -492,17 +506,14 @@ Could be configured with (`config/config.exs`):
config :blog, Blog.MultilevelCache,
model: :inclusive,
levels: [
l1: [
gc_interval: 86_400_000,
backend: :shards
],
l2: [
adapter: Nebulex.Adapters.Partitioned,
primary: [
gc_interval: 86_400_000,
backend: :shards
]
]
{
Blog.MultilevelCache.L1,
gc_interval: :timer.seconds(3600), backend: :shards
},
{
Blog.MultilevelCache.L2,
primary: [gc_interval: :timer.seconds(3600), backend: :shards]
}
]
```

Expand Down
9 changes: 5 additions & 4 deletions guides/migrating-to-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ v2, just pointing out the areas the new documentation should be consulted on.

This is one of the biggest changes. Version 1.x, most of the configuration
options are resolved in compile-time, but this brings with a lot of limitations.
Since version 2.x, the only argument or option configured in compile-time is the
adapter when defining a new cache module, the rest of them are given via config
file or at startup time. For more information and examples, see `Nebulex.Cache`,
`Nebulex.Adapters.Local`, `Nebulex.Adapters.Partitioned`,
Since version 2.x, only few arguments are configured in compile-time when
defining a cache, e.g.: `otp_app:`, `adapter:`, and `primary_storage_adapter:`
(for partitioned and replicated adapters). The rest of configuration parameters
are given via config file or at startup time. For more information and examples,
see `Nebulex.Cache`, `Nebulex.Adapters.Local`, `Nebulex.Adapters.Partitioned`,
`Nebulex.Adapters.Replicated`, `Nebulex.Adapters.Multilevel`.

## Cache API
Expand Down
2 changes: 1 addition & 1 deletion lib/nebulex/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Nebulex.Adapter do
* `:name` - The nase of the cache.
* `:pid` - The PID returned by the child spec returned in `c:init/1`
"""
@type adapter_meta :: map
@type adapter_meta :: %{optional(atom) => term}

@type cache :: Nebulex.Cache.t()
@type key :: Nebulex.Cache.key()
Expand Down
12 changes: 6 additions & 6 deletions lib/nebulex/adapters/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ defmodule Nebulex.Adapters.Local do
config :my_app, MyApp.LocalCache,
backend: :shards,
gc_interval: 86_400_000,
gc_interval: 3_600_000,
max_size: 200_000,
allocated_memory: 2_000_000_000,
gc_cleanup_min_timeout: 10_000,
Expand All @@ -107,12 +107,12 @@ defmodule Nebulex.Adapters.Local do
config :my_app, MyApp.LocalCache,
backend: :shards,
gc_interval: 86_400_000,
gc_interval: 3_600_000,
max_size: 200_000,
allocated_memory: 2_000_000_000,
gc_cleanup_min_timeout: 10_000,
gc_cleanup_max_timeout: 600_000,
partitions: System.schedulers_online()
partitions: System.schedulers_online() * 2
For more information about the usage, check out `Nebulex.Cache`.
Expand Down Expand Up @@ -311,16 +311,16 @@ defmodule Nebulex.Adapters.Local do
entry(key: key, value: value, touched: touched, ttl: ttl)
end

do_put_all(name, backend, ref, entries, on_write)
do_put_all(on_write, name, backend, ref, entries)
end

defp do_put_all(name, backend, ref, entries, :put) do
defp do_put_all(:put, name, backend, ref, entries) do
name
|> put_entries(backend, entries)
|> update_stats(:put_all, {ref, entries})
end

defp do_put_all(name, backend, ref, entries, :put_new) do
defp do_put_all(:put_new, name, backend, ref, entries) do
name
|> put_new_entries(backend, entries)
|> update_stats(:put_all, {ref, entries})
Expand Down
Loading

0 comments on commit 5bfa880

Please sign in to comment.