Working with async databases

Config caching and how to get the config object

Want more?

This lesson for enrolled students only. Join the course to unlock it!

You can see the code changes implemented in this lecture below.

If you have purchased the course in a different platform, you still have access to the code changes per lecture here on Teclado. The lecture video and lecture notes remain locked.
Join course for $30

Modified files

storeapi/config.py
--- 
+++ 
@@ -1,5 +1,6 @@
 # Most of this taken from Redowan Delowar's post on configurations with Pydantic
 # https://rednafi.github.io/digressions/python/2020/06/03/python-configs.html
+from functools import lru_cache
 from typing import Optional

 from pydantic_settings import BaseSettings, SettingsConfigDict
@@ -31,3 +32,13 @@
     DB_FORCE_ROLL_BACK: bool = True

     model_config = SettingsConfigDict(env_prefix="TEST_")
+
+
+@lru_cache()
+def get_config(env_state):
+    """Instantiate config based on the environment."""
+    configs = {"dev": DevConfig, "prod": ProdConfig, "test": TestConfig}
+    return configs[env_state]()
+
+
+config = get_config(BaseConfig().ENV_STATE)