Caching page collections is difficult. The usual trick of using collection counting and max updated_at is mostly not applicable!
As you said, counting a collection is so useless if you do not allow dynamic per_page values.
The latest updated_at entirely up to you sort your collection.
Imagine adding a new entry and ending on the first page. This means that one entry, previously page 1, now enters page 2. One previous page 2 now becomes page 3. If new record 2 is not updated more recently than the previous max, the cache key remains unchanged, but the collection is not! The same thing happens when a record is deleted.
Only if you can guarantee that new entries always end on the last page and that no entries are ever deleted, using max updated_at will be a reliable way.
As a solution, you can include the total number of entries and the total number of max updated_at in the cache key in addition to the page number and per page value. This will require additional queries, but may be worth it depending on the database configuration and the number of records.
Another solution is to use a key that takes into account some reduced form of the actual contents of the collection. For example, also considering all record identifiers.
If you use postgres as a database, this stone can help you, although I have never used it myself. https://github.com/cmer/scope_cache_key
And rails 4 forks: https://github.com/joshblour/scope_cache_key
kogre
source share