Permalink
Browse files

Add cache warming

  • Loading branch information...
1 parent b3e6a77 commit 627fd546d1e12091158e8290c0890aae7145694f twinturbo committed Jul 11, 2012
Showing with 22 additions and 0 deletions.
  1. +22 −0 blog/_posts/advanced_caching/2012-07-11-advanced_caching_part_6-fast_json_apis.md
@@ -88,6 +88,28 @@ class ApplicationSerializer < ActiveModel::Serializer
end
```
+## Background Cache Warming
+
+We've consolidated all the JSON generation into individual classes.
+Since the API only returns JSON we can generate that JSON silently in
+the background to warm the caches. This won't do anything about HTTP
+caching but it will make initial requests faster since JSON will be
+cached. Here's a simple Sidekiq worker:
+
+```ruby
+class CacheWarmer
+ include Sidekiq::Worker
+
+ def perform
+ Post.find_each do |post|
+ serializer = post.active_model_serializer.new post
+ # This wil cache the JSON and the hash it's generated from
+ serializer.to_json
+ end
+ end
+end
+```
+
And that's all there is too it folks! It's not complicated but it will
make your API significantly faster.

0 comments on commit 627fd54

Please sign in to comment.