Refactor writing and deleting cache

Should be safe from race conditions an exception now