Skip to content

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_in to set expiration time

Cache Configuration

The cached annotation requires cache to be enabled in Cache Options configuration.

Two cache types are available:

TypeDescriptionUse Case
MemoryIn-memory cache on the application serverSingle instance deployments, development
RedisDistributed cache using RedisMulti-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.

Released under the MIT License.