Search documentation of phoenix_kit

# PhoenixKit.Sitemap.Cache (phoenix_kit v1.6.19)

View Source

ETS-based caching layer for sitemap generation.

Provides fast in-memory caching for generated XML and HTML sitemaps to avoid regenerating them on every request. Cache can be invalidated when content changes.

## Usage

    # Initialize cache table (usually in application startup)
    Cache.init()

    # Store generated sitemap
    Cache.put(:xml, xml_content)
    Cache.put(:html, html_content)

    # Retrieve cached sitemap
    case Cache.get(:xml) do
      {:ok, content} -> content
      :error -> generate_new_sitemap()
    end

    # Clear cache when content changes
    Cache.invalidate()

## Cache Keys

- `:xml` - Full XML sitemap
- `:html` - HTML sitemap
- `:parts` - List of sitemap part files for sitemap index

# Summary

## Functions

delete(key)

Deletes a specific cache entry.

get(key)

Retrieves a cached value by key.

has?(key)

Checks if a cached value exists for the given key.

init()

Initializes the ETS cache table.

invalidate()

Clears all cached data.

put(key, value)

Stores a value in cache with the given key.

stats()

Returns cache statistics.

# Functions

# delete(key)

    @spec delete(atom()) :: :ok

Deletes a specific cache entry.

## Examples

    iex> Cache.delete(:xml)
    :ok

# get(key)

    @spec get(atom()) :: {:ok, any()} | :error

Retrieves a cached value by key.

## Examples

    iex> Cache.get(:xml)
    {:ok, "<?xml version=..."}

    iex> Cache.get(:nonexistent)
    :error

# has?(key)

    @spec has?(atom()) :: boolean()

Checks if a cached value exists for the given key.

## Examples

    iex> Cache.has?(:xml)
    true

    iex> Cache.has?(:nonexistent)
    false

# init()

    @spec init() :: :ok

Initializes the ETS cache table.

Creates a public named table with read concurrency for optimal performance. Safe to call multiple times - returns :ok if table already exists.

## Examples

    iex> Cache.init()
    :ok

    iex> Cache.init()
    :ok  # Idempotent

# invalidate()

    @spec invalidate() :: :ok

Clears all cached data.

Should be called when sitemap content changes (new pages, updated content, etc).

## Examples

    iex> Cache.invalidate()
    :ok

# put(key, value)

    @spec put(atom(), any()) :: :ok

Stores a value in cache with the given key.

## Examples

    iex> Cache.put(:xml, xml_content)
    :ok

    iex> Cache.put(:html, html_content)
    :ok

# stats()

    @spec stats() :: map()

Returns cache statistics.

## Examples

    iex> Cache.stats()
    %{size: 2, memory: 1024}