A witness is allowed to refresh his memory, respecting a fact, by anything written by himself, or under his direction, at the time when the fact occurred or immediately thereafter, or at any other time when the fact was fresh in his memory and he knew that the same was correctly stated in the writing. In either case the writing must be produced, and may be inspected by the adverse party, who may cross-examine the witness upon it. A witness may testify from such a writing, though he retains no recollection of the particular facts; but such evidence shall be received with caution.