MDL-39450 course formats: add static cache to get_format_or_default

This function gets called hundreds of times on every page, often with
exactly the same input, so I added a static cache.