Skip to content

Commit 5f19d9e

Browse files
Static and dynamic key expiration examples
1 parent 6e3bfe8 commit 5f19d9e

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
Title: Set custom expiration times / TTL
3+
alwaysopen: false
4+
categories:
5+
- docs
6+
- integrate
7+
- rs
8+
- rdi
9+
description: null
10+
group: di
11+
linkTitle: Set expiration times / TTL
12+
summary: How to set expiration times / TTL to keys
13+
type: integration
14+
weight: 40
15+
---
16+
17+
18+
You can configure custom key expiration times (TTL) for keys written to Redis by using the `expire` parameter in the `output` section of the job file. This parameter specifies the duration, in seconds, that a newly created key will remain in Redis before being automatically deleted. If the `expire` parameter is not provided, the keys will persist indefinitely.
19+
20+
There are two ways to set the expiration time:
21+
22+
- as a static value
23+
- as a dynamic value using a JMESPath or SQL expression
24+
25+
26+
## Static expiration time
27+
28+
The following example sets the expiration time to 100 seconds for all keys:
29+
30+
```yaml
31+
output:
32+
- uses: redis.write
33+
with:
34+
data_type: hash
35+
expire: 100
36+
```
37+
38+
## Dynamic expiration time
39+
40+
Settings the expiration time dynamically using a JMESPath or SQL expression is useful when the expiration time is based on a field in the source data. For example, you can set the expiration time to the value of a `ttl` field in the source data:
41+
42+
```yaml
43+
output:
44+
- uses: redis.write
45+
with:
46+
data_type: hash
47+
expire:
48+
expression: ttl
49+
language: jmespath
50+
```
51+
52+
Settings the expiration according to a field that contains a date, datetime, or timestamp value is also possible, but it depends on the source database and the data types it supports.
53+
54+
55+
### Oracle examples
56+
The transformation depends on the data type of the field in the source database:
57+
58+
- `DATE` - represented by debezium as 64-bit integer representing the milliseconds since epoch
59+
```yaml
60+
output:
61+
- uses: redis.write
62+
with:
63+
data_type: hash
64+
expire:
65+
# To set the expiration time to a date field, convert the value to seconds and subtract the current time in seconds since epoch
66+
expression: EXPIRES_DATE / 1000 - STRFTIME('%s', 'now')
67+
language: sql
68+
```
69+
- `TIMESTAMP` - the value is represented by debezium as 64-bit integer and depends on the fractional second precision of the column. For example, if the column is defined as `TIMESTAMP(6)`, the value is represented as microseconds since epoch.
70+
```yaml
71+
output:
72+
- uses: redis.write
73+
with:
74+
data_type: hash
75+
expire:
76+
# To set the expiration time to a date field, convert the value to seconds (divider differs based on the fractional second precision) and subtract the current time in seconds since epoch. Example below is for 6 digits of precision.
77+
expression: EXPIRES_TIMESTAMP / 1000000 - STRFTIME('%s', 'now')
78+
language: sql
79+
```
80+
- `TIMESTAMP WITH TIME ZONE` - the value is represented as string representation of the timestamp with time zone information.
81+
- `TIMESTAMP WITH LOCAL TIME ZONE` - the value is represented as string representation of the timestamp with local time zone information.
82+
83+
For both `TIMESTAMP WITH TIME ZONE` and `TIMESTAMP WITH LOCAL TIME ZONE`, a two-step approach is needed. First, calculate the difference between the given time and now in seconds and then invert the value.
84+
```yaml
85+
transform:
86+
- uses: add_field
87+
with:
88+
fields:
89+
- field: expire_seconds
90+
language: jmespath
91+
expression: time_delta_seconds(EXPIRES_TS_TZ)
92+
output:
93+
- uses: redis.write
94+
with:
95+
data_type: hash
96+
expire:
97+
# `time_delta_seconds` Returns the number of seconds between a given dt and now.
98+
# A negative value means that the given dt is in the future, so we need to invert it.
99+
# A positive value means that the given dt is in the past, so we set the expiration to -1 (expire immediately).
100+
expression: CASE WHEN expire_seconds < 0 THEN -expire_seconds ELSE -1 END
101+
language: sql
102+
```

0 commit comments

Comments
 (0)