cache: Improve _strip_invalid_entites() API

Add more information to documentation.
Add a check on the input string to make sure we avoid crashes.
Add test case