1- # NebulexRedisAdapter
1+ # Nebulex.Adapters.Redis
22> Nebulex adapter for Redis (including [ Redis Cluster] [ redis_cluster ] support).
33
4- ![ CI] ( https://github.com/cabol /nebulex_redis_adapter/workflows/CI/badge.svg )
5- [ ![ 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 )
4+ ![ CI] ( https://github.com/elixir-nebulex /nebulex_redis_adapter/workflows/CI/badge.svg )
5+ [ ![ Codecov ] ( https://codecov .io/gh/elixir-nebulex/ nebulex_redis_adapter/branch/v3.0.0-dev/graph/ badge.svg )] ( https://codecov .io/gh/elixir-nebulex /nebulex_redis_adapter/ branch/v3.0.0-dev/graph/badge.svg )
66[ ![ Hex Version] ( https://img.shields.io/hexpm/v/nebulex_redis_adapter.svg )] ( https://hex.pm/packages/nebulex_redis_adapter )
7- [ ![ Docs] ( https://img.shields.io/badge/docs-hexpm-blue.svg )] ( https://hexdocs.pm/nebulex_redis_adapter )
7+ [ ![ Documentation] ( https://img.shields.io/badge/Documentation-ff69b4 )] ( https://hexdocs.pm/nebulex_redis_adapter )
8+
9+ ## About
810
911This adapter uses [ Redix] ( https://github.com/whatyouhide/redix ) ; a Redis driver
1012for Elixir.
@@ -15,8 +17,8 @@ next sections.
1517See also [ online documentation] [ nbx_redis_adapter ]
1618and [ Redis cache example] [ nbx_redis_example ] .
1719
18- [ nbx_redis_adapter ] : http://hexdocs.pm/nebulex_redis_adapter/NebulexRedisAdapter .html
19- [ nbx_redis_example ] : https://github.com/cabol /nebulex_examples/tree/master/redis_cache
20+ [ nbx_redis_adapter ] : http://hexdocs.pm/nebulex_redis_adapter/Nebulex.Adapters.Redis .html
21+ [ nbx_redis_example ] : https://github.com/elixir-nebulex /nebulex_examples/tree/master/redis_cache
2022[ redis_cluster ] : https://redis.io/topics/cluster-tutorial
2123
2224## Installation
@@ -26,9 +28,9 @@ Add `:nebulex_redis_adapter` to your list of dependencies in `mix.exs`:
2628``` elixir
2729defp deps do
2830 [
29- {:nebulex_redis_adapter , " ~> 2.3 " },
30- {:crc , " ~> 0.10" }, # => Needed when using Redis Cluster
31- {:jchash , " ~> 0.1.4 " } # => Needed when using consistent-hashing
31+ {:nebulex_redis_adapter , " ~> 3.0.0-rc.0 " },
32+ {:crc , " ~> 0.10" }, # => Needed when using `:redis_cluster` mode
33+ {:ex_hash_ring , " ~> 6.0 " } # => Needed when using `:client_side_cluster` mode
3234 ]
3335end
3436```
@@ -38,8 +40,8 @@ needed ones. For example:
3840
3941 * ` :crc ` - Required when using the adapter in mode ` :redis_cluster ` .
4042 See [ Redis Cluster] [ redis_cluster ] .
41- * ` :jchash ` - Required if you want to use consistent-hashing when using the
42- adapter in mode ` :client_side_cluster ` .
43+ * ` :ex_hash_ring ` - Required when using the adapter in mode
44+ ` :client_side_cluster ` .
4345
4446Then run ` mix deps.get ` to fetch the dependencies.
4547
@@ -51,7 +53,7 @@ After installing, we can define our cache to use Redis adapter as follows:
5153defmodule MyApp .RedisCache do
5254 use Nebulex .Cache ,
5355 otp_app: :my_app ,
54- adapter: NebulexRedisAdapter
56+ adapter: Nebulex . Adapters . Redis
5557end
5658```
5759
@@ -69,15 +71,15 @@ config :my_app, MyApp.RedisCache,
6971
7072Since this adapter is implemented by means of ` Redix ` , it inherits the same
7173options, including regular Redis options and connection options as well. For
72- more information about the options, please check out ` NebulexRedisAdapter `
74+ more information about the options, please check out ` Nebulex.Adapters.Redis `
7375module and also [ Redix] ( https://github.com/whatyouhide/redix ) .
7476
7577See also [ Redis cache example] [ nbx_redis_example ] .
7678
7779## Distributed Caching
7880
7981There are different ways to support distributed caching when using
80- ** NebulexRedisAdapter ** .
82+ ** Nebulex.Adapters.Redis ** .
8183
8284### Redis Cluster
8385
@@ -95,7 +97,7 @@ Then we can define our cache which will use **Redis Cluster**:
9597defmodule MyApp .RedisClusterCache do
9698 use Nebulex .Cache ,
9799 otp_app: :my_app ,
98- adapter: NebulexRedisAdapter
100+ adapter: Nebulex . Adapters . Redis
99101end
100102```
101103
@@ -127,20 +129,20 @@ The pool of connections to the different master nodes is automatically
127129configured by the adapter once it gets the cluster slots info.
128130
129131> This one could be the easiest and recommended way for distributed caching
130- using Redis and ** NebulexRedisAdapter ** .
132+ using Redis and ** Nebulex.Adapters.Redis ** .
131133
132- ### Client-side Cluster based on Sharding
134+ ### Client-side Cluster
133135
134- ** NebulexRedisAdapter ** also brings with a simple client-side cluster
135- implementation based on Sharding distribution model.
136+ ** Nebulex.Adapters.Redis ** also brings with a simple client-side cluster
137+ implementation based on sharding distribution model.
136138
137139We define our cache normally:
138140
139141``` elixir
140142defmodule MyApp .ClusteredCache do
141143 use Nebulex .Cache ,
142144 otp_app: :my_app ,
143- adapter: NebulexRedisAdapter
145+ adapter: Nebulex . Adapters . Redis
144146end
145147```
146148
@@ -182,101 +184,41 @@ config :my_app, MyApp.ClusteredCache,
182184 ]
183185```
184186
185- By default, the adapter uses ` NebulexRedisAdapter.ClientCluster.Keyslot ` for the
186- keyslot. Besides, if ` :jchash ` is defined as dependency, the adapter will use
187- consistent-hashing automatically.
188-
189- > ** NOTE:** It is highly recommended to define the ` :jchash ` dependency
190- when using the adapter in ` :client_side_cluster ` mode.
191-
192- However, you can also provide your own implementation by implementing the
193- ` Nebulex.Adapter.Keyslot ` and set it into the ` :keyslot ` option. For example:
194-
195- ``` elixir
196- defmodule MyApp .ClusteredCache .Keyslot do
197- use Nebulex .Adapter .Keyslot
198-
199- @impl true
200- def hash_slot (key, range) do
201- # your implementation goes here
202- end
203- end
204- ```
205-
206- And the config:
207-
208- ``` elixir
209- config :my_app , MyApp .ClusteredCache ,
210- # Enable client-side cluster mode
211- mode: :client_side_cluster ,
212-
213- client_side_cluster: [
214- # Provided Keyslot implementation
215- keyslot: MyApp .ClusteredCache .Keyslot ,
216-
217- # Nodes config (each node has its own options)
218- nodes: [
219- .. .
220- ]
221- ]
222- ```
223-
224- ### Using ` Nebulex.Adapters.Partitioned `
225-
226- Another simple option is to use the ` Nebulex.Adapters.Partitioned ` and set as
227- local cache the ` NebulexRedisAdapter ` . The idea here is each Elixir node running
228- the distributed cache (` Nebulex.Adapters.Partitioned ` ) will have as local
229- backend or cache a Redis instance (handled by ` NebulexRedisAdapter ` ).
230-
231-
232- This example shows how the setup a distributed cache using
233- ` Nebulex.Adapters.Partitioned ` and ` NebulexRedisAdapter ` :
234-
235- ``` elixir
236- defmodule MyApp .DistributedCache do
237- use Nebulex .Cache ,
238- otp_app: :my_app ,
239- adapter: Nebulex .Adapters .Partitioned ,
240- primary_storage_adapter: NebulexRedisAdapter
241- end
242- ```
243-
244187### Using a Redis Proxy
245188
246189The other option is to use a proxy, like [ Envoy proxy] [ envoy ] or
247190[ Twemproxy] [ twemproxy ] on top of Redis. In this case, the proxy does the
248- distribution work, and from the adparter's side (** NebulexRedisAdapter ** ),
191+ distribution work, and from the adparter's side (** Nebulex.Adapters.Redis ** ),
249192it would be only configuration. Instead of connect the adapter against the
250193Redis nodes, we connect it against the proxy nodes, this means, in the config,
251194we setup the pool with the host and port pointing to the proxy.
252195
253196[ envoy ] : https://www.envoyproxy.io/
254197[ twemproxy ] : https://github.com/twitter/twemproxy
255198
256- ## Running Redis commands and/or pipelines
199+ ## Using the adapter as a Redis client
257200
258- Since ` NebulexRedisAdapter ` works on top of ` Redix ` and provides features like
259- connection pools and "Redis Cluster" support, it may be seen also as a sort of
260- Redis client, but it is meant to be used mainly with the Nebulex cache API.
261- However, Redis API is quite extensive and there are a lot of useful commands
262- we may want to run taking advantage of the ` NebulexRedisAdapter ` features.
263- Therefore, the adapter injects two additional/extended functions to the
264- defined cache: ` command!/2 ` and ` pipeline!/2 ` .
201+ Since the Redis adapter works on top of ` Redix ` and provides features like
202+ connection pools, "Redis Cluster", etc., it may also work as a Redis client.
203+ The Redis API is quite extensive, and there are many useful commands we may
204+ want to run, leveraging the Redis adapter features. Therefore, the adapter
205+ provides additional functions to do so.
265206
266207``` elixir
267- iex> MyCache .command! ([" LPUSH" , " mylist" , " world" ], key: " mylist" )
208+ iex> conn = MyCache .fetch_conn! ()
209+ iex> Redix .command! (conn, [" LPUSH" , " mylist" , " world" ])
2682101
269- iex> MyCache .command! ([" LPUSH" , " mylist" , " hello" ], key: " mylist " )
211+ iex> Redix .command! (conn, [" LPUSH" , " mylist" , " hello" ])
2702122
271- iex> MyCache .command! ([" LRANGE" , " mylist" , " 0" , " -1" ], key: " mylist " )
213+ iex> Redix .command! (conn, [" LRANGE" , " mylist" , " 0" , " -1" ])
272214[" hello" , " world" ]
273215
274- iex> [
216+ iex> conn = MyCache .fetch_conn! (key: " mylist" )
217+ iex> Redix .pipeline! (conn, [
275218.. .> [" LPUSH" , " mylist" , " world" ],
276219.. .> [" LPUSH" , " mylist" , " hello" ],
277220.. .> [" LRANGE" , " mylist" , " 0" , " -1" ]
278- .. .> ]
279- .. .> |> cache .pipeline! (key: " mylist" )
221+ .. .> ])
280222[1 , 2 , [" hello" , " world" ]]
281223```
282224
@@ -287,8 +229,8 @@ you have to pass the cache name explicitly.
287229
288230## Testing
289231
290- To run the ** NebulexRedisAdapter ** tests you will have to have Redis running
291- locally. ** NebulexRedisAdapter ** requires a complex setup for running tests
232+ To run the ** Nebulex.Adapters.Redis ** tests you will have to have Redis running
233+ locally. ** Nebulex.Adapters.Redis ** requires a complex setup for running tests
292234(since it needs a few instances running, for standalone, cluster and Redis
293235Cluster). For this reason, there is a [ docker-compose.yml] ( docker-compose.yml )
294236file in the repo so that you can use [ Docker] [ docker ] and
@@ -302,7 +244,7 @@ $ docker-compose up
302244[ docker ] : https://www.docker.com/
303245[ docker_compose ] : https://docs.docker.com/compose/
304246
305- Since ` NebulexRedisAdapter ` uses the support modules and shared tests
247+ Since ` Nebulex.Adapters.Redis ` uses the support modules and shared tests
306248from ` Nebulex ` and by default its test folder is not included in the Hex
307249dependency, the following steps are required for running the tests.
308250
@@ -357,9 +299,9 @@ $ MIX_ENV=test mix run benchmarks/benchmark.exs
357299
358300Contributions to Nebulex are very welcome and appreciated!
359301
360- Use the [ issue tracker] ( https://github.com/cabol /nebulex_redis_adapter/issues )
302+ Use the [ issue tracker] ( https://github.com/elixir-nebulex /nebulex_redis_adapter/issues )
361303for bug reports or feature requests. Open a
362- [ pull request] ( https://github.com/cabol /nebulex_redis_adapter/pulls )
304+ [ pull request] ( https://github.com/elixir-nebulex /nebulex_redis_adapter/pulls )
363305when you are ready to contribute.
364306
365307When submitting a pull request you should not update the [ CHANGELOG.md] ( CHANGELOG.md ) ,
@@ -373,4 +315,4 @@ all checks run successfully.
373315
374316Copyright (c) 2018, Carlos Bolaños.
375317
376- NebulexRedisAdapter source code is licensed under the [ MIT License] ( LICENSE ) .
318+ Nebulex.Adapters.Redis source code is licensed under the [ MIT License] ( LICENSE ) .
0 commit comments