
Performance and Caching in Rails — Russian Doll Caching, Fragment Caching, and Redis for AI Apps
Your AI features work. Your tests pass. But every time a user asks a question, your app hits an external API, waits 2-5 seconds, and burns tokens. Multiply that by a hundred concurrent users, and your server is crying. The fix isn't complicated. It's caching — and Rails has the best caching story in web development. Let's make your AI app fast. The Problem with AI Apps Every AI call is expensive in three ways: Time — API calls take 1-10 seconds Money — tokens cost real dollars Rate limits — providers throttle you Caching solves all three. Same question? Same answer. No API call needed. Low-Level Caching with Rails.cache The simplest approach — cache raw AI responses: # app/services/chat_service.rb class ChatService def ask ( question ) cache_key = "ai_response/ #{ Digest :: SHA256 . hexdigest ( question ) } " Rails . cache . fetch ( cache_key , expires_in: 1 . hour ) do client . chat ( parameters: { model: "gpt-4" , messages: [{ role: "user" , content: question }] } ). dig ( "choices"
Continue reading on Dev.to
Opens in a new tab



