Skip to content

Commit

Permalink
refactor: ♻️ rework caching logic
Browse files Browse the repository at this point in the history
  • Loading branch information
spences10 committed May 12, 2024
1 parent c9e13d6 commit 9e804b5
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions src/routes/api/fetch-post-analytics/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ export const GET = async ({ url, fetch }) => {

const fetch_visits = async (slug: string) => {
const cache_key = `analytics-${slug}`
const cached_data = analytics_cache.get(cache_key)
let cached_data = analytics_cache.get(cache_key)

// Check if cached data exists and is not older than 24 hours
if (
cached_data &&
differenceInHours(
Expand All @@ -58,50 +59,51 @@ const fetch_visits = async (slug: string) => {
) < 24
) {
return cached_data.data
}

const client = turso_client()
let page_analytics: any = {
daily: null,
monthly: null,
yearly: null,
}
} else {
// If cache is stale or doesn't exist, fetch new data
const client = turso_client()
let page_analytics: any = {
daily: null,
monthly: null,
yearly: null,
}

// Construct and execute the UNION query
const sql = `
SELECT 'day' AS period, * FROM post_analytics WHERE date_grouping = 'day' AND slug = ?
UNION
SELECT 'month' AS period, * FROM post_analytics WHERE date_grouping = 'month' AND slug = ?
UNION
SELECT 'year' AS period, * FROM post_analytics WHERE date_grouping = 'year' AND slug = ?;
`
// Construct and execute the UNION query
const sql = `
SELECT 'day' AS period, * FROM post_analytics WHERE date_grouping = 'day' AND slug = ?
UNION
SELECT 'month' AS period, * FROM post_analytics WHERE date_grouping = 'month' AND slug = ?
UNION
SELECT 'year' AS period, * FROM post_analytics WHERE date_grouping = 'year' AND slug = ?;
`

try {
const result = await client.execute({
sql,
args: [slug, slug, slug],
})
try {
const result = await client.execute({
sql,
args: [slug, slug, slug],
})

// Process the results
result.rows.forEach(row => {
if (row.period === 'day') page_analytics.daily = row
if (row.period === 'month') page_analytics.monthly = row
if (row.period === 'year') page_analytics.yearly = row
})
} catch (error) {
console.error('Error fetching from Turso DB:', error)
return null
}

// Process the results
result.rows.forEach(row => {
if (row.period === 'day') page_analytics.daily = row
if (row.period === 'month') page_analytics.monthly = row
if (row.period === 'year') page_analytics.yearly = row
})
} catch (error) {
console.error('Error fetching from Turso DB:', error)
return null
}
// After fetching new data, update the cache
const new_data = {
last_fetched: new Date().toISOString(),
data: page_analytics,
}
analytics_cache.set(cache_key, new_data)

// After fetching new data, update the cache
const new_data = {
last_fetched: new Date().toISOString(),
data: page_analytics,
// Return the data
return page_analytics
}
analytics_cache.set(cache_key, new_data)

// Return the data
return page_analytics
}

const stale_data = async (slug: string) => {
Expand Down

0 comments on commit 9e804b5

Please sign in to comment.