Skip to content

Commit a8c0c66

Browse files
etewiahclaude
andcommitted
Optimize social media links and add database warmup
Social Media Links (7 queries -> 1 query): - Load all social media links in a single query using WHERE IN - Memoize results to avoid repeated queries within same request - Add clear_social_media_cache method for cache invalidation Database Warmup: - Add initializer to pre-warm database connection on boot - Executes SELECT 1 and preloads first website in production - Moves PostgreSQL type mapping overhead from first request to boot Before: 28 queries, 280ms After: 25 queries, 257ms 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0f5374b commit a8c0c66

File tree

3 files changed

+90
-18
lines changed

3 files changed

+90
-18
lines changed

app/models/concerns/pwb/website_social_linkable.rb

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,64 @@
55
# Provides social media link accessors for websites.
66
# Retrieves social media URLs from the links association.
77
#
8+
# Performance: All social media links are loaded in a single query and memoized
9+
# to avoid N+1 queries when accessing multiple social media platforms.
10+
#
811
module Pwb
912
module WebsiteSocialLinkable
1013
extend ActiveSupport::Concern
1114

15+
# All supported social media platforms
16+
SOCIAL_MEDIA_PLATFORMS = %w[facebook instagram linkedin youtube twitter whatsapp pinterest].freeze
17+
18+
# Load all social media links in one query and index by slug
19+
# This is memoized to avoid repeated queries within the same request
20+
def social_media_links_cache
21+
@social_media_links_cache ||= begin
22+
slugs = SOCIAL_MEDIA_PLATFORMS.map { |p| "social_media_#{p}" }
23+
links.where(slug: slugs).index_by(&:slug)
24+
end
25+
end
26+
27+
# Clear the cache (call after updating social links)
28+
def clear_social_media_cache
29+
@social_media_links_cache = nil
30+
end
31+
1232
def social_media_facebook
13-
links.find_by(slug: "social_media_facebook")&.link_url
33+
social_media_links_cache["social_media_facebook"]&.link_url
1434
end
1535

1636
def social_media_twitter
17-
links.find_by(slug: "social_media_twitter")&.link_url
37+
social_media_links_cache["social_media_twitter"]&.link_url
1838
end
1939

2040
def social_media_linkedin
21-
links.find_by(slug: "social_media_linkedin")&.link_url
41+
social_media_links_cache["social_media_linkedin"]&.link_url
2242
end
2343

2444
def social_media_youtube
25-
links.find_by(slug: "social_media_youtube")&.link_url
45+
social_media_links_cache["social_media_youtube"]&.link_url
2646
end
2747

2848
def social_media_pinterest
29-
links.find_by(slug: "social_media_pinterest")&.link_url
49+
social_media_links_cache["social_media_pinterest"]&.link_url
3050
end
3151

3252
def social_media_instagram
33-
links.find_by(slug: "social_media_instagram")&.link_url
53+
social_media_links_cache["social_media_instagram"]&.link_url
3454
end
3555

3656
def social_media_whatsapp
37-
links.find_by(slug: "social_media_whatsapp")&.link_url
57+
social_media_links_cache["social_media_whatsapp"]&.link_url
3858
end
3959

4060
# Returns all social media platforms with their current values
4161
# Used by admin UI for editing
42-
SOCIAL_MEDIA_PLATFORMS = %w[facebook instagram linkedin youtube twitter whatsapp].freeze
43-
4462
def social_media_links_for_admin
4563
SOCIAL_MEDIA_PLATFORMS.map do |platform|
4664
slug = "social_media_#{platform}"
47-
link = links.find_by(slug: slug)
65+
link = social_media_links_cache[slug]
4866
{
4967
platform: platform,
5068
slug: slug,
@@ -63,7 +81,9 @@ def update_social_media_link(platform, url)
6381
visible: url.present?,
6482
icon_class: "fa fa-#{platform}"
6583
)
66-
link.save
84+
result = link.save
85+
clear_social_media_cache if result
86+
result
6787
end
6888
end
6989
end

app/models/concerns/website/social_linkable.rb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,56 @@
55
# Provides social media link accessors for websites.
66
# Retrieves social media URLs from the links association.
77
#
8+
# Performance: All social media links are loaded in a single query and memoized
9+
# to avoid N+1 queries when accessing multiple social media platforms.
10+
#
811
module Website
912
module SocialLinkable
1013
extend ActiveSupport::Concern
1114

15+
# All supported social media platforms
16+
SOCIAL_MEDIA_PLATFORMS = %w[facebook instagram linkedin youtube twitter whatsapp pinterest].freeze
17+
18+
# Load all social media links in one query and index by slug
19+
# This is memoized to avoid repeated queries within the same request
20+
def social_media_links_cache
21+
@social_media_links_cache ||= begin
22+
slugs = SOCIAL_MEDIA_PLATFORMS.map { |p| "social_media_#{p}" }
23+
links.where(slug: slugs).index_by(&:slug)
24+
end
25+
end
26+
27+
# Clear the cache (call after updating social links)
28+
def clear_social_media_cache
29+
@social_media_links_cache = nil
30+
end
31+
1232
def social_media_facebook
13-
links.find_by(slug: "social_media_facebook")&.link_url
33+
social_media_links_cache["social_media_facebook"]&.link_url
1434
end
1535

1636
def social_media_twitter
17-
links.find_by(slug: "social_media_twitter")&.link_url
37+
social_media_links_cache["social_media_twitter"]&.link_url
1838
end
1939

2040
def social_media_linkedin
21-
links.find_by(slug: "social_media_linkedin")&.link_url
41+
social_media_links_cache["social_media_linkedin"]&.link_url
2242
end
2343

2444
def social_media_youtube
25-
links.find_by(slug: "social_media_youtube")&.link_url
45+
social_media_links_cache["social_media_youtube"]&.link_url
2646
end
2747

2848
def social_media_pinterest
29-
links.find_by(slug: "social_media_pinterest")&.link_url
49+
social_media_links_cache["social_media_pinterest"]&.link_url
3050
end
3151

3252
def social_media_instagram
33-
links.find_by(slug: "social_media_instagram")&.link_url
53+
social_media_links_cache["social_media_instagram"]&.link_url
3454
end
3555

3656
def social_media_whatsapp
37-
links.find_by(slug: "social_media_whatsapp")&.link_url
57+
social_media_links_cache["social_media_whatsapp"]&.link_url
3858
end
3959
end
4060
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
# Database Connection Warm-up
4+
#
5+
# Pre-warms the database connection pool on application boot to avoid
6+
# cold-start latency on the first request. This moves the PostgreSQL
7+
# type mapping and connection setup overhead from the first request
8+
# to the boot phase.
9+
#
10+
# This is especially helpful in production where the first request
11+
# might otherwise take 50-100ms longer due to connection setup.
12+
13+
Rails.application.config.after_initialize do
14+
if Rails.env.production? || ENV["WARMUP_DB"] == "true"
15+
Rails.logger.info "[Database Warmup] Pre-warming database connection..."
16+
17+
begin
18+
# Execute a simple query to establish the connection and
19+
# trigger PostgreSQL type mapping
20+
ActiveRecord::Base.connection.execute("SELECT 1")
21+
22+
# Optionally preload the first website to populate caches
23+
if defined?(Pwb::Website) && Pwb::Website.table_exists?
24+
Pwb::Website.first
25+
end
26+
27+
Rails.logger.info "[Database Warmup] Connection warmed up successfully"
28+
rescue StandardError => e
29+
Rails.logger.warn "[Database Warmup] Failed to warm up: #{e.message}"
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)