How to cache a paged list? I have a list which is built by consuming an API. The data that comes from response is not stored in Drupal which mean I am using views to create the list. The list is a paged one which shows 10 items at every page. How can cache each page of the list?

What I am thinking of is setting the cache name with a variable of the page.

\Drupal::cache()->set('cache_demo_page_'.$page, $posts, CacheBackendInterface::CACHE_PERMANENT);

This will lead to a lot of cache entries as there will be 100s of such pages in many lists across the site.

What should be the best way to do this?
Yes, having hundreds of pages also results in hundreds of caches. Not just for your cache, but also for any block that varies per URL.

That's how it is, no way around it. Except not caching at all.

Note that with remote content, you need to think about cache invalidation (Well, you need to think about that anyway, but it's different with reomote content). You have two options: 


Use a short cache expiration time. That also helps with the amount of cache entries, as they will get deleted when they are expired
Or you need to find a way to get notified when changes happen in the remote system or check for changes in a background process to invalidate your cache entries.