Thank you for the fast and helpful response.
I was not thinking through the full consequences of switching users using the set_user
context manager. I have adopted a different solution to avoid doing that, and now it is working as expected.
(I know this is a not a full explanation of what was happening with the cache.)