Phoenix.ReactServer.Cache (Phoenix.ReactServer NG v0.8.4)

Copy Markdown View Source

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 name
  • props - Serialized component props
  • static_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 cache_key() :: {String.t(), map(), atom()}

Cache key type for component rendering

cache_method()

@type cache_method() ::
  :render_to_static_markup | :render_to_string | :render_to_readable_stream

Cache method type

cache_record()

@type cache_record() :: {cache_key(), String.t(), integer()}

Cache record stored in ETS

ttl()

@type ttl() :: non_neg_integer()

TTL in seconds

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

delete_cache(component, props, method)

@spec delete_cache(String.t(), map(), cache_method()) :: true

Removes a cached rendering result.

Parameters

  • component - Component name
  • props - Component props
  • method - 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

get(component, props, method)

@spec get(String.t(), map(), cache_method()) :: String.t() | nil

Retrieves a cached rendering result.

Parameters

  • component - Component name
  • props - Component props
  • method - Rendering method

Returns

  • binary() - Cached HTML if found and not expired
  • nil - No cached result found or expired

Example

iex> Phoenix.ReactServer.Cache.get("chart", %{"data" => [1,2,3]}, :render_to_string)
"<div>...</div>"

put(component, props, method, result, opt \\ [])

@spec put(String.t(), map(), cache_method(), String.t(), keyword()) :: true

Stores a rendering result in cache.

Parameters

  • component - Component name
  • props - Component props
  • method - Rendering method
  • result - HTML result to cache
  • opts - 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

start_link(_)