@@ -2446,4 +2446,220 @@ def authenticate_api_user(user)
24462446 'HS256'
24472447 )
24482448 request . headers [ 'Authorization' ] = "Bearer #{ token } "
2449- end
2449+ end
2450+
2451+
2452+ # ═══════════════════════════════════════════════════════════════════════════════
2453+ # 11. PERFORMANCE OPTIMIZATION
2454+ # ═══════════════════════════════════════════════════════════════════════════════
2455+
2456+ # config/application.rb - Performance configurations
2457+ module MyWebApp
2458+ class Application < Rails ::Application
2459+ # ... existing configuration ...
2460+
2461+ # Asset pipeline optimizations
2462+ config . assets . compile = false
2463+ config . assets . digest = true
2464+ config . assets . compress = true
2465+
2466+ # Gzip compression
2467+ config . middleware . use Rack ::Deflater
2468+
2469+ # Cache store configuration
2470+ config . cache_store = :redis_cache_store , {
2471+ url : ENV [ 'REDIS_URL' ] ,
2472+ namespace : 'myapp_cache'
2473+ }
2474+
2475+ # Session store with Redis
2476+ config . session_store :redis_store ,
2477+ servers : [ ENV [ 'REDIS_URL' ] ] ,
2478+ expire_after : 1 . week ,
2479+ key : '_myapp_session'
2480+ end
2481+ end
2482+
2483+ # app/models/concerns/cacheable.rb
2484+ module Cacheable
2485+ extend ActiveSupport ::Concern
2486+
2487+ class_methods do
2488+ def cached_find ( id , expires_in : 1 . hour )
2489+ Rails . cache . fetch ( "#{ self . name . downcase } /#{ id } " , expires_in : expires_in ) do
2490+ find ( id )
2491+ end
2492+ end
2493+
2494+ def cached_count ( expires_in : 10 . minutes )
2495+ Rails . cache . fetch ( "#{ self . name . downcase } /count" , expires_in : expires_in ) do
2496+ count
2497+ end
2498+ end
2499+ end
2500+
2501+ def cache_key_with_version
2502+ "#{ super } /#{ updated_at . to_i } "
2503+ end
2504+
2505+ def expire_cache
2506+ Rails . cache . delete ( "#{ self . class . name . downcase } /#{ id } " )
2507+ Rails . cache . delete ( "#{ self . class . name . downcase } /count" )
2508+ end
2509+ end
2510+
2511+ # Include in models that need caching
2512+ class Post < ApplicationRecord
2513+ include Cacheable
2514+
2515+ after_update :expire_cache
2516+ after_destroy :expire_cache
2517+
2518+ # ... rest of model ...
2519+ end
2520+
2521+ # app/controllers/concerns/caching.rb
2522+ module Caching
2523+ extend ActiveSupport ::Concern
2524+
2525+ private
2526+
2527+ def cache_page ( key , expires_in : 1 . hour , &block )
2528+ Rails . cache . fetch ( key , expires_in : expires_in ) do
2529+ yield
2530+ end
2531+ end
2532+
2533+ def set_cache_headers ( max_age : 1 . hour )
2534+ response . cache_control [ :max_age ] = max_age . to_i
2535+ response . cache_control [ :public ] = true
2536+ end
2537+ end
2538+
2539+ # Database optimization examples
2540+ class OptimizedPostsQuery
2541+ def self . homepage_posts
2542+ Post . includes ( :user , :category , :tags )
2543+ . published
2544+ . featured
2545+ . select ( :id , :title , :slug , :excerpt , :published_at , :user_id , :category_id )
2546+ . recent
2547+ . limit ( 3 )
2548+ end
2549+
2550+ def self . posts_with_stats
2551+ Post . joins ( :user , :category )
2552+ . select ( 'posts.*, users.first_name, users.last_name, categories.name as category_name' )
2553+ . where ( status : :published )
2554+ . order ( published_at : :desc )
2555+ end
2556+
2557+ def self . popular_posts ( limit : 10 )
2558+ Rails . cache . fetch ( "popular_posts/#{ limit } " , expires_in : 1 . hour ) do
2559+ Post . published
2560+ . select ( :id , :title , :slug , :views_count )
2561+ . order ( views_count : :desc )
2562+ . limit ( limit )
2563+ end
2564+ end
2565+ end
2566+
2567+ # Background job for cache warming
2568+ class CacheWarmupJob < ApplicationJob
2569+ queue_as :low_priority
2570+
2571+ def perform
2572+ # Warm up popular posts cache
2573+ OptimizedPostsQuery . popular_posts
2574+
2575+ # Warm up categories cache
2576+ Category . active . includes ( :posts ) . order ( :name )
2577+
2578+ # Warm up homepage data
2579+ OptimizedPostsQuery . homepage_posts
2580+ end
2581+ end
2582+
2583+ # Database indexes for performance
2584+ =begin
2585+ # Add these to your migrations for better query performance
2586+
2587+ class AddIndexesForPerformance < ActiveRecord::Migration[7.1]
2588+ def change
2589+ # Posts indexes
2590+ add_index :posts, [:status, :published_at]
2591+ add_index :posts, [:category_id, :status]
2592+ add_index :posts, [:user_id, :status]
2593+ add_index :posts, [:featured, :status]
2594+ add_index :posts, :views_count
2595+
2596+ # Comments indexes
2597+ add_index :comments, [:post_id, :status, :created_at]
2598+ add_index :comments, [:parent_id]
2599+
2600+ # Users indexes
2601+ add_index :users, [:status, :created_at]
2602+ add_index :users, :username
2603+
2604+ # Full-text search indexes (PostgreSQL)
2605+ add_index :posts, :title, using: :gin, opclass: :gin_trgm_ops
2606+ add_index :posts, :excerpt, using: :gin, opclass: :gin_trgm_ops
2607+ end
2608+ end
2609+ =end
2610+
2611+ # ═══════════════════════════════════════════════════════════════════════════════
2612+ # 12. DEPLOYMENT AND PRODUCTION
2613+ # ═══════════════════════════════════════════════════════════════════════════════
2614+
2615+ # config/environments/production.rb
2616+ Rails . application . configure do
2617+ # Settings specified here will take precedence over those in config/application.rb.
2618+
2619+ # Code is not reloaded between requests.
2620+ config . cache_classes = true
2621+
2622+ # Eager load code on boot.
2623+ config . eager_load = true
2624+
2625+ # Full error reports are disabled and caching is turned on.
2626+ config . consider_all_requests_local = false
2627+ config . action_controller . perform_caching = true
2628+
2629+ # Ensures that a master key has been made available
2630+ config . require_master_key = true
2631+
2632+ # Disable serving static files from the `/public` folder by default since
2633+ # Apache or NGINX already handles this.
2634+ config . public_file_server . enabled = ENV [ 'RAILS_SERVE_STATIC_FILES' ] . present?
2635+
2636+ # Compress CSS using a preprocessor.
2637+ config . assets . css_compressor = :sass
2638+
2639+ # Do not fallback to assets pipeline if a precompiled asset is missed.
2640+ config . assets . compile = false
2641+
2642+ # Enable serving of images, stylesheets, and JavaScripts from an asset server.
2643+ config . asset_host = ENV [ 'ASSET_HOST' ] if ENV [ 'ASSET_HOST' ] . present?
2644+
2645+ # Specifies the header that your server uses for sending files.
2646+ config . action_dispatch . x_sendfile_header = 'X-Accel-Redirect' # for NGINX
2647+
2648+ # Store uploaded files on the local file system (see config/storage.yml for options).
2649+ config . active_storage . variant_processor = :mini_magick
2650+
2651+ # Mount Action Cable outside main process or domain.
2652+ # config.action_cable.mount_path = nil
2653+ # config.action_cable.url = 'wss://example.com/cable'
2654+ # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
2655+
2656+ # Force all access to the app over SSL
2657+ config . force_ssl = true
2658+
2659+ # Include generic and useful information about system operation
2660+ config . log_level = :info
2661+
2662+ # Prepend all log lines with the following tags.
2663+ config . log_tags = [ :request_id ] # RUBY ON RAILS WEB DEVELOPMENT - Comprehensive Reference - by Richard Rembert
2664+ # Ruby on Rails enables rapid prototyping and development of full-stack web applications
2665+ # with convention over configuration, built-in security, and powerful abstractions
0 commit comments