High-performance ETS-based caching for React component rendering.
This module provides intelligent caching for rendered React components with configurable TTL and automatic cleanup of expired entries.
Features
- ETS-based Storage: In-memory caching with O(1) access time
- Configurable TTL: Time-to-live per cache entry (default: 3600 seconds)
- Automatic Cleanup: Garbage collection runs every 60 seconds
- Smart Cache Keys: Based on component name, props, and static flag
- Memory Efficient: Automatic cleanup prevents memory leaks
Cache Key Structure
Cache keys are tuples: {component, props, static_flag}
component- The React component nameprops- Serialized component propsstatic_flag- Whether rendering is static (affects caching strategy)
Configuration
config :phoenix_react_ng, Phoenix.ReactServer,
cache_ttl: 3600 # Cache TTL in seconds (default: 1 hour)Set cache_ttl: 0 to disable caching entirely.
Performance
- Cache hit: O(1) lookup time
- Cache miss: Triggers component rendering and stores result
- Memory usage: Proportional to number of cached components
- Cleanup cost: O(n) where n is number of expired entries
Summary
Types
Cache key type for component rendering
Cache method type
Cache record stored in ETS
TTL in seconds
Functions
Returns a specification to start this module under a supervisor.
Removes a cached rendering result.
Retrieves a cached rendering result.
Stores a rendering result in cache.
Types
Cache key type for component rendering
@type cache_method() ::
:render_to_static_markup | :render_to_string | :render_to_readable_stream
Cache method type
Cache record stored in ETS
@type ttl() :: non_neg_integer()
TTL in seconds
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec delete_cache(String.t(), map(), cache_method()) :: true
Removes a cached rendering result.
Parameters
component- Component nameprops- Component propsmethod- Rendering method
Returns
true- Entry removed (or didn't exist)
Example
iex> Phoenix.ReactServer.Cache.delete_cache("chart", %{"data" => [1,2,3]}, :render_to_string)
true
@spec get(String.t(), map(), cache_method()) :: String.t() | nil
Retrieves a cached rendering result.
Parameters
component- Component nameprops- Component propsmethod- Rendering method
Returns
binary()- Cached HTML if found and not expirednil- No cached result found or expired
Example
iex> Phoenix.ReactServer.Cache.get("chart", %{"data" => [1,2,3]}, :render_to_string)
"<div>...</div>"
@spec put(String.t(), map(), cache_method(), String.t(), keyword()) :: true
Stores a rendering result in cache.
Parameters
component- Component nameprops- Component propsmethod- Rendering methodresult- HTML result to cacheopts- Options including:ttl(default: configured cache TTL)
Returns
true- Successfully cached
Example
iex> Phoenix.ReactServer.Cache.put("chart", %{"data" => [1,2,3]}, :render_to_string, "<div>...</div>", ttl: 300)
true