1010# Local
1111from webapp .decorators import login_required , exchange_required
1212from webapp .helpers import api_publisher_session , get_brand_id
13+ from cache .cache_utility import redis_cache
1314
1415
1516publisher_gateway = PublisherGW ("snap" , api_publisher_session )
2021)
2122
2223
24+ def get_models_cache_key (store_id : str ) -> str :
25+ return f"models:{ store_id } "
26+
27+
28+ def get_policies_cache_key (store_id : str , model_name : str ) -> str :
29+ return f"policies:{ store_id } :{ model_name } "
30+
31+
2332@models .route ("/api/store/<store_id>/models" )
2433@login_required
2534@exchange_required
26- def get_models (store_id ):
35+ def get_models (store_id : str ):
2736 """
2837 Retrieves models associated with a given store ID.
2938
@@ -34,9 +43,18 @@ def get_models(store_id):
3443 dict: A dictionary containing the response message, success status,
3544 and data.
3645 """
46+
3747 res = {}
3848 try :
39- models = publisher_gateway .get_store_models (flask .session , store_id )
49+ cached_models_key = get_models_cache_key (store_id )
50+ cached_models = redis_cache .get (cached_models_key , expected_type = list )
51+ if cached_models is not None :
52+ models = cached_models
53+ else :
54+ models = publisher_gateway .get_store_models (
55+ flask .session , store_id
56+ )
57+ redis_cache .set (cached_models_key , models , ttl = 3600 )
4058 res ["success" ] = True
4159 res ["data" ] = models
4260 response = make_response (res , 200 )
@@ -74,9 +92,17 @@ def get_policies(store_id: str, model_name: str):
7492 res = {}
7593
7694 try :
77- policies = publisher_gateway .get_store_model_policies (
78- flask .session , brand_id , model_name
95+ cached_policies_key = get_policies_cache_key (store_id , model_name )
96+ cached_policies = redis_cache .get (
97+ cached_policies_key , expected_type = list
7998 )
99+ if cached_policies is not None :
100+ policies = cached_policies
101+ else :
102+ policies = publisher_gateway .get_store_model_policies (
103+ flask .session , brand_id , model_name
104+ )
105+ redis_cache .set (cached_policies_key , policies , ttl = 3600 )
80106 res ["success" ] = True
81107 res ["data" ] = policies
82108 response = make_response (res , 200 )
@@ -131,6 +157,8 @@ def create_policy(store_id: str, model_name: str):
131157 flask .session , store_id , model_name , signing_key
132158 )
133159 res ["success" ] = True
160+ cached_policies_key = get_policies_cache_key (store_id , model_name )
161+ redis_cache .delete (cached_policies_key )
134162 else :
135163 res ["message" ] = "Invalid signing key"
136164 res ["success" ] = False
@@ -157,6 +185,8 @@ def delete_policy(store_id: str, model_name: str, revision: str):
157185 )
158186 if response .status_code == 204 :
159187 res = {"success" : True }
188+ cached_policies_key = get_policies_cache_key (store_id , model_name )
189+ redis_cache .delete (cached_policies_key )
160190 return make_response (res , 200 )
161191 elif response .status_code == 404 :
162192 res = {"success" : False , "message" : "Policy not found" }
@@ -203,8 +233,10 @@ def create_models(store_id: str):
203233 publisher_gateway .create_store_model (
204234 flask .session , store_id , name , api_key
205235 )
206- res ["success" ] = True
236+ cached_models_key = get_models_cache_key (store_id )
237+ redis_cache .delete (cached_models_key )
207238
239+ res ["success" ] = True
208240 return make_response (res , 201 )
209241 except StoreApiResponseErrorList as error_list :
210242 res ["success" ] = False
@@ -251,6 +283,9 @@ def update_model(store_id: str, model_name: str):
251283 )
252284 res ["success" ] = True
253285
286+ cached_models_key = get_models_cache_key (store_id )
287+ redis_cache .delete (cached_models_key )
288+
254289 except StoreApiResponseErrorList as error_list :
255290 res ["success" ] = False
256291 res ["message" ] = error_list .errors [0 ]["message" ]
0 commit comments