Back to Pagy

count only once and memoize for all pages

docs/toolbox/paginators/countish.md

43.5.31.5 KB
Original Source

:icon-move-to-end:  :countish


:countish is an OFFSET paginator that memoizes the result of the COUNT query in the page links, running it only once per collection (instead of once per page), optionally recounting it when it's stale.

It fully supports all the helpers and navigators.

=== :icon-tools:  Usage

ruby
# count only once and memoize for all pages
@pagy, @records = pagy(:countish, collection, **options)
# count once, memoize, and recount when the memoized count is older than ttl
@pagy, @records = pagy(:countish, collection, ttl: 300, **options)
  • @pagy is the pagination instance. It provides the readers and the helpers to use in your code.
  • @records represents the paginated collection of records for the page (lazy-loaded records).
  • :ttl is the Time To Live of the memoized count. Counts only once if nil.

==- :icon-sliders:  Options

ttl: 180 : A positive number of seconds enables recounting. Set to nil (falsey) queries the DB for the COUNT only once, and reuse it for all the other pages served.

!!!warning Recounting/TTL

  • Recounting gets the user more precise info and minimizes the page differences with lengthy page-browsing and abundant DB insertions/deletions.
    • It does not fix the OFFSET-intrinsic "drift" of records on active DB insertions/deletions.

{{ include "options/offset" }}

==- :icon-mention:  Readers

{{ include "snippets/offset-readers" }}

===