PhoenixKitOG.Render.Cache (PhoenixKitOG v0.1.0)

Copy Markdown View Source

On-disk cache for rendered OG PNGs.

Cache key = SHA-256 of (template_uuid, template_updated_at, canvas, binding_values). Two different posts with the same resolved values share a single file; editing the template invalidates every dependent cache entry automatically (the template's updated_at is part of the key).

Files live under priv/static/og_cache/ so we can reuse the host's static-file plug without registering a new endpoint. Filename pattern:

<16-hex hash>.png

The full hash is 64 hex; we truncate to 16 (64 bits) which is collision-resistant for our scale (millions of templates × posts).

Summary

Functions

Clears every cached render — useful after upgrading the renderer.

Returns {cache_key, absolute_path}. The path may or may not exist — call exists?/1 or read/1 to check.

Writes the PNG bytes to the cache file. Atomic: write to a tempfile in the same dir then rename, so a concurrent read never sees a partial file.

Functions

clear()

@spec clear() :: :ok

Clears every cached render — useful after upgrading the renderer.

exists?(key)

@spec exists?(String.t()) :: boolean()

key_and_path(template, context)

@spec key_and_path(map(), map()) :: {String.t(), String.t()}

Returns {cache_key, absolute_path}. The path may or may not exist — call exists?/1 or read/1 to check.

read(key)

@spec read(String.t()) :: {:ok, binary()} | {:error, term()}

write(key, png_bytes)

@spec write(String.t(), binary()) :: :ok | {:error, term()}

Writes the PNG bytes to the cache file. Atomic: write to a tempfile in the same dir then rename, so a concurrent read never sees a partial file.