This function gets all the Media ID's from the database whose names begins with a specific string. I'm using this for an array of default images. You can see I'm using both drupal_static() and \Drupal::cache(). Necessary? I would bet \Drupal::cache() goes to the database to ask for information (unless you're using Memcache or Redis, that is), and thus in a view with multiple content listings calling this function drupal_static() will be faster, because that saves to a PHP variable. Overkill for a little query like this? OK, sue me. I love performance. Here's the code…
Read more about Drupal EntityFieldQuery, drupal_static(), \Drupal::cache() Example