Cachex - flexible and advanced caching
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.
Why Cachex?
- High-Speed Caching: Get instant access to data, making your application ultra-responsive.
- Versatile Storage: Beyond caching, use it as a general-purpose in-memory store.
- Tuned for You: All features are optional, enabling you to optimize for your specific use case.
Key Features
- Time-Based Expirations: Automatically invalidate keys after a set time.
- Size Limit: Control the maximum size of the cache to prevent overflow.
- Hooks: Use pre/post-execution hooks for customized workflows.
- Multi-Layered Caching: Implement key fallbacks for a more resilient system.
- Transactions: Perform safe read/write operations with row locking.
- Asynchronous Writes: Non-blocking write operations for better performance.
- Filesystem Sync: Optionally synchronize the cache with your local filesystem.
- Custom Commands: Invoke user-defined commands for specialized behaviors.
Quick Example
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!