How To Cache Highly Dynamic Data in Rails with Memcache – Part 1

There are a number of ways increase Ruby on Rails performance through caching. Caching works because things don’t change….or don’t change frequently.

In Cullect, almost everything is dynamic, even Cullect’s HTML presentation format has 3 different states depending on access privileges and there are 8 other presentation formats available.

The standard page, action, and fragment caching make less sense when the ‘heaviest’ data are also the most dynamic – the feed items.

For the feed items, I’m using a building a custom memcached name-value-pair holding 10 attributes describing the request as the key name and the items themselves as the key value.

From the ‘show’ action in my controller:

key = "
#{item_count}:
#{attribute_1}:
#{attribute_2}:
#{attribute_3}:
#{attribute_4}:
#{etc...}"

Notice the first attribute in the key is the total number of items within a specific Cullect Reading List – which will change when a feed updates or an item is hidden – automatically expiring stale caches.

Then, I check the cache for the key and pull the items from the cache if it exists.

if Cache.get(key)
@allitems = Cache.get(key)

If there’s no key in the cache, I do the query and put the retrieved items into the cache.

else
@allitems = get_items(attribute_1, attribute_2, attribute_3, attribute_4, etc)
Cache.put key, @allitems
end

While this speeds up subsequent requests, there’s still the question of speeding up the initial request. I’ll save that for part 2.