Introducing the Cachex feature, an in-memory key/value store designed to elevate your application's speed and efficiency. Built on top of ETS, this fast and versatile caching solution provides a range of options to fine-tune performance without sacrificing speed.
Incorporate Cachex into your application with just a few lines of Elixir code. Define caches by names and apply them using a simple helper function.
# Define a cache
{Cachex, name: :general_cache},
# Using Cachex in your application
defmodule Tutorial.CacheHelper do
@cache_name :general_cache
def with_cache(key, fun, ttl \\ 60 * 60) do
case Cachex.get(@cache_name, key) do
{:ok, nil} ->
result = fun.()
Cachex.put(@cache_name, key, result, ttl: ttl)
result
{:ok, result} ->
result
end
end
end
# Retrieve posts with caching
posts = with_cache("list_posts", fn -> Posts.list_posts() end)
Boost your application's performance and efficiency by integrating the Cachex feature today!