-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Implement keyset_cached (unhandled cache in session)
- Loading branch information
Showing
6 changed files
with
133 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# See the Pagy documentation: https://ddnexus.github.io/pagy/docs/extras/keyset/cached | ||
# frozen_string_literal: true | ||
|
||
require_relative '../keyset/cached' | ||
|
||
class Pagy # :nodoc: | ||
# Add keyset pagination | ||
module KeysetCachedExtra | ||
private | ||
|
||
# Return Pagy::Keyset::Cached object and paginated records | ||
def pagy_keyset_cached(set, **vars) | ||
vars[:cache] ||= pagy_keyset_cached_hash(vars) | ||
vars[:page] ||= pagy_get_page(vars) # numeric page | ||
vars[:limit] ||= pagy_get_limit(vars) | ||
pagy = Keyset::Cached.new(set, **vars) | ||
pagy.finalize | ||
[pagy, pagy.records] | ||
end | ||
|
||
# Return the cached hash | ||
def pagy_keyset_cached_hash(vars) | ||
key = "pagy-keyset-#{B64.encode(params.slice(vars.delete(:key_params)).to_json)}" | ||
puts key | ||
session[key] ||= {} | ||
end | ||
end | ||
Backend.prepend KeysetCachedExtra | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# See Pagy::Countless API documentation: https://ddnexus.github.io/pagy/docs/api/keyset/cached | ||
# frozen_string_literal: true | ||
|
||
require_relative '../keyset' | ||
|
||
class Pagy # :nodoc: | ||
# No need to know the count to paginate | ||
class Keyset | ||
class Cached < Keyset | ||
Check failure on line 9 in gem/lib/pagy/keyset/cached.rb GitHub Actions / Ruby 3.2 Test
|
||
|
||
Check failure on line 10 in gem/lib/pagy/keyset/cached.rb GitHub Actions / Ruby 3.2 Test
|
||
def self.new(set, **vars) | ||
allocate.tap do |instance| | ||
instance.instance_variable_set(:@cache, vars[:cache]) | ||
instance.send(:initialize, set, **vars) | ||
end | ||
end | ||
|
||
# Override adding default variables required by UI | ||
def default | ||
{ **super, **DEFAULT.slice(:ends, :page, :size) } | ||
end | ||
|
||
# Override the assign_cursor | ||
def assign_cursor | ||
@cursor = @cache[@page] | ||
end | ||
|
||
# Return the next page from cache or chache the next_page/next_cursor pair | ||
# It must be called before finalize | ||
def next | ||
next_page = @page + 1 | ||
records | ||
return if !@more || (@vars[:max_pages] && @last > vars[:max_pages]) | ||
|
||
@cache[next_page] = @cache[next_page] || next_cursor | ||
next_page | ||
end | ||
|
||
# Finalize the instance variables based on the fetched size | ||
def finalize | ||
# Ensure next is called, so we know the actual last page | ||
@next = self.next | ||
@prev = (@page - 1 unless @page == 1) | ||
@last = @cache.keys.last | ||
raise OverflowError.new(self, :page, "in 1..#{@last}", @page) if @page > @last | ||
|
||
@in = @records.size | ||
@offset = @limit * (@page - 1) # may not be accurate | ||
@from = @in.zero? ? 0 : @offset + 1 | ||
@to = @offset + @in | ||
self | ||
end | ||
end | ||
end | ||
|
||
module SeriesOverride | ||
Check failure on line 56 in gem/lib/pagy/keyset/cached.rb GitHub Actions / Ruby 3.2 Test
|
||
# Override the original series. | ||
# Return nil if :keyset_numeric_minimal is enabled | ||
def series(**) | ||
super unless @vars[:keyset_numeric_minimal] | ||
end | ||
end | ||
prepend SeriesOverride | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters