{
  "id": "caching-expression-results",
  "title": "Caching expression results",
  "url": "https://redis.io/docs/latest/integrate/redis-data-integration/data-pipelines/transform-examples/caching-expression-results/",
  "summary": "",
  "tags": [
    "docs",
    "integrate",
    "rs",
    "rdi"
  ],
  "last_updated": "2026-05-12T09:07:59-04:00",
  "page_type": "content",
  "content_hash": "4464a53d03d2783a05aab1ce52064f18663329a08765a58c4df8c59a16cfc91c",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "The Flink processor can cache the result of any expression that\nproduces a value (for example, an\n[`add_field`](https://redis.io/docs/latest/integrate/redis-data-integration/reference/data-transformation/add_field)\nexpression, a [`map`](https://redis.io/docs/latest/integrate/redis-data-integration/reference/data-transformation/map)\nexpression, the arguments to a\n[`redis.lookup`](https://redis.io/docs/latest/integrate/redis-data-integration/reference/data-transformation/lookup),\nor a custom output `key`/`expire` expression). Caching is useful when\nthe same expression is evaluated repeatedly with the same input field\nvalues, for example when many incoming records share a common foreign\nkey.\n\nCaching is supported only by the **Flink processor**. The\nclassic processor silently ignores `cache:` blocks."
    },
    {
      "id": "the-cache-block",
      "title": "The `cache:` block",
      "role": "content",
      "text": "You enable caching by adding a `cache:` block next to the expression\nyou want to cache. Cache keys are derived from the values of the input\nfields referenced by the expression, not from the full record. See\n[`cache`](https://redis.io/docs/latest/integrate/redis-data-integration/reference/data-transformation/cache)\nfor the full property list.\n\n| Property      | Type      | Description                                                    | Default |\n| ------------- | --------- | -------------------------------------------------------------- | ------- |\n| `enabled`     | `boolean` | Set to `true` to enable caching.                               | `false` |\n| `max_size`    | `integer` | Maximum number of entries kept in the cache. Must be positive. | `1000`  |\n| `ttl_seconds` | `integer` | Time-to-live for each entry, in seconds. Must be positive.     | `60`    |"
    },
    {
      "id": "caching-an-add-field-expression",
      "title": "Caching an `add_field` expression",
      "role": "content",
      "text": "The example below adds a `country` field whose value is derived from\n`country_code` and `country_name`. When the same combination of input\nvalues appears repeatedly (for example, many customers from the same\ncountry), caching the result avoids re-evaluating the expression.\n\n[code example]"
    },
    {
      "id": "caching-a-map-expression",
      "title": "Caching a `map` expression",
      "role": "content",
      "text": "[code example]"
    },
    {
      "id": "caching-redis-lookup-arguments-and-results",
      "title": "Caching `redis.lookup` arguments and results",
      "role": "content",
      "text": "`redis.lookup` supports two independent caches. The `cache:` block\ncaches the *argument* expressions (the JMESPath or SQL expressions\nthat produce the Redis command arguments). The `lookup_cache:` block\ncaches the *result* of the Redis command itself, keyed by the\nresolved arguments. Both blocks accept the same properties as the\n`cache:` block above.\n\n[code example]"
    },
    {
      "id": "caching-key-and-expire-output-expressions",
      "title": "Caching `key` and `expire` output expressions",
      "role": "content",
      "text": "A `cache:` block can also be added to the\n[output `key` and `expire` expressions](https://redis.io/docs/latest/integrate/redis-data-integration/data-pipelines/transform-examples/_index)\nwhen those are dynamic. The properties are the same as above.\n\n[code example]"
    }
  ],
  "examples": [
    {
      "id": "caching-an-add-field-expression-ex0",
      "language": "yaml",
      "code": "name: Cached country field\nsource:\n  schema: dbo\n  table: customer\ntransform:\n  - uses: add_field\n    with:\n      field: country\n      language: sql\n      expression: country_code || ' - ' || UPPER(country_name)\n      cache:\n        enabled: true\n        max_size: 500\n        ttl_seconds: 300",
      "section_id": "caching-an-add-field-expression"
    },
    {
      "id": "caching-a-map-expression-ex0",
      "language": "yaml",
      "code": "name: Cached map expression\nsource:\n  table: customer\ntransform:\n  - uses: map\n    with:\n      language: jmespath\n      expression: |\n        {\n          \"CustomerId\": customer_id,\n          \"Country\": country_code\n        }\n      cache:\n        enabled: true",
      "section_id": "caching-a-map-expression"
    },
    {
      "id": "caching-redis-lookup-arguments-and-results-ex0",
      "language": "yaml",
      "code": "name: Cached lookup\nsource:\n  table: order\ntransform:\n  - uses: redis.lookup\n    with:\n      connection: target\n      cmd: HGETALL\n      args:\n        - concat(['customer:', customer_id])\n      language: jmespath\n      field: customer\n      cache:\n        enabled: true\n        ttl_seconds: 60\n      lookup_cache:\n        enabled: true\n        max_size: 10000\n        ttl_seconds: 300",
      "section_id": "caching-redis-lookup-arguments-and-results"
    },
    {
      "id": "caching-key-and-expire-output-expressions-ex0",
      "language": "yaml",
      "code": "name: Cached key expression\nsource:\n  table: order\noutput:\n  - uses: redis.write\n    with:\n      data_type: hash\n      key:\n        expression: concat(['order:', order_id])\n        language: jmespath\n        cache:\n          enabled: true",
      "section_id": "caching-key-and-expire-output-expressions"
    }
  ]
}
