Speeding Up your Website with Varnish

Varnish Cache is a popular caching HTTP reverse proxy. Awhile back, I wrote about using nginx as a reverse proxy. But while nginx is great as a reverse proxy, it doesn’t perform caching. Caching can be highly desirable for a website or web application that needs to serve lots of static content. Most generic websites fall into this group, while more dynamic web applications may or may not benefit from caching.

Varnish sits in front of any HTTP compatible server, and it can be configured to selectively cache the contents. It will almost certainly deliver the cached content faster than the backend — especially if the requests ordinarily take some processing on the backend, such as to render or look up a resource on the filesystem or in a database. While the default storage mechanism for Varnish is backed by the filesystem, it can be configured to allocate storage with malloc using RAM to cache directly. Read more on Speeding Up your Website with Varnish…

Beware the Elvis Operator in Groovy

While implementing a really simple caching mechanism in a Grails app, I came across what seemed like some odd behavior. I had an array of objects that would be built up within a function if not passed from the caller. To keep it as an optional argument, I had a default value of “null.”

def doStuff(arg1, arg2 = null) {
    arg2 ?: DomainClass.findAll(it.property == arg1)
 
    // Do stuff
}

This strategy was working most of the time, but there were still spurious database accesses whenever an empty array was being passed in. As it turns out, the spurious database accesses were occuring whenever the parent was passing an empty array for arg2. In Groovy, empty collections are falsy, as are empty strings and the number 0.

Read more on Beware the Elvis Operator in Groovy…

Generic Memoization in C#

I have seen some very nice, generic forms of memoization in the dynamic languages I’ve used. In languages like Ruby and Perl, for example, dynamically redefining a method to be a memoized version of itself is a good way to transparently handle it. However, I haven’t seen any examples of generic case memoization for C# methods that I’ve been happy with. So I took a stab at it.

Read more on Generic Memoization in C#…