(Bug1353802) Add query result caching to handle_event

Improve the performance of handle_event by caching the results of
database queries. In testing adding 2000 users from a CSV file with
a typical number of fields (username, password, email, firstname,
lastname, studentid, address, town, city, country, homenumber,
mobilenumber), this change reduced the average import time over
three runs (with a fresh install each time) as follows:

                       Before      After     Saving
Total Wall Time        246s        114s       132s - 54%
handle_event           132s         21s       111s - 84%

(The change to handle_event seems to affect other functions'
execution times, perhaps due to other database caches still
being in memory that would otherwise be replaced).

Change-Id: Ibf28650df710dd807f95fca30a2dfc29ed92f5cd
Signed-off-by: Nigel Cunningham <nigelc@catalyst-au.net>