Lock store before clearing or persisting (Fix #97)

Hey,

although I can't reproduce #97 anymore, someone else on my team can until I apply this patch (sometimes at least).

This is work-in-progress, if we agree on the direction this goes, then I will of course remove debug spam, fix formatting/whitespaces, add documentation and tests.

I was especially unsure about triggering the events. Should we only trigger them if the store was not locked? Should we always trigger them? Is this too important as an edge case that is hardly reproducible anyways? At least my team member doesn't notice any borkage afterwards... 

I believe this improves the situation a lot anyway.

Looking forward to your suggestions!