You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs-v2/pages/workflows/building-workflows/code/python/using-data-stores.mdx
+66-4Lines changed: 66 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,11 +44,39 @@ def handler(pd: "pipedream"):
44
44
45
45
### Setting expiration (TTL) for records
46
46
47
-
<Callouttype="info">
48
-
TTL functionality is currently only available in Node.js code steps. If you need to set expiration on records from Python steps, you'll need to use a Node.js step to set or update the TTL.
49
-
</Callout>
47
+
You can set an expiration time for a record by passing a TTL (Time-To-Live) option as the third argument to the `set` method. The TTL value is specified in seconds:
48
+
49
+
```python
50
+
defhandler(pd: "pipedream"):
51
+
# Access the data store under the pd.inputs
52
+
data_store = pd.inputs["data_store"]
53
+
54
+
# Store a temporary value that will expire after 1 hour (3600 seconds)
When the TTL period elapses, the record will be automatically deleted from the data store.
62
+
63
+
### Updating TTL for existing records
64
+
65
+
You can update the TTL for an existing record using the `set_ttl` method:
66
+
67
+
```python
68
+
defhandler(pd: "pipedream"):
69
+
# Access the data store under the pd.inputs
70
+
data_store = pd.inputs["data_store"]
71
+
72
+
# Update an existing record to expire after 30 minutes
73
+
data_store.set_ttl("temporaryToken", ttl=1800)
50
74
51
-
For temporary data that should automatically expire, you can use a Node.js step with TTL functionality. See the [Node.js documentation](/workflows/building-workflows/code/nodejs/using-data-stores/#setting-expiration-ttl-for-records) for details on how to use TTL with data stores.
75
+
# Remove expiration from a record
76
+
data_store.set_ttl("temporaryToken", ttl=None)
77
+
```
78
+
79
+
This is useful for extending the lifetime of temporary data or removing expiration from records that should now be permanent.
## TTL use case: temporary caching and rate limiting
325
+
326
+
TTL functionality is particularly useful for implementing temporary caching and rate limiting. Here's an example of a simple rate limiter that prevents a user from making more than 5 requests per hour:
327
+
328
+
```python
329
+
defhandler(pd: "pipedream"):
330
+
# Access the data store
331
+
data_store = pd.inputs["data_store"]
332
+
user_id = pd.steps["trigger"]["event"]["user_id"]
333
+
rate_key =f"ratelimit:{user_id}"
334
+
335
+
# Try to get current rate limit counter
336
+
requests_num = data_store.get("rate_key")
337
+
338
+
ifnot requests_num:
339
+
# First request from this user in the time window
340
+
data_store.set(rate_key, 1, ttl=3600) # Expire after 1 hour
0 commit comments