API: fast serialization
This course examines how a Rails app's Jbuilder views serialize JSON and when caching that output is worth it. It traces the two branches of array!, the block form and the partial form with cached:, and shows the batched cache reads the partial form triggers. It covers json.cache! and its use of Rails.cache, and render json:, which skips ActionView and Jbuilder. It compares the app's HTML fragment caching and weighs every mechanism against the search view's limit of eight results per type.
What you'll learn
- ✓The block and partial branches of Jbuilder's array!
- ✓Batched cache reads through ActionView::CollectionRenderer with cached:
- ✓How json.cache! reads and writes Rails.cache
- ✓What render json: skips compared with a Jbuilder view
- ✓Single-key and collection fragment caching in the app's HTML
- ✓Judging caching against the search view's size
This app's array views take the simple path
The junior course "API development: Jbuilder" read this app's .json.jbuilder views. Several of them build arrays: the catalogue search behind the command palette, and the partner API's lists of learners, certificates and activity. The search view is the simplest:
Read the full lesson free →Curriculum
7 lessonsThis app's array views take the simple path
Traces the search view's block call to array! and shows why that branch never reads a cached: option.
The partial-and-cached path this app has never used
Runs array! with partial:, as: and cached: on search data, firing one batched cache read and write.
json.cache!: the same cache this app already trusts
Shows json.cache! uses the same Rails.cache as the app's HTML fragments, with a miss, write and hit.
render json: skips Jbuilder and ActionView entirely
Explains how render json: calls to_json directly, and why the app limits it to small hash literals.
Where this app's HTML already uses multi-fetch caching
Compares the app's four single-key HTML caches with its three collection renders that use batched cached: reads.
Is any of this worth doing at this app's real scale?
Weighs caching for the search view, capped at eight results per type, against its two Postgres queries.
Putting it together: which views need caching
Reviews the course's findings and introduces a plain Ruby challenge that models batched cache reads.