Skip to content

Commit

Permalink
Migrate to Nebulex v2
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Oct 23, 2020
1 parent 0a29e6e commit 1de1087
Show file tree
Hide file tree
Showing 24 changed files with 851 additions and 683 deletions.
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
1 change: 1 addition & 0 deletions .dialyzer_ignore.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
nebulex_test:
name: 'NebulexRedisAdapter Test (Elixir ${{ matrix.elixir }} OTP ${{ matrix.otp }})'
runs-on: ubuntu-latest
strategy:
matrix:
include:
- elixir: 1.10.x
otp: 23.x
- elixir: 1.10.x
otp: 22.x
- elixir: 1.9.x
otp: 22.x
env:
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'
MIX_ENV: test
NBX_TEST: true
REDIS_CLUSTER_IP: '0.0.0.0'
steps:
- uses: actions/checkout@v2
- name: Start Redis
run: docker-compose up -d
- uses: actions/setup-elixir@v1
with:
otp-version: '${{ matrix.otp }}'
elixir-version: '${{ matrix.elixir }}'
- uses: actions/cache@v1
with:
path: deps
key: >-
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-mix-${{
hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
restore-keys: |
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-mix-
- uses: actions/cache@v1
with:
path: _build
key: >-
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-build-${{
hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
restore-keys: |
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-build-
- name: Install Dependencies
run: mix deps.get
- 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: Run Tests
run: |
epmd -daemon
mix coveralls.github
- uses: actions/cache@v1
with:
path: priv/plts
key: '${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v1'
restore-keys: |
${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plt-v1
- name: Run Dialyzer
run: mix dialyzer --format short
- 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 @@ -17,3 +17,4 @@ erl_crash.dump
.elixir*
.vs*
mix.lock
/priv
39 changes: 0 additions & 39 deletions .travis.yml

This file was deleted.

40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# NebulexRedisAdapter
> ### Nebulex adapter for Redis with cluster support.
[![Build Status](https://travis-ci.org/cabol/nebulex_redis_adapter.svg?branch=master)](https://travis-ci.org/cabol/nebulex_redis_adapter)
![CI](https://github.com/cabol/nebulex_redis_adapter/workflows/CI/badge.svg)
[![Coverage Status](https://coveralls.io/repos/github/cabol/nebulex_redis_adapter/badge.svg?branch=master)](https://coveralls.io/github/cabol/nebulex_redis_adapter?branch=master)
[![Inline docs](http://inch-ci.org/github/cabol/nebulex_redis_adapter.svg)](http://inch-ci.org/github/cabol/nebulex_redis_adapter)
[![Hex Version](https://img.shields.io/hexpm/v/nebulex_redis_adapter.svg)](https://hex.pm/packages/nebulex_redis_adapter)
Expand Down Expand Up @@ -71,12 +71,11 @@ There are different ways to support distributed caching when using

### Redis Cluster

Redis can be setup in distributed fashion by means of [Redis Cluster][redis_cluster],
which is a built-in feature since version 3.0 (or greater). The adapter provides
the `:redis_cluster` mode to setup **Redis Cluster** from client-side
automatically and be able to use it transparently.

[redis_cluster]: https://redis.io/topics/cluster-tutorial
[Redis Cluster](https://redis.io/topics/cluster-tutorial) is a built-in feature
in Redis since version 3, and it may be the most convenient and recommendable
way to set up Redis in a cluster and have a distributed cache storage out-of-box.
This adapter provides the `:redis_cluster` mode to set up **Redis Cluster**
from the client-side automatically and be able to use it transparently.

First of all, ensure you have **Redis Cluster** configured and running.

Expand Down Expand Up @@ -124,11 +123,10 @@ configured by the adapter once it gets the cluster slots info.
> This one could be the easiest and recommended way for distributed caching
using Redis and **NebulexRedisAdapter**.

### Client-side Cluster based on Sharding (and consistent hashing)
### Client-side Cluster based on Sharding

**NebulexRedisAdapter** also brings with a simple client-side cluster
implementation based on Sharding as distribution model and consistent
hashing for node resolution.
implementation based on Sharding distribution model.

We define our cache normally:

Expand All @@ -140,13 +138,31 @@ defmodule MyApp.ClusteredCache do
end
```

The Keyslot module using consistent hashing:

```elixir
defmodule MyApp.ClusteredCache.Keyslot do
use Nebulex.Adapter.Keyslot

@impl true
def hash_slot(key, range) do
key
|> :erlang.phash2()
|> :jchash.compute(range)
end
end
```

And then, within the config:

```elixir
config :my_app, MayApp.ClusteredCache,
# Enable client-side cluster mode
mode: :cluster,

# Keyslot with consistent hashing
keyslot: MyApp.ClusteredCache.Keyslot,

# Nodes config (each node has its own options)
nodes: [
node1: [
Expand Down Expand Up @@ -175,6 +191,9 @@ config :my_app, MayApp.ClusteredCache,
]
```

> **NOTE:** It is highly recommendable to provide a consistent hashing
implementation for `Nebulex.Adapter.Keyslot`.

That's all, the rest of the work is done by **NebulexRedisAdapter**
automatically.

Expand Down Expand Up @@ -289,7 +308,6 @@ Before to submit a PR it is highly recommended to run:

* `export NBX_TEST=true` to fetch Nebulex from GH directly and be able to
re-use shared tests.
* `mix test` to run tests
* `mix coveralls.html && open cover/excoveralls.html` to run tests and check
out code coverage (expected 100%).
* `mix format && mix credo --strict` to format your code properly and find code
Expand Down
8 changes: 3 additions & 5 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ use Mix.Config

# Redis Standalone
config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.Standalone,
version_generator: Nebulex.Version.Timestamp,
conn_opts: [
host: "127.0.0.1",
port: 6379
]

# Redis test cache
config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.Cluster,
version_generator: Nebulex.Version.Timestamp,
mode: :cluster,
keyslot: NebulexRedisAdapter.TestCache.Keyslot,
nodes: [
node1: [
conn_opts: [
Expand All @@ -35,7 +34,6 @@ config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.Cluster,

# Redis test clustered cache
config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.RedisCluster,
version_generator: Nebulex.Version.Timestamp,
mode: :redis_cluster,
master_nodes: [
[
Expand Down Expand Up @@ -68,9 +66,9 @@ config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.RedisClusterConnErr
]

# Redis test clustered cache
config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.RedisClusterWithHashSlot,
config :nebulex_redis_adapter, NebulexRedisAdapter.TestCache.RedisClusterWithKeyslot,
mode: :redis_cluster,
hash_slot: NebulexRedisAdapter.TestCache.HashSlot,
keyslot: NebulexRedisAdapter.TestCache.Keyslot,
pool_size: 2,
master_nodes: [
[
Expand Down
4 changes: 1 addition & 3 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"skip_files": [
"test/*",
"lib/nebulex_redis_adapter/cluster/hash_slot.ex",
"lib/nebulex_redis_adapter/list.ex"
"test/*"
],

"coverage_options": {
Expand Down
Loading

0 comments on commit 1de1087

Please sign in to comment.