CACHED
Enable server-side response caching for scalar results.
Keywords
cached
Syntax
cached
cached <param1> [param2] [param3] ...Parameters specified become part of the cache key.
Examples
Simple Caching
sql
create function get_app_settings()
returns json
language sql
as $$select settings from app_config where id = 1$$;
comment on function get_app_settings() is
'HTTP GET
cached';Cache Key by Parameter
sql
create function get_user_profile(_user_id int)
returns json
language sql
as $$select row_to_json(u) from users u where id = _user_id$$;
comment on function get_user_profile(int) is
'HTTP GET
cached _user_id';Different _user_id values create separate cache entries.
Multiple Cache Key Parameters
sql
create function get_report(_year int, _department text)
returns json
language sql
as $$...$$;
comment on function get_report(int, text) is
'HTTP GET
cached _year _department';With Cache Expiration
sql
comment on function get_config() is
'HTTP GET
cached
cache_expires_in 1h';Behavior
- Caches the response for subsequent identical requests
- Only works with scalar (single-value) results
- Cache key is based on specified parameters
- Use with
cache_expires_into set expiration time
Cache Configuration
The cached annotation requires cache to be enabled in Cache Options configuration.
Two cache types are available:
| Type | Description | Use Case |
|---|---|---|
Memory | In-memory cache on the application server | Single instance deployments, development |
Redis | Distributed cache using Redis | Multi-instance deployments, production |
Example configuration:
json
{
"CacheOptions": {
"Enabled": true,
"Type": "Memory"
}
}For Redis:
json
{
"CacheOptions": {
"Enabled": true,
"Type": "Redis",
"RedisConfiguration": "localhost:6379"
}
}See Cache Options for complete configuration reference.
Related
- Cache Options configuration - Configure cache backend (Memory or Redis)
- Comment Annotations Guide - How annotations work
- Configuration Guide - How configuration works
Related Annotations
- CACHE_EXPIRES_IN - Set expiration time