|
1 | 1 | defmodule Nebulex.Adapters.Cachex do |
2 | 2 | @moduledoc """ |
3 | 3 | Nebulex adapter for Cachex. |
| 4 | +
|
| 5 | + Cachex is one of the most used cache (maybe the most used one) in Elixir. |
| 6 | + For that reason, Nebulex provides this adapter to use Cachex via Nebulex |
| 7 | + taking advantage of all their benefits and/or features. |
| 8 | +
|
| 9 | + Cachex is now a very good alternative for local caching aside from the |
| 10 | + built-in local adapter provided by Nebulex. Besides, you can setup distributed |
| 11 | + topologies by using the distributed adapters Nebulex brings with it and set up |
| 12 | + Cachex as local cache. |
| 13 | +
|
| 14 | + ## Options |
| 15 | +
|
| 16 | + Since Nebulex is just a wrapper on top of Cachex, the options are the same as |
| 17 | + [Cachex.start_link/1][cachex_start_link]. |
| 18 | +
|
| 19 | + [cachex_start_link]: https://hexdocs.pm/cachex/Cachex.html#start_link/1 |
| 20 | +
|
| 21 | + ## Example |
| 22 | +
|
| 23 | + You can define a cache using Cachex as follows: |
| 24 | +
|
| 25 | + defmodule MyApp.Cache do |
| 26 | + use Nebulex.Cache, |
| 27 | + otp_app: :my_app, |
| 28 | + adapter: Nebulex.Adapters.Cachex |
| 29 | + end |
| 30 | +
|
| 31 | + Where the configuration for the cache must be in your application |
| 32 | + environment, usually defined in your `config/config.exs`: |
| 33 | +
|
| 34 | + config :my_app, MyApp.Cache, |
| 35 | + limit: 1_000_000, |
| 36 | + stats: true, |
| 37 | + ... |
| 38 | +
|
| 39 | + If your application was generated with a supervisor (by passing `--sup` |
| 40 | + to `mix new`) you will have a `lib/my_app/application.ex` file containing |
| 41 | + the application start callback that defines and starts your supervisor. |
| 42 | + You just need to edit the `start/2` function to start the cache as a |
| 43 | + supervisor on your application's supervisor: |
| 44 | +
|
| 45 | + def start(_type, _args) do |
| 46 | + children = [ |
| 47 | + {MyApp.Cache, []}, |
| 48 | + ] |
| 49 | +
|
| 50 | + ... |
| 51 | + end |
| 52 | +
|
| 53 | + Since Cachex uses macros for some configuration options, you could also |
| 54 | + pass the options in runtime when the cache is started, either by calling |
| 55 | + `MyApp.Cache.start_link/1` directly, or in your app supervision tree: |
| 56 | +
|
| 57 | + def start(_type, _args) do |
| 58 | + children = [ |
| 59 | + {MyApp.Cache, cachex_opts()}, |
| 60 | + ] |
| 61 | +
|
| 62 | + ... |
| 63 | + end |
| 64 | +
|
| 65 | + defp cachex_opts do |
| 66 | + import Cachex.Spec |
| 67 | +
|
| 68 | + [ |
| 69 | + expiration: expiration( |
| 70 | + # default record expiration |
| 71 | + default: :timer.seconds(60), |
| 72 | +
|
| 73 | + # how often cleanup should occur |
| 74 | + interval: :timer.seconds(30), |
| 75 | +
|
| 76 | + # whether to enable lazy checking |
| 77 | + lazy: true |
| 78 | + ), |
| 79 | +
|
| 80 | + # complex limit |
| 81 | + limit: limit( |
| 82 | + size: 500, |
| 83 | + policy: Cachex.Policy.LRW, |
| 84 | + reclaim: 0.5, |
| 85 | + options: [] |
| 86 | + ), |
| 87 | +
|
| 88 | + ... |
| 89 | + ] |
| 90 | + end |
| 91 | +
|
| 92 | + > See [Cachex.start_link/1][cachex_start_link] for more information. |
| 93 | +
|
| 94 | + ## Distributed caching topologies |
| 95 | +
|
| 96 | + In the same way we use the distributed adapters and the multilevel one to |
| 97 | + create distributed topologies, we can also do the same but instead of using |
| 98 | + the built-in local adapter using Cachex. |
| 99 | +
|
| 100 | + For example, let's define a multi-level cache (near cache topology), where |
| 101 | + the L1 is a local cache using Cachex and the L2 is a partitioned cache. |
| 102 | +
|
| 103 | + defmodule MyApp.NearCache do |
| 104 | + use Nebulex.Cache, |
| 105 | + otp_app: :nebulex, |
| 106 | + adapter: Nebulex.Adapters.Multilevel |
| 107 | +
|
| 108 | + defmodule L1 do |
| 109 | + use Nebulex.Cache, |
| 110 | + otp_app: :nebulex, |
| 111 | + adapter: Nebulex.Adapters.Cachex |
| 112 | + end |
| 113 | +
|
| 114 | + defmodule L2 do |
| 115 | + use Nebulex.Cache, |
| 116 | + otp_app: :nebulex, |
| 117 | + adapter: Nebulex.Adapters.Partitioned, |
| 118 | + primary_storage_adapter: Nebulex.Adapters.Cachex |
| 119 | + end |
| 120 | + end |
| 121 | +
|
| 122 | + And the configuration may look like: |
| 123 | +
|
| 124 | + config :my_app, MyApp.NearCache, |
| 125 | + model: :inclusive, |
| 126 | + levels: [ |
| 127 | + {MyApp.NearCache.L1, [limit: 100_000]}, |
| 128 | + {MyApp.NearCache.L2, primary: [limit: 1_000_000]} |
| 129 | + ] |
| 130 | +
|
| 131 | + > **NOTE:** You could also use [NebulexRedisAdapter][nbx_redis_adapter] for |
| 132 | + L2, it would be matter of changing the adapter for the L2 and the |
| 133 | + configuration for set up Redis adapter. |
| 134 | +
|
| 135 | + [nbx_redis_adapter]: https://github.com/cabol/nebulex_redis_adapter |
| 136 | +
|
| 137 | + See [Nebulex examples](https://github.com/cabol/nebulex_examples). You will |
| 138 | + find examples for all different topologies, even using other adapters like |
| 139 | + Redis; for all examples you can just replace `Nebulex.Adapters.Local` by |
| 140 | + `Nebulex.Adapters.Cachex`. |
4 | 141 | """ |
5 | 142 |
|
6 | 143 | # Provide Cache Implementation |
|
0 commit comments