Skip to content

Commit e69b352

Browse files
Merge pull request #1620 from ilianiliev-redis/RDSC-3622-static-and-dynamic-ttl-examples
RDSC-3622 static and dynamic ttl examples
2 parents f64d236 + 9e77905 commit e69b352

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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 for 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+
You can use a JMESPath or SQL expression to set the expiration time dynamically when it 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+
## Dynamic expiration time based on a date, datetime, or timestamp field
53+
54+
In some cases, you can also set the expiration time based on a field that contains a date, datetime, or timestamp value, but it depends on the source database and the data types it supports. See the examples below for your specific source database and data type.
55+
56+
### Oracle examples
57+
58+
The transformation depends on the data type of the field in the source database:
59+
60+
- `DATE` - represented by debezium as a 64-bit integer representing the milliseconds since epoch
61+
```yaml
62+
output:
63+
- uses: redis.write
64+
with:
65+
data_type: hash
66+
expire:
67+
# To set the expiration time to a date field, convert the value to seconds and subtract the current time in seconds since epoch
68+
expression: EXPIRES_DATE / 1000 - STRFTIME('%s', 'now')
69+
language: sql
70+
```
71+
- `TIMESTAMP` - the value is represented by Debezium as a 64-bit integer and depends on the number of decimal places of precision of the column, representing fractions of a second. For example, if the column is defined as `TIMESTAMP(6)`, there are six decimal places and so the value is represented as microseconds since epoch (since there are 10^6 microseconds in each second).
72+
```yaml
73+
output:
74+
- uses: redis.write
75+
with:
76+
data_type: hash
77+
expire:
78+
# 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.
79+
expression: EXPIRES_TIMESTAMP / 1000000 - STRFTIME('%s', 'now')
80+
language: sql
81+
```
82+
- `TIMESTAMP WITH TIME ZONE` - the value is represented as string representation of the timestamp with time zone information.
83+
- `TIMESTAMP WITH LOCAL TIME ZONE` - the value is represented as string representation of the timestamp with local time zone information.
84+
85+
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.
86+
```yaml
87+
transform:
88+
- uses: add_field
89+
with:
90+
fields:
91+
- field: expire_seconds
92+
language: jmespath
93+
expression: time_delta_seconds(EXPIRES_TS_TZ)
94+
output:
95+
- uses: redis.write
96+
with:
97+
data_type: hash
98+
expire:
99+
# `time_delta_seconds` Returns the number of seconds between a given dt and now.
100+
# A negative value means that the given dt is in the future, so we need to invert it.
101+
# A positive value means that the given dt is in the past, so set the expiration to -1 (expire immediately).
102+
expression: CASE WHEN expire_seconds < 0 THEN -expire_seconds ELSE -1 END
103+
language: sql
104+
```
105+
106+
----
107+
108+
### SQL Server examples
109+
SQL Server supports the following date and time data types:
110+
111+
- `date` - represented in Debezium as number of days since epoch (1970-01-01). Please note that due to the lack of time information, this method is not very accurate.
112+
```yaml
113+
output:
114+
- uses: redis.write
115+
with:
116+
data_type: hash
117+
expire:
118+
# Calculate the number of seconds equivalent to the number of days and subtract the current time in seconds since epoch.
119+
expression: (event_date * 86400) - strftime('%s', 'now')
120+
language: sql
121+
```
122+
123+
- `datetime`, `smalldatetime` - represented in Debezium as number of milliseconds since epoch.
124+
```yaml
125+
output:
126+
- uses: redis.write
127+
with:
128+
data_type: hash
129+
expire:
130+
# Since event_datetime is in miliseconds, you must divide it by 1000 to convert it to seconds.
131+
expression: event_datetime / 1000 - strftime('%s', 'now')
132+
language: sql
133+
```
134+
- `datetime2` - similar to `datetime` but with higher precision. For `datetime2(0-3)` the representation is the same as for `datetime`. For `datetime2(4-6)` it is the number of microseconds since epoch. and for `datetime2(7)` it is the number of nanoseconds since epoch. You can use the same approach as for `datetime` but you need to divide by 1000, 1000000 or 1000000000 depending on the precision.
135+
136+
- `time` - the time of milliseconds since midnight.
137+
```yaml
138+
output:
139+
- uses: redis.write
140+
with:
141+
data_type: hash
142+
expire:
143+
# Convert the time to seconds and subtract the current time in seconds since midnight.
144+
expression: (event_time / 1000.0) -
145+
(
146+
CAST(strftime('%H', 'now') AS INTEGER) * 3600 +
147+
CAST(strftime('%M', 'now') AS INTEGER) * 60 +
148+
CAST(strftime('%S', 'now') AS INTEGER)
149+
)
150+
language: sql
151+
```
152+
- `datetimeoffset` - represented as a timestamp with timezone information, where the timezone is GMT
153+
```yaml
154+
output:
155+
- uses: redis.write
156+
with:
157+
data_type: hash
158+
expire:
159+
# Convert the time to seconds and subtract the current time in seconds since epoch.
160+
expression: strftime('%s', event_datetimeoffset) - strftime('%s', 'now')
161+
language: sql
162+
```
163+
164+
<!-- TODO [ilianiliev-redis]: Test and document the dynamic expressions for the rest of the supported databases - MySQL, PostgresSQL, MongoDB -->

0 commit comments

Comments
 (0)