endpoint "GET /api/v1/products/{sku}" {
pipeline {
redis "cache_lookup" {
connection = connection.redis.cache
command = "GET"
key = "cache:product:${ctx.request.path.sku}"
}
respond {
condition = steps.cache_lookup.value != null
status = 200
headers = { "X-Cache" = "HIT" }
body = json_decode(steps.cache_lookup.value)
}
sql "db_query" {
connection = connection.postgres.main
query = "SELECT id, sku, name, price FROM products WHERE sku = @sku"
args = { sku = ctx.request.path.sku }
}
respond {
condition = steps.db_query.rows_affected == 0
status = 404
body = { error = "Product not found" }
}
redis "cache_write" {
connection = connection.redis.cache
command = "SET"
key = "cache:product:${ctx.request.path.sku}"
value = json_encode(steps.db_query.row)
ttl = "30m"
}
respond {
status = 200
headers = { "X-Cache" = "MISS" }
body = steps.db_query.row
}
}
}