Monthly Archives: January 2009

Booko – now with better fragment caching!

One of the more processor intensive sections on the Booko site is the “Most Viewed” section.  This section is generated by finding all the books which have been viewed in the last 7 days, and ranking them by their frequency. This query can take up to 1.6 seconds according to Benchmark.realtime {}. I had set up fragment caching for this, but every time a book is viewed, Booko notes down the ISBN and the time, then, because the data has changed, goes and recalculates the most popular book again.  So the caching really isn’t helping anyone who’s looking at books – it may help someone who’s on the front page, but that’s it. 

Ideally I wanted this section of HTML to have a time based expiry but that functionality isn’t part of the regular Rails caching system and I don’t want to install plugins to do this for me. After a bit of searching, Google found me a very relevant page which discussed the issue I was facing.

The goal is for a time based expiry – let’s say 15 minutes or 900 seconds. When you call the cache function, you pass in some data to help it create a unique key for the HTML being cached.  If that name already has some cached HTML associated with it, that HTML will be rendered rather than generating the HTML again. Previously, I was just naming this cache “Popular” – the code looked like this:
[sourcecode language=’ruby’]
<% cache("Popular") do %>
# Generate the list of most popular books
# Print out the popular books
<% end %>
Instead, we’ll do this:
[sourcecode language=’ruby’]<% cache(["Popular", / 900 ].to_s) do %>
# Generate the list of most popular books
# Print out the popular books
<% end %>[/sourcecode]
This version will append a number to the cache key which will change every 900 seconds, or 15 minutes. Here’s what it generates as a key:

>> ["Popular", / 900 ].to_s
=> "Popular1369073"

So, now we have a new key for the cache every 15 minutes. When the key changes, the cache system won’t be able to find the HTML and will regenerate it.   This would be a terrible idea if you’re using file based caching BTW, since you’d be constantly creating new cache files.  However, if you’re using memcached, like Booko does, you’re home free. Memcached automatically expires objects from its cache using an LRU algorithm. So, with any luck, Booko’s performance should improve.

Bookstore features to help out Affiliates

I was talking with Jeremy (the brains behind Booktagger) about what kind of features an online bookstore could have to help affiliates do a better job. After documenting some, I thought they might make an interesting blog post. So, good features for a book site to have, to help affiliates do a better job:

1) An API.  The API should accept two types of query – ISBN and a text based search. They could be separate URLs or they could be combined into a single URL and just intelligently recognise if an ISBN or search string has been submitted.
1a) If an ISBN has been submitted, return an XML formatted document including ISBN, title, author, publisher, format, availability, link to cover image and price.  If the ISBN isn’t found, return an XML document explaining that there were 0 matching records.
1b) If a search term has been submitted, do a search and return a list of matching items.  The items could either be complete – multiple records of the type above in 1a, or an abridged version, requiring the user to resubmit the individual ISBN to get a detailed version of the book.

2) A method of allowing an affiliate to add multiple items to a users carts.  To see an example of this, add a few books to Booko’s cart – make sure the books are all available at one of the sites which include this functionality such as the Nile or an Amazon. Once you’ve added a few items, click the “the Nile” or “Amazon ..” link in the cart and you’ll be taken to a page where you confirm that you’d like to add all the following items.

3) Easy to read and generate links to books which can be created based on the ISBN. Like,

4) An Affiliate program.  The affiliate program should make it easy to create dynamic links to any book in store. Perhaps within the URL or as an argument ?refid=123 style.

These features would make it a book store easy to work with, and helps the end user’s experience by integrating the shopping carts.

More shops and various updates

I’ve added DStore to Booko this morning. Two other shops, Emporium Books and Glee Books were also added earlier in the year bringing Booko to a total of 25 stores. 

Thanks to Anish for a couple of suggestions I’ve implemented including Favicon to help those of you with more browser tabs than desktop space and clearer page titles.