B-62418 Fixed bug causing user rate limits to expire in the cache

Also added some tests and cleaned up some code.