Cachetastic is a powerful and user-friendly caching library for Elixir. The goal is to provide a unified interface for various caching mechanisms like ETS and Redis, with built-in fault tolerance.
- Unified Interface: Interact with different caching backends through a consistent API.
- Hybrid Caching: Combine in-memory caching with persistent storage.
- Fault Tolerance: Automatically handle failures in the primary backend.
- Ecto Integration: Cache and retrieve Ecto query results seamlessly.
Add cachetastic
to your list of dependencies in mix.exs
:
def deps do
[
{:cachetastic, "~> 0.1.0"},
{:redix, "~> 1.0"},
]
end
Run mix deps.get
to fetch the dependencies.
Define the backends and fault tolerance configuration in config/config.exs
:
use Mix.Config
config :cachetastic,
backends: [
ets: [ttl: 600],
redis: [host: "localhost", port: 6379, ttl: 3600]
],
fault_tolerance: [primary: :redis, backup: :ets]
Initialize the cache and perform basic operations:
# Start the cache
{:ok, _pid} = Cachetastic.start_link()
# Cache operations
Cachetastic.put("key", "value")
{:ok, value} = Cachetastic.get("key")
Cachetastic.delete("key")
Cachetastic.clear()
You can use Cachetastic to cache and retrieve Ecto query results.
Update your mix.exs
file to include Cachetastic as a dependency:
defp deps do
[
{:cachetastic, "~> 0.1.0"}
]
end
Run mix deps.get
to fetch the dependencies.
Add the configuration for Cachetastic in your config/config.exs
file:
use Mix.Config
config :cachetastic,
backends: [
ets: [ttl: 600],
redis: [host: "localhost", port: 6379, ttl: 3600]
],
fault_tolerance: [primary: :redis, backup: :ets]
Add the Cachetastic plugin to your Ecto repo:
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
use Cachetastic.Ecto, repo: MyApp.Repo
end
Now you can use Cachetastic to cache and retrieve Ecto query results:
defmodule MyApp.SomeModule do
alias MyApp.Repo
alias MyApp.User
def some_function do
query = from u in User, where: u.active == true
# Fetch with cache
{:ok, users} = Repo.get_with_cache(query)
# Invalidate cache
Repo.invalidate_cache(query)
end
end
Here's an example of how you can integrate Cachetastic into your own Elixir application:
Update your mix.exs
file to include Cachetastic as a dependency:
defp deps do
[
{:cachetastic, "~> 0.1.0"}
]
end
Run mix deps.get
to fetch the dependencies.
Add the configuration for Cachetastic in your config/config.exs
file:
use Mix.Config
config :cachetastic,
backends: [
ets: [ttl: 600],
redis: [host: "localhost", port: 6379, ttl: 3600]
],
fault_tolerance: [primary: :redis, backup: :ets]
In your application module, start Cachetastic and use it to perform caching operations:
defmodule MyApp.Application do
use Application
def start(_type, _args) do
children = [
# Start the Cachetastic cache
{Cachetastic, []}
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
end
end
Now you can use Cachetastic to perform caching operations anywhere in your application:
defmodule MyApp.SomeModule do
def some_function do
# Put a value in the cache
Cachetastic.put("my_key", "my_value")
# Get a value from the cache
case Cachetastic.get("my_key") do
{:ok, value} -> IO.puts("Got value: #{value}")
:error -> IO.puts("Key not found")
end
# Delete a value from the cache
Cachetastic.delete("my_key")
# Clear the entire cache
Cachetastic.clear()
end
end
Feel free to open issues and pull requests. We appreciate your contributions!
This project is licensed under the MIT License.