learningequality/ka-lite — pull request #819 by bcipolli (2013-10-13 13:14:57+00:00)
Title: Cache total points

This is dependent on #818, see comparison for this PR's code changes:
https://github.com/bcipolli/ka-lite/compare/minimize_queries_2...cache_total_points?expand=1

Background:
* On every request, we recompute total points by doing aggregation over all VideoLog, then ExerciseLog objects.

Issues:
* These are the only queries remaining for the `status` API request. Any query can be blocked behind (potentially slow) DB writes
* The queries themselves may be slow/expensive.

Changes:
* Cache the points computation in the session object. This means they get computed only when:
* The user logs in
* The user saves and ExerciseLog or VideoLog

Notes:
* There's no guarantee of a performance boost.
* When the user is just browsing, it should be blazing fast now
* When the user is doing exercises, things will be slower: they have to do 2 DB writes (Log update, Session update) instead of 1.
* This would interact poorly with other ways to change total points, notably syncing and importing data from KA.
* However, frequency is low (syncing a user's data from another facility to a facility where they're active currently has never happened), simple workaround exists (log out, log back in), and impact is low (total points is temporarily out of sync)

PR help requests:
* Timing: This is an important code change, so let's give a full week to consider.
* @gimick Could you compare this to #818? This might be a 4-way comparison:
* #818 with db-backed and file-backed sessions
* This PR with db-backed and file-backed sessions
* @jamalex any thoughts on the code design are appreciated!