We present a quantum memory protocol for photons that is based on the direct control of the transition dipole moment. We focus on the case where the light-matter interaction is enhanced by a cavity. We show that the optimal write process (maximizing the storage efficiency) is related to the optimal read process by a reversal of the effective time τ = ∫ dtg(t)/κ, where g(t) is the timedependent coupling and κ is the cavity decay rate. We discuss the implementation of the protocol in a rare-earth ion doped crystal, where an optical transition can be turned on and off by switching a magnetic field.