Skip to content

Commit ee1f85b

Browse files
committed
fix(influxdb3): Restore best practices for caching section
1 parent f79c1c2 commit ee1f85b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

content/shared/v3-core-plugins/_index.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,48 @@ influxdb3_local.cache.put("execution_count", counter)
573573
influxdb3_local.info(f"This plugin has run {counter} times")
574574
```
575575

576+
#### Best practices for in-memory caching
577+
578+
- [Use the trigger-specific namespace](#use-the-trigger-specific-namespace)
579+
- [Use TTL appropriately](#use-ttl-appropriately)
580+
- [Cache computation results](#cache-computation-results)
581+
- [Warm the cache](#warm-the-cache)
582+
- [Consider cache limitations](#consider-cache-limitations)
583+
584+
##### Use the trigger-specific namespace
585+
586+
The cache is designed to support stateful operations while maintaining isolation between different triggers. Use the trigger-specific namespace for most operations and the global namespace only when data sharing across triggers is necessary.
587+
588+
##### Use TTL appropriately
589+
Set realistic expiration times based on how frequently data changes.
590+
591+
```python
592+
# Cache external API responses for 5 minutes
593+
influxdb3_local.cache.put("weather_data", api_response, ttl=300)
594+
```
595+
596+
##### Cache computation results
597+
Store the results of expensive calculations that need to be utilized frequently.
598+
```python
599+
# Cache aggregated statistics
600+
influxdb3_local.cache.put("daily_stats", calculate_statistics(data), ttl=3600)
601+
```
602+
603+
##### Warm the cache
604+
For critical data, prime the cache at startup. This can be especially useful for global namespace data where multiple triggers need the data.
605+
606+
```python
607+
# Check if cache needs to be initialized
608+
if not influxdb3_local.cache.get("lookup_table"):
609+
influxdb3_local.cache.put("lookup_table", load_lookup_data())
610+
```
611+
612+
##### Consider cache limitations
613+
614+
- **Memory Usage**: Since cache contents are stored in memory, monitor your memory usage when caching large datasets.
615+
- **Server Restarts**: Because the cache is cleared when the server restarts, design your plugins to handle cache initialization (as noted above).
616+
- **Concurrency**: Be cautious of accessing inaccurate or out-of-date data when multiple trigger instances might simultaneously update the same cache key.
617+
576618
## Install Python dependencies
577619

578620
If your plugin needs additional Python packages, use the `influxdb3 install` command:

0 commit comments

Comments
 (0)