-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc(usage): Scope usage caching to the charge filter level
- Loading branch information
1 parent
c4fed78
commit 9f957be
Showing
8 changed files
with
228 additions
and
155 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
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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
module Subscriptions | ||
class ChargeCacheMiddleware | ||
def initialize(subscription:, charge:, to_datetime:, cache: true) | ||
@subscription = subscription | ||
@charge = charge | ||
@to_datetime = to_datetime | ||
@cache = cache | ||
end | ||
|
||
def call(charge_filter:) | ||
return yield unless cache | ||
|
||
json = Rails.cache.fetch(cache_key(charge_filter), expires_in: cache_expiration) do | ||
yield.to_json | ||
end | ||
|
||
JSON.parse(json).map { |j| Fee.new(j.slice(*Fee.column_names)) } | ||
end | ||
|
||
private | ||
|
||
attr_reader :subscription, :charge, :to_datetime, :cache | ||
|
||
def cache_key(charge_filter) | ||
Subscriptions::ChargeCacheService.new(subscription:, charge:, charge_filter:).cache_key | ||
end | ||
|
||
def cache_expiration | ||
(to_datetime - Time.current).to_i.seconds | ||
end | ||
end | ||
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
Oops, something went wrong.