Implement Document.links cache

Closes #1885.