Cache: Fix a race condition
Cache: Fix a race condition

This was causing the insertion assert to trigger, due a thread querying
the database while:
- Holding the cache lock
- Acquiring the database lock
- Inserting the fetched entity in cache if it didn't exist

While another thread was inserting an entity while:
- Holding the database lock
- Releasing it after the insertion was done
- Holding the cache lock
- Checking that the entity didn't exist in cache

Since the locking order wasn't consistent, the 1st thread was
interupting the 2nd before it was done with the entire insertion
method, causing an inconsistent state.

chouquette/medialibrary,chouquette/medialibrary,chouquette/medialibrary,chouquette/medialibrary