ps1811 commited on
Commit
aab34cc
·
verified ·
1 Parent(s): 6b96fd7

Updated session loader

Browse files
Files changed (1) hide show
  1. app/controller/session_loader.py +19 -9
app/controller/session_loader.py CHANGED
@@ -1,21 +1,31 @@
1
- from app.ads1.fetch_ads_data import fetch_all_data, to_dataframes
2
  import os
 
 
 
 
3
 
4
- _cached_dfs = None
 
5
 
6
  def load_google_ads_data(force_refresh=False):
7
- global _cached_dfs
8
-
9
  customer_id = os.getenv("GOOGLE_ADS_CUSTOMER_ID")
10
-
11
  if not customer_id:
12
  raise ValueError("GOOGLE_ADS_CUSTOMER_ID missing")
13
 
14
- if _cached_dfs is not None and not force_refresh:
15
- return _cached_dfs
 
 
 
 
 
16
 
 
17
  raw = fetch_all_data(customer_id)
18
  dfs = to_dataframes(raw)
19
 
20
- _cached_dfs = dfs
21
- return dfs
 
 
 
 
 
1
  import os
2
+ import time
3
+ import pickle
4
+ import pandas as pd
5
+ from app.ads1.fetch_ads_data import fetch_all_data, to_dataframes
6
 
7
+ CACHE_FILE = "/tmp/google_ads_cache.pkl"
8
+ CACHE_TTL = 3600 # Cache data for 1 hour (3600 seconds)
9
 
10
  def load_google_ads_data(force_refresh=False):
 
 
11
  customer_id = os.getenv("GOOGLE_ADS_CUSTOMER_ID")
 
12
  if not customer_id:
13
  raise ValueError("GOOGLE_ADS_CUSTOMER_ID missing")
14
 
15
+ # Check if a fresh disk cache exists
16
+ if not force_refresh and os.path.exists(CACHE_FILE):
17
+ file_mod_time = os.path.getmtime(CACHE_FILE)
18
+ if (time.time() - file_mod_time) < CACHE_TTL:
19
+ print("🚀 Loading data from local disk cache...")
20
+ with open(CACHE_FILE, "rb") as f:
21
+ return pickle.load(f)
22
 
23
+ print("🌐 Disk cache expired or missing. Fetching live Google Ads data...")
24
  raw = fetch_all_data(customer_id)
25
  dfs = to_dataframes(raw)
26
 
27
+ # Save to disk cache safely
28
+ with open(CACHE_FILE, "wb") as f:
29
+ pickle.dump(dfs, f)
30
+
31
+ return dfs