Caching

http://dzqxh.com became too slow by rendering every page on each request, so I started to add caching to bamboo.

There's a bug in memcached haskell package, in particular, a get key won't return unless the key exists. There is no way to pre check its existence in memcache, so the interface is pretty much unusable.

This leads me to tokyo-cabinet, a fast on disk hash server. I simply used the haskell binding for tokyo-cabinet and created a cache' helper:

1
2
-- etag generator -> value function -> model -> value
cache' :: (a -> IO String) -> (a -> IO String) -> a -> (IO String)

The result is that performance increased about 3 times on my local machine, and 6 times on virtual server.

The caching mechanism I used is to make the key what guards the sanity of data, so the result is always correct.

Adding expiration based caching will probably yield more gains in performance, but the current result is good enough for me.

I found the first disadvantage in making view pure: it's not compatible with caching, since, in this case, caching involves IO. As a result, in bamboo caching can only happen in models and controllers.

blog comments powered by Disqus