Fix bugs in caching for dev branch

* merges code for caching from master
* uses primary key for math_inputhash in math table
  to avoid problems with table prefixes (i.e. in
  the scope of unit_tests)

Change-Id: Ica752e3ae9a244c5a076a25308043749679983d6