ADD: Reset database connection if a query fails, to remove any locks it might have.
FIX: mem-cache incorrectly marked items which wasn't saved because of a time-out as saved
CLEANUP: used templates to generalize mem-cache items, and centralize its implementation