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
Add documentation for Data Store TTL functionality (#15825)
* Add documentation for Data Store TTL functionality
* Update pnpm-lock.yaml
* update python data stores
* change data to dataStore
* remove duplicate limitation
---------
Co-authored-by: Andrew Chuang <[email protected]>
@@ -6,23 +6,23 @@ In Node.js (Javascript) code steps, you can also store and retrieve data within
6
6
7
7
Add data stores to steps as props. By adding the store as a prop, it's available under `this`.
8
8
9
-
For example, you can define a data store as a data prop, and reference it at `this.data`:
9
+
For example, you can define a data store as a dataStore prop, and reference it at `this.dataStore`:
10
10
11
11
```javascript
12
12
exportdefaultdefineComponent({
13
13
props: {
14
-
// Define that the "data" variable in our component is a data store
15
-
data: { type:"data_store" },
14
+
// Define that the "dataStore" variable in our component is a data store
15
+
dataStore: { type:"data_store" },
16
16
},
17
17
asyncrun({ steps, $ }) {
18
-
// Now we can access the data store at "this.data"
19
-
awaitthis.data.get("email");
18
+
// Now we can access the data store at "this.dataStore"
19
+
awaitthis.dataStore.get("email");
20
20
},
21
21
});
22
22
```
23
23
24
24
<Callouttype="info">
25
-
**`props` injects variables into `this`**. See how we declared the `data` prop in the `props` object, and then accessed it at `this.data` in the `run` method.
25
+
**`props` injects variables into `this`**. See how we declared the `dataStore` prop in the `props` object, and then accessed it at `this.dataStore` in the `run` method.
26
26
</Callout>
27
27
28
28
<Callouttype="warning">
@@ -37,32 +37,74 @@ Once you've defined a data store prop for your component, then you'll be able to
37
37
38
38
## Saving data
39
39
40
-
Data Stores are key-value stores. Save data within a Data Store using the `this.data.set` method. The first argument is the _key_ where the data should be held, and the second argument is the _value_ assigned to that key.
40
+
Data Stores are key-value stores. Save data within a Data Store using the `this.dataStore.set` method. The first argument is the _key_ where the data should be held, and the second argument is the _value_ assigned to that key.
41
41
42
42
```javascript
43
43
exportdefaultdefineComponent({
44
44
props: {
45
-
data: { type:"data_store" },
45
+
dataStore: { type:"data_store" },
46
46
},
47
47
asyncrun({ steps, $ }) {
48
48
// Store a timestamp each time this step is executed in the workflow
49
-
awaitthis.data.set("lastRanAt", newDate());
49
+
awaitthis.dataStore.set("lastRanAt", newDate());
50
50
},
51
51
});
52
52
```
53
53
54
+
### Setting expiration (TTL) for records
55
+
56
+
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:
57
+
58
+
```javascript
59
+
exportdefaultdefineComponent({
60
+
props: {
61
+
dataStore: { type:"data_store" },
62
+
},
63
+
asyncrun({ steps, $ }) {
64
+
// Store a temporary value that will expire after 1 hour (3600 seconds)
## TTL use case: temporary caching and rate limiting
288
+
289
+
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:
290
+
291
+
```javascript
292
+
exportdefaultdefineComponent({
293
+
props: {
294
+
dataStore: { type:"data_store" },
295
+
},
296
+
asyncrun({ steps, $ }) {
297
+
constuserId=steps.trigger.event.userId;
298
+
constrateKey=`ratelimit:${userId}`;
299
+
300
+
// Try to get current rate limit counter
301
+
let requests =awaitthis.dataStore.get(rateKey);
302
+
303
+
if (requests ===undefined) {
304
+
// First request from this user in the time window
305
+
awaitthis.dataStore.set(rateKey, 1, { ttl:3600 }); // Expire after 1 hour
Data Stores are only currently available in Node.js and Python steps. They are not yet available [Bash](/workflows/building-workflows/code/bash/) or [Go](/workflows/building-workflows/code/go/).
321
+
This pattern can be extended for various temporary caching scenarios like:
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)
## 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