Skip to content

Commit 4ed4c97

Browse files
committed
Reduce item limits and optimize variable search caching
Decreased the limit for listed items from 50 to 25 in multiple queries for better performance. Enhanced the caching mechanism by introducing an initial load check to prevent unnecessary searches on initial component render, and refined the cache key handling to ensure consistency. Took 13 minutes
1 parent 0416cf4 commit 4ed4c97

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

app/dfda/components/VariableSearchAutocomplete.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export default function VariableSearchAutocomplete({
7373
const [showDropdown, setShowDropdown] = useState(false)
7474
const componentRef = useRef<HTMLDivElement>(null)
7575
const [cachedVariables, setCachedVariables] = useState<GlobalVariable[]>([])
76-
// Create a cache key based on search term and params
76+
const initialLoadComplete = useRef(false)
77+
78+
// Single cache key function for both empty and search terms
7779
const cacheKey = `dfda-variable-search:${searchTerm}:${
7880
JSON.stringify(Object.keys(searchParams).sort().reduce<Record<string, string>>((obj, key) => {
7981
obj[key] = searchParams[key]
@@ -95,16 +97,28 @@ export default function VariableSearchAutocomplete({
9597
}, [])
9698

9799
useEffect(() => {
98-
const cachedVariables = getCachedResults(cacheKey)
99-
if (cachedVariables) {
100-
setCachedVariables(cachedVariables)
100+
const cachedResults = getCachedResults(cacheKey)
101+
if (cachedResults) {
102+
setCachedVariables(cachedResults)
103+
} else if (!initialLoadComplete.current && !searchTerm) {
104+
// Only do initial load if no cache and no search term
105+
const loadInitialResults = async () => {
106+
try {
107+
const results = await searchDfdaVariables('', searchParams)
108+
setCachedResults(cacheKey, results)
109+
setCachedVariables(results)
110+
} catch (error) {
111+
console.error('Error loading initial results:', error)
112+
}
113+
}
114+
loadInitialResults()
115+
initialLoadComplete.current = true
101116
}
102117

103118
const search = async () => {
104119
console.log('Searching for:', searchTerm ? `"${searchTerm}"` : '(empty string)')
105120
setIsLoading(true)
106121
try {
107-
108122
// Check cache first
109123
const cachedResults = getCachedResults(cacheKey)
110124
if (cachedResults) {
@@ -130,7 +144,7 @@ export default function VariableSearchAutocomplete({
130144

131145
const debounce = setTimeout(search, 300)
132146
return () => clearTimeout(debounce)
133-
}, [searchTerm, searchParams])
147+
}, [searchTerm, searchParams, cacheKey])
134148

135149
return (
136150
<div ref={componentRef} className="relative flex-grow">

app/dfda/components/dfda-home-page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export default function DFDAHomePage() {
175175
sort: '-numberOfCorrelationsAsCause',
176176
isPublic: '1',
177177
variableCategoryName: 'Foods',
178-
limit: '50'
178+
limit: '25'
179179
}}
180180
placeholder="Enter Foods🍟"
181181
/>
@@ -192,7 +192,7 @@ export default function DFDAHomePage() {
192192
sort: '-numberOfCorrelationsAsCause',
193193
isPublic: '1',
194194
variableCategoryName: 'Treatments',
195-
limit: '50'
195+
limit: '25'
196196
}}
197197
placeholder="Enter treatment 💊"
198198
/>
@@ -209,7 +209,7 @@ export default function DFDAHomePage() {
209209
sort: '-numberOfCorrelationsAsCause',
210210
isPublic: '1',
211211
variableCategoryName: 'Symptoms',
212-
limit: '50'
212+
limit: '25'
213213
}}
214214
placeholder="Enter treatment 💊"
215215
/>

0 commit comments

Comments
 (0)