Skip to content

Commit 4eae44b

Browse files
chekosclaude
andcommitted
Fix cache_table to read from cache before API call in estimates/flows
The cache_table parameter was write-only — it stored results but never checked for cached data before making API calls. Also adds variables, breakdown, and year to cache keys to prevent collisions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 172490e commit 4eae44b

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

pypums/estimates.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ def get_estimates(
131131
if year is not None:
132132
params["YEAR"] = str(year)
133133

134+
# Build cache key from all parameters that affect the result.
135+
vars_str = ",".join(variables) if variables is not None else ""
136+
breakdown_str = ",".join(breakdown) if breakdown is not None else ""
137+
cache_key = (
138+
f"est_{vintage}_{resolved_product}_{geography}_{state}_{county}"
139+
f"_{year}_{vars_str}_{breakdown_str}"
140+
)
141+
142+
# Check cache before calling API.
143+
if cache_table:
144+
disk_cache = CensusCache(_DEFAULT_CACHE_DIR)
145+
cached = disk_cache.get(cache_key)
146+
if cached is not None:
147+
return cached
148+
134149
if show_call:
135150
print(f"Census API call: {url}")
136151
print(f" Parameters: {params}")
@@ -158,7 +173,6 @@ def get_estimates(
158173
df = attach_geometry(df, geography, state=state, year=vintage)
159174

160175
if cache_table:
161-
cache_key = f"est_{vintage}_{resolved_product}_{geography}_{state}_{county}"
162176
disk_cache = CensusCache(_DEFAULT_CACHE_DIR)
163177
disk_cache.set(cache_key, df, ttl_seconds=86400)
164178

pypums/flows.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ def get_flows(
141141
for dim in breakdown:
142142
params[dim] = "*"
143143

144+
# Build cache key from all parameters that affect the result.
145+
vars_str = ",".join(variables) if variables is not None else ""
146+
breakdown_str = ",".join(breakdown) if breakdown is not None else ""
147+
cache_key = (
148+
f"flows_{year}_{geography}_{state}_{county}_{msa}"
149+
f"_{vars_str}_{breakdown_str}"
150+
)
151+
152+
# Check cache before calling API.
153+
if cache_table:
154+
disk_cache = CensusCache(_DEFAULT_CACHE_DIR)
155+
cached = disk_cache.get(cache_key)
156+
if cached is not None:
157+
return cached
158+
144159
if show_call:
145160
print(f"Census API call: {url}")
146161
print(f" Parameters: {params}")
@@ -157,7 +172,6 @@ def get_flows(
157172
df[col] = pd.to_numeric(df[col], errors="coerce")
158173

159174
if cache_table:
160-
cache_key = f"flows_{year}_{geography}_{state}_{county}_{msa}"
161175
disk_cache = CensusCache(_DEFAULT_CACHE_DIR)
162176
disk_cache.set(cache_key, df, ttl_seconds=86400)
163177

0 commit comments

Comments
 (0)