Skip to content

Commit db02c58

Browse files
update python data stores
1 parent db0d0f9 commit db02c58

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

docs-v2/pages/workflows/building-workflows/code/python/using-data-stores.mdx

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,39 @@ def handler(pd: "pipedream"):
4444

4545
### Setting expiration (TTL) for records
4646

47-
<Callout type="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+
def handler(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)
55+
data_store.set("temporaryToken", "abc123", ttl=3600)
56+
57+
# Store a value that will expire after 1 day
58+
data_store.set("dailyMetric", 42, ttl=86400)
59+
```
60+
61+
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+
def handler(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)
5074

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.
5280

5381
## Retrieving keys
5482

@@ -293,6 +321,40 @@ def handler(pd: "pipedream"):
293321
return new_email
294322
```
295323

324+
## 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+
def handler(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+
if not 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
341+
return { "allowed": True, "remaining": 4 }
342+
343+
if requests_num >= 5:
344+
# Rate limit exceeded
345+
return { "allowed": False, "error": "Rate limit exceeded", "retry_after": "1 hour" }
346+
347+
# Increment the counter
348+
data_store["rate_key"] = requests_num + 1
349+
return { "allowed": True, "remaining": 4 - requests_num }
350+
```
351+
352+
This pattern can be extended for various temporary caching scenarios like:
353+
- Session tokens with automatic expiration
354+
- Short-lived feature flags
355+
- Temporary access grants
356+
- Time-based promotional codes
357+
296358
### Supported data types
297359

298360
Data stores can hold any JSON-serializable data within the storage limits. This includes data types including:

0 commit comments

Comments
 (0)