|
| 1 | +# How to Choose the Right Cache Adapter |
| 2 | + |
| 3 | +ElixirCache supports multiple cache adapters, each with its own strengths and use cases. This guide will help you choose the most appropriate adapter for your specific needs. |
| 4 | + |
| 5 | +## Available Adapters |
| 6 | + |
| 7 | +ElixirCache provides the following adapters: |
| 8 | + |
| 9 | +1. `Cache.ETS` - Erlang Term Storage |
| 10 | +2. `Cache.DETS` - Disk-based ETS |
| 11 | +3. `Cache.Redis` - Redis-backed distributed cache |
| 12 | +4. `Cache.Agent` - Simple Agent-based in-memory cache |
| 13 | +5. `Cache.ConCache` - ConCache wrapper |
| 14 | +6. `Cache.Sandbox` - Isolated cache for testing |
| 15 | + |
| 16 | +## Choosing an Adapter |
| 17 | + |
| 18 | +### Cache.ETS |
| 19 | + |
| 20 | +**Best for:** |
| 21 | +- High-performance in-memory caching |
| 22 | +- Single node applications |
| 23 | +- Low-latency requirements |
| 24 | + |
| 25 | +**Configuration example:** |
| 26 | + |
| 27 | +```elixir |
| 28 | +defmodule MyApp.Cache do |
| 29 | + use Cache, |
| 30 | + adapter: Cache.ETS, |
| 31 | + name: :my_app_cache, |
| 32 | + opts: [ |
| 33 | + read_concurrency: true, |
| 34 | + write_concurrency: true |
| 35 | + ] |
| 36 | +end |
| 37 | +``` |
| 38 | + |
| 39 | +### Cache.DETS |
| 40 | + |
| 41 | +**Best for:** |
| 42 | +- Persistent caching across application restarts |
| 43 | +- Larger datasets that shouldn't be lost on restart |
| 44 | +- Less frequent access patterns |
| 45 | + |
| 46 | +**Configuration example:** |
| 47 | + |
| 48 | +```elixir |
| 49 | +defmodule MyApp.PersistentCache do |
| 50 | + use Cache, |
| 51 | + adapter: Cache.DETS, |
| 52 | + name: :my_app_persistent_cache, |
| 53 | + opts: [ |
| 54 | + file: "cache_data.dets" |
| 55 | + ] |
| 56 | +end |
| 57 | +``` |
| 58 | + |
| 59 | +### Cache.Redis |
| 60 | + |
| 61 | +**Best for:** |
| 62 | +- Distributed applications running on multiple nodes |
| 63 | +- Systems requiring shared cache across services |
| 64 | +- Applications needing advanced features like expiration, pub/sub, etc. |
| 65 | + |
| 66 | +**Configuration example:** |
| 67 | + |
| 68 | +```elixir |
| 69 | +defmodule MyApp.DistributedCache do |
| 70 | + use Cache, |
| 71 | + adapter: Cache.Redis, |
| 72 | + name: :my_app_redis_cache, |
| 73 | + opts: [ |
| 74 | + host: "localhost", |
| 75 | + port: 6379, |
| 76 | + pool_size: 5 |
| 77 | + ] |
| 78 | +end |
| 79 | +``` |
| 80 | + |
| 81 | +### Cache.Agent |
| 82 | + |
| 83 | +**Best for:** |
| 84 | +- Simple use cases |
| 85 | +- Small applications |
| 86 | +- Development environments |
| 87 | + |
| 88 | +**Configuration example:** |
| 89 | + |
| 90 | +```elixir |
| 91 | +defmodule MyApp.SimpleCache do |
| 92 | + use Cache, |
| 93 | + adapter: Cache.Agent, |
| 94 | + name: :my_app_simple_cache |
| 95 | +end |
| 96 | +``` |
| 97 | + |
| 98 | +### Cache.ConCache |
| 99 | + |
| 100 | +**Best for:** |
| 101 | +- Applications already using ConCache |
| 102 | +- Needs for automatic key expiration and callback execution |
| 103 | + |
| 104 | +**Configuration example:** |
| 105 | + |
| 106 | +```elixir |
| 107 | +defmodule MyApp.ConCache do |
| 108 | + use Cache, |
| 109 | + adapter: Cache.ConCache, |
| 110 | + name: :my_app_con_cache, |
| 111 | + opts: [ |
| 112 | + ttl_check_interval: :timer.seconds(1), |
| 113 | + global_ttl: :timer.minutes(10) |
| 114 | + ] |
| 115 | +end |
| 116 | +``` |
| 117 | + |
| 118 | +### Cache.Sandbox |
| 119 | + |
| 120 | +**Best for:** |
| 121 | +- Testing environments |
| 122 | +- Isolated tests that shouldn't interfere with each other |
| 123 | + |
| 124 | +**Configuration example:** |
| 125 | + |
| 126 | +```elixir |
| 127 | +defmodule MyApp.TestCache do |
| 128 | + use Cache, |
| 129 | + adapter: Cache.ETS, |
| 130 | + name: :my_app_test_cache, |
| 131 | + sandbox?: true |
| 132 | +end |
| 133 | +``` |
| 134 | + |
| 135 | +## Switching Between Adapters |
| 136 | + |
| 137 | +One of the main benefits of ElixirCache is the ability to easily switch between adapters without changing your application code. You can use different adapters in different environments: |
| 138 | + |
| 139 | +```elixir |
| 140 | +defmodule MyApp.Cache do |
| 141 | + use Cache, |
| 142 | + adapter: get_adapter(), |
| 143 | + name: :my_app_cache, |
| 144 | + opts: get_opts() |
| 145 | + |
| 146 | + defp get_adapter do |
| 147 | + case Mix.env() do |
| 148 | + :test -> Cache.Sandbox |
| 149 | + :dev -> Cache.ETS |
| 150 | + :prod -> Cache.Redis |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + defp get_opts do |
| 155 | + case Mix.env() do |
| 156 | + :test -> [] |
| 157 | + :dev -> [read_concurrency: true] |
| 158 | + :prod -> [ |
| 159 | + host: System.get_env("REDIS_HOST", "localhost"), |
| 160 | + port: String.to_integer(System.get_env("REDIS_PORT", "6379")), |
| 161 | + pool_size: 10 |
| 162 | + ] |
| 163 | + end |
| 164 | + end |
| 165 | +end |
| 166 | +``` |
| 167 | + |
| 168 | +## Performance Considerations |
| 169 | + |
| 170 | +When choosing an adapter, consider: |
| 171 | + |
| 172 | +1. **Access patterns** - How frequently are you reading vs writing? |
| 173 | +2. **Data volume** - How much data will be stored? |
| 174 | +3. **Persistence requirements** - Does the data need to survive restarts? |
| 175 | +4. **Distribution needs** - Will multiple nodes/services need access? |
| 176 | +5. **Complexity** - Do you need advanced features or simple key-value storage? |
| 177 | + |
| 178 | +Always benchmark different options with your specific workload to determine the best fit. |
0 commit comments