Update CachedRequest._write_cache() in pywikibot/data/api/_requests.py to:
- Serialize into a unique temporary file in the cache directory, preserving the existing format and pickle protocol.
- Close the temporary file before publishing it with os.replace().
- On failure, clean up only the temporary file, preserving any existing destination entry.
- Warn on filesystem failures while allowing successful API responses to return. Preserve serialization exceptions and interruptions without masking them with cleanup errors.
- Ensure the cache maintenance script skips temporary files.
Benefits
- Prevent readers from encountering partially written cache entries.
- Prevent failed writers from deleting existing entries or another writer’s completed entry.
- Avoid secondary cleanup exceptions failing otherwise successful API requests.
- Make cache persistence failures visible and diagnosable.
Limitations
- Atomic replacement does not prevent concurrent cache misses from issuing duplicate API requests.
- Use unique temporary filenames; a shared .tmp path introduces writer races.
- Verify Windows behavior and file permissions.
- Keep read-side exception changes, corrupt-entry deletion, and cache-key memoization outside this task.