-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathREADME.md
272 lines (209 loc) · 8.94 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# Nebulex 🌌
> In-memory and distributed caching toolkit for Elixir.

[](https://coveralls.io/github/cabol/nebulex)
[](http://inch-ci.org/github/cabol/nebulex)
[](https://hex.pm/packages/nebulex)
[](https://hexdocs.pm/nebulex)
[](LICENSE)
Nebulex provides support for transparently adding caching into an existing
Elixir application. Similar to [Ecto][ecto], the caching abstraction allows
consistent use of various caching solutions with minimal impact on the code.
Nebulex cache abstraction shields developers from directly dealing with the
underlying caching implementations, such as [Redis][redis],
[Memcached][memcached], or even other Elixir cache implementations like
[Cachex][cachex]. Additionally, it provides totally out-of-box features such as
[cache usage patterns][cache_patterns],
[declarative annotation-based caching][nbx_caching], and
[distributed cache topologies][cache_topologies], among others.
See the [getting started guide](http://hexdocs.pm/nebulex/getting-started.html)
and the [online documentation](http://hexdocs.pm/nebulex/Nebulex.html)
for more information.
[ecto]: https://github.com/elixir-ecto/ecto
[cachex]: https://github.com/whitfin/cachex
[redis]: https://redis.io/
[memcached]: https://memcached.org/
[nbx_caching]: http://hexdocs.pm/nebulex/Nebulex.Caching.html
[cache_patterns]: http://hexdocs.pm/nebulex/cache-usage-patterns.html
[cache_topologies]: https://docs.oracle.com/middleware/1221/coherence/develop-applications/cache_intro.htm
## Usage
You need to add `nebulex` as a dependency to your `mix.exs` file. However, in
the case you want to use an external (a non built-in adapter) cache adapter,
you also have to add the proper dependency to your `mix.exs` file.
The supported caches and their adapters are:
Cache | Nebulex Adapter | Dependency
:-----| :---------------| :---------
Generational Local Cache | [Nebulex.Adapters.Local][la] | Built-In
Partitioned | [Nebulex.Adapters.Partitioned][pa] | Built-In
Replicated | [Nebulex.Adapters.Replicated][ra] | Built-In
Multilevel | [Nebulex.Adapters.Multilevel][ma] | Built-In
Nil (special adapter that disables the cache) | [Nebulex.Adapters.Nil][nil] | Built-In
Cachex | Nebulex.Adapters.Cachex | [nebulex_adapters_cachex][nbx_cachex]
Redis | NebulexRedisAdapter | [nebulex_redis_adapter][nbx_redis]
[la]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Local.html
[pa]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Partitioned.html
[ra]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Replicated.html
[ma]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Multilevel.html
[nil]: http://hexdocs.pm/nebulex/Nebulex.Adapters.Nil.html
[nbx_cachex]: https://github.com/cabol/nebulex_adapters_cachex
[nbx_redis]: https://github.com/cabol/nebulex_redis_adapter
For example, if you want to use a built-in cache, add to your `mix.exs` file:
```elixir
def deps do
[
{:nebulex, "~> 2.1"},
{:shards, "~> 1.0"}, #=> When using :shards as backend
{:decorator, "~> 1.3"}, #=> When using Caching Annotations
{:telemetry, "~> 0.4"} #=> When using the Telemetry events (Nebulex stats)
]
end
```
In order to give more flexibility and fetch only needed dependencies, Nebulex
makes all dependencies optional. For example:
* For intensive workloads, you may want to use `:shards` as the backend for
the local adapter and having partitioned tables. In such a case, you have
to add `:shards` to the dependency list.
* For enabling the usage of
[declarative annotation-based caching via decorators][nbx_caching],
you have to add `:decorator` to the dependency list.
* For enabling Telemetry events to be dispatched when using Nebulex,
you have to add `:telemetry` to the dependency list.
See [telemetry guide][telemetry].
* If you want to use an external adapter (e.g: Cachex or Redis adapter), you
have to add the adapter dependency too.
[telemetry]: http://hexdocs.pm/nebulex/telemetry.html
Then run `mix deps.get` in your shell to fetch the dependencies. If you want to
use another cache adapter, just choose the proper dependency from the table
above.
Finally, in the cache definition, you will need to specify the `adapter:`
respective to the chosen dependency. For the local built-in cache it is:
```elixir
defmodule MyApp.Cache do
use Nebulex.Cache,
otp_app: :my_app,
adapter: Nebulex.Adapters.Local
end
```
## Quickstart example
Assuming you are using `Ecto` and you want to use declarative caching:
```elixir
# In the config/config.exs file
config :my_app, MyApp.PartitionedCache,
primary: [
gc_interval: :timer.hours(12),
backend: :shards,
partitions: 2
]
# Defining a Cache with a partitioned topology
defmodule MyApp.PartitionedCache do
use Nebulex.Cache,
otp_app: :my_app,
adapter: Nebulex.Adapters.Partitioned,
primary_storage_adapter: Nebulex.Adapters.Local
end
# Some Ecto schema
defmodule MyApp.Accounts.User do
use Ecto.Schema
schema "users" do
field(:username, :string)
field(:password, :string)
field(:role, :string)
end
def changeset(user, attrs) do
user
|> cast(attrs, [:username, :password, :role])
|> validate_required([:username, :password, :role])
end
end
# The Accounts context
defmodule MyApp.Accounts do
use Nebulex.Caching
alias MyApp.Accounts.User
alias MyApp.PartitionedCache, as: Cache
alias MyApp.Repo
@ttl :timer.hours(1)
@decorate cacheable(cache: Cache, key: {User, id}, opts: [ttl: @ttl])
def get_user!(id) do
Repo.get!(User, id)
end
@decorate cacheable(cache: Cache, key: {User, username}, opts: [ttl: @ttl])
def get_user_by_username(username) do
Repo.get_by(User, [username: username])
end
@decorate cache_put(
cache: Cache,
keys: [{User, usr.id}, {User, usr.username}],
match: &match_update/1
)
def update_user(%User{} = usr, attrs) do
usr
|> User.changeset(attrs)
|> Repo.update()
end
defp match_update({:ok, usr}), do: {true, usr}
defp match_update({:error, _}), do: false
@decorate cache_evict(cache: Cache, keys: [{User, usr.id}, {User, usr.username}])
def delete_user(%User{} = usr) do
Repo.delete(usr)
end
def create_user(attrs \\ %{}) do
%User{}
|> User.changeset(attrs)
|> Repo.insert()
end
end
```
See more [Nebulex examples](https://github.com/cabol/nebulex_examples).
## Important links
* [Getting Started](http://hexdocs.pm/nebulex/getting-started.html)
* [Documentation](http://hexdocs.pm/nebulex/Nebulex.html)
* [Cache Usage Patterns](http://hexdocs.pm/nebulex/cache-usage-patterns.html)
* [Instrumenting the Cache with Telemetry](http://hexdocs.pm/nebulex/telemetry.html)
* [Migrating to v2.x](http://hexdocs.pm/nebulex/migrating-to-v2.html)
* [Examples](https://github.com/cabol/nebulex_examples)
## Testing
Testing by default spawns nodes internally for distributed tests. To run tests
that do not require clustering, exclude the `clustered` tag:
```
$ mix test --exclude clustered
```
If you have issues running the clustered tests try running:
```
$ epmd -daemon
```
before running the tests.
## Benchmarks
Nebulex provides a set of basic benchmark tests using the library
[benchee](https://github.com/PragTob/benchee), and they are located within
the directory [benchmarks](./benchmarks).
To run a benchmark test you have to run:
```
$ MIX_ENV=test mix run benchmarks/{BENCH_TEST_FILE}
```
Where `BENCH_TEST_FILE` can be any of:
* `local_with_ets_bench.exs`: benchmark for the local adapter using
`:ets` backend.
* `local_with_shards_bench.exs`: benchmark for the local adapter using
`:shards` backend.
* `partitioned_bench.exs`: benchmark for the partitioned adapter.
For example, for running the benchmark for the local adapter using `:shards`
backend:
```
$ MIX_ENV=test mix run benchmarks/local_with_shards_bench.exs
```
Additionally, you can also run performance tests using `:basho_bench`.
See [nebulex_bench example](https://github.com/cabol/nebulex_examples/tree/master/nebulex_bench)
for more information.
## Contributing
Contributions to Nebulex are very welcome and appreciated!
Use the [issue tracker](https://github.com/cabol/nebulex/issues) for bug reports
or feature requests. Open a [pull request](https://github.com/cabol/nebulex/pulls)
when you are ready to contribute.
When submitting a pull request you should not update the [CHANGELOG.md](CHANGELOG.md),
and also make sure you test your changes thoroughly, include unit tests
alongside new or changed code.
Before to submit a PR it is highly recommended to run `mix check` and ensure
all checks run successfully.
## Copyright and License
Copyright (c) 2017, Carlos Bolaños.
Nebulex source code is licensed under the [MIT License](LICENSE).