LemonCore.Idempotency (lemon_core v0.1.0)

View Source

Idempotency service for deduplicating operations.

This module provides a mechanism to ensure operations like send, agent, and node.invoke are executed at-most-once by tracking operation keys.

Storage

Backed by LemonCore.IdempotencyStore. Values include {result, inserted_at_ms}.

Examples

# Check for existing result
case LemonCore.Idempotency.get("messages", "msg_abc123") do
  {:ok, result} ->
    # Return cached result
    result
  :miss ->
    # Execute operation and store result
    result = execute_operation()
    LemonCore.Idempotency.put("messages", "msg_abc123", result)
    result
end

Summary

Functions

Delete an idempotency entry.

Execute a function with idempotency guarantees.

Get a cached result for a scope and key.

Store a result for a scope and key.

Store a result only if the key doesn't already exist.

Types

key()

@type key() :: binary()

result()

@type result() :: term()

scope()

@type scope() :: binary()

Functions

delete(scope, key)

@spec delete(scope(), key()) :: :ok

Delete an idempotency entry.

execute(scope, key, fun)

@spec execute(scope(), key(), (-> result())) :: result()

Execute a function with idempotency guarantees.

If the key exists, returns the cached result. Otherwise, executes the function, caches the result, and returns it.

get(scope, key)

@spec get(scope(), key()) :: {:ok, result()} | :miss

Get a cached result for a scope and key.

Returns {:ok, result} if found, or :miss if not found.

put(scope, key, result)

@spec put(scope(), key(), result()) :: :ok

Store a result for a scope and key.

Always overwrites any existing value.

put_new(scope, key, result)

@spec put_new(scope(), key(), result()) :: :ok | :exists

Store a result only if the key doesn't already exist.

Returns :ok if stored, or :exists if the key already exists.