Knowledge resources

We like to share some of our experiences in the hope that other developers wont have to waste hours on the same battles that we have already fought and won.

Varnish caching in memory

We've been experimenting with Varnish for the last week or so and have been very satisfied with the results. Initially we allocated 1GB for cache storage, but have found that to be insufficient. Our /var/lib/varnish/varnish_storage.bin file had hit the limit and Varnish started removing items from cache based on its "least-recently used" LRU algorithm. It was only just then we realised that there was probably a better way. After googling a bit, we discovered the malloc option for Varnish.

Stop Varnish caching timed-out pages

We've been working with a client who runs a very overloaded ColdFusion web server and have been setting up Varnish cache on Amazon AWS to alleviate some of the load and significantly increase page response and load times.

MySQL building interface to Memcache

Mario beck writes:

Why not use memcached directly?

Why that plugin to MySQL? Memcache is a memory caching but not a persistent datastore. The memcached plugin for MySQL stores the data in an InnoDB table. So you get persistence, nearly unlimited table size and still you can access your data through SQL if you want more complex stuff like COUNT(*) WHERE pkey LIKE ="page:%"; This would not be possible in memcached. But with the memcached plugin you can store data with memcached SET and report on your data with SQL queries.

This feature is available in the MySQL Labs release.

Mongo tip: Disable table scans for development

Just like in any relational database, table scans in Mongo are very slow. In the mongod.conf configuration file you can disable table scans with:

notablescan = true

Now when you try to make a query that would require a table scan, an exception will be thrown by Mongo and passed back via your driver. It's best to turn this on early during the development process since it can also help highlight poorly written queries.

Note for example that a query like:

Mongo tip: Avoid DBRef at all costs

We use Morphia for making life easy when persisting Java objects to a MongoDB datastore. This library does most of the work of marshalling objects and preparing queries. Unfortunately when referencing (rather than embedding) objects using the @Reference annotation, Morphia using Mongo's DBRef mechanism for storing the ID. The DBRef is a compound key to a Mongo Collection and the ID of the object inside that collection.

Refreshing hash part of URL to trigger scrolling

For the admin pages of the "Unified Database" we've built, I recently wrote a simple jump link generator using javascript/jquery. It basically looks up all <h2> headings on the page, inserts an anchor link inside and adds a corresponding jump link to a navigation box in the side menu.

jQuery ajax callback not being called?

When replacing calls to some legacy ajax help functions, I recently came across a problem it took a while for me to resolve. According to Firebug (a very useful FireFox plugin, in case you don't know about it), the ajax call was made and the response looked perfectly fine, but my callback function was never invoked. In the end, for testing, I had a simple snippet like this:

$.get('/some/url', function(){ alert('called'); });

Passing custom arguments to jQuery ajax callback functions

When phasing out our old collection of ajax help functions, I ran into a problem with passing custom arguments to the callback function of the ajax request. Our old ajax functions were based on eval()'ing and could accept custom arguments as a literal string that was used together with the function name to construct the call of the callback. When using jQuery, the situation is different, since jQuery provides the callback function with a fixed set of arguments:

$.get('/some/url', function(data, textStatus, XMLHttpRequest) {
	//do something
});

Cfquery result structs

In old Coldfusion 6, you could access the number of rows returned for a select query by going queryName.recordcount.

In CF8, cfquery now has a result attribute, which is used for naming a struct containing additional information about the query executed. So now for the query:

<cfquery name="a" result="aRes" datasource="uid">
...
</cfquery>

You can do both a.recordcount and aRes.recordcount, but only the latter works for update or delete queries!

Improving user experience in Google translated mode

We recently added a simple translate menu to a client's website, which offered the website to be google translated into a few different languages. Since there is no easy way to get rid of the translated mode's frame layout and go back to the original language without Google's top bar, the client wanted us to open the translation in a new window. This was easily achieved by adding target="_blank" to the form responsible for launching the translation.

पृष्ठ

Subscribe to Knowledge resources