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/index.md
+86Lines changed: 86 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2392,6 +2392,92 @@ For nested models, Secret Manager supports the `env_nested_delimiter` setting as
2392
2392
2393
2393
For more details on creating and managing secrets in Google Cloud Secret Manager, see the [official Google Cloud documentation](https://cloud.google.com/secret-manager/docs).
2394
2394
2395
+
### Lazy Loading
2396
+
2397
+
Lazy loading defers field value resolution until fields are actually accessed, rather than eagerly fetching all values during settings initialization. This is particularly useful when working with Google Cloud Secret Manager where each field access triggers an API call, avoiding unnecessary network requests for fields that may never be used.
2398
+
2399
+
2400
+
#### Basic Usage
2401
+
2402
+
You can enable lazy loading for Google Cloud Secret Manager via the `lazy_load` parameter when configuring `GoogleSecretManagerSettingsSource`:
logger.info('Settings initialized with lazy loading enabled')
2448
+
# Only secret1 is fetched from GCP Secret Manager
2449
+
s1 = settings.secret1
2450
+
2451
+
# secret1 is already cached, so this doesn't trigger another API call
2452
+
s1_copy = settings.secret1
2453
+
2454
+
# secret2 is fetched on first access, triggering an API CALL
2455
+
s2 = settings.secret2
2456
+
2457
+
# This triggers fetching of all remaining fields not yet accessed
2458
+
all_values = settings.model_dump()
2459
+
```
2460
+
2461
+
#### Behavior and Caching
2462
+
2463
+
When lazy loading is enabled:
2464
+
2465
+
1.**Initialization**: Settings are created with minimal overhead. Sources return empty dictionaries instead of eagerly fetching all values.
2466
+
2467
+
2.**First Access**: When you access a field for the first time (e.g., `settings.api_key`), the value is fetched from the configured source and cached in memory.
2468
+
2469
+
3.**Subsequent Access**: Accessing the same field again returns the cached value without making another API call.
2470
+
2471
+
4.**All Fields**: Iteration over all fields (via `model_dump()`, etc.) will trigger resolution of all fields at once.
2472
+
2473
+
***When to use lazy loading:**
2474
+
2475
+
* Your settings have many fields but your application only uses a subset of them
2476
+
* You want to reduce initialization time and API call costs
2477
+
* Network latency to GCP Secret Manager is significant
2478
+
2479
+
2480
+
2395
2481
## Other settings source
2396
2482
2397
2483
Other settings sources are available for common configuration files:
0 commit comments