Memoization fixes (rebased onto develop)



This is the same as gh-952 but rebased onto develop.

----

This PR should fix https://trac.openmicroscopy.org.uk/ome/ticket/11971 and https://trac.openmicroscopy.org.uk/ome/ticket/12019.

/cc @jianguo11