\$\begingroup\$ Caches are harder to implement correctly then it would first appear. Unless you have a compelling reason to do so, strongly consider an existing implementation. \$\endgroup\$ – Eric Stein Dec 29 '15 at 4:11