Skip to content

Commit

Permalink
Add cache service
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Oct 31, 2024
1 parent f87b3c8 commit 8fffa66
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 189 deletions.
319 changes: 146 additions & 173 deletions Gemfile.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions app/services/billable_metrics/destroy_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def initialize(metric:)
def call
return result.not_found_failure!(resource: 'billable_metric') unless metric

BillableMetrics::ExpressionCacheService.expire_cache(metric.organization.id, metric.code)

draft_invoice_ids = Invoice.draft.joins(plans: [:billable_metrics])
.where(billable_metrics: {id: metric.id}).distinct.pluck(:id)

Expand Down
2 changes: 1 addition & 1 deletion app/services/billable_metrics/expression_cache_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(organization_id, billable_metric_code)
@organization_id = organization_id
@billable_metric_code = billable_metric_code

super(nil)
super
end

def cache_key
Expand Down
2 changes: 1 addition & 1 deletion app/services/billable_metrics/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def call
billable_metric.expression = params[:expression] if params.key?(:expression)

if params.key?(:expression) || params.key?(:field_name)
Rails.cache.delete("expression/#{organization.id}/#{billable_metric.code}")
BillableMetrics::ExpressionCacheService.expire_cache(organization.id, billable_metric.code)
end
end

Expand Down
17 changes: 13 additions & 4 deletions app/services/cache_service.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
# frozen_string_literal: true

class CacheService < BaseService
def initialize(*, expires_in: nil)
@expires_in = expires_in
super(nil)
end

def self.expire_cache(*, **)
new(*, **).expire_cache
end

def cache_key
raise NotImplementedError
end

def call(&)
fetch(&)
Rails.cache.fetch(cache_key, expires_in:, &)
end

def fetch(&)
Rails.cache.fetch(cache_key, &)
end
private

attr_reader :expires_in

def expire_cache
Rails.cache.delete(cache_key)
Expand Down
2 changes: 1 addition & 1 deletion app/services/events/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def call
attr_reader :organization, :params, :timestamp, :metadata

def pre_ingest(event)
field_name, expression = Rails.cache.fetch("expression/#{organization.id}/#{event.code}") do
field_name, expression = BillableMetrics::ExpressionCacheService.call(organization.id, event.code) do
bm = organization.billable_metrics.with_expression.find_by(code: event.code)
[bm&.field_name, bm&.expression]
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/events/post_process_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def expire_cached_charges(subscriptions)
charge_filter = ChargeFilters::EventMatchingService.call(charge:, event:).charge_filter

active_subscription.each do |subscription|
Subscriptions::ChargeCacheService.new(subscription:, charge:, charge_filter:).expire_cache
Subscriptions::ChargeCacheService.expire_cache(subscription:, charge:, charge_filter:)
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions app/services/subscriptions/charge_cache_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(subscription:, charge:, to_datetime:, cache: true)
def call(charge_filter:)
return yield unless cache

json = Rails.cache.fetch(cache_key(charge_filter), expires_in: cache_expiration) do
json = Subscriptions::ChargeCacheService.call(subscription:, charge:, charge_filter:, expires_in: cache_expiration) do
yield.to_json
end

Expand All @@ -23,10 +23,6 @@ def call(charge_filter:)

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
Expand Down
8 changes: 5 additions & 3 deletions app/services/subscriptions/charge_cache_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ def self.expire_for_subscription(subscription)

def self.expire_for_subscription_charge(subscription:, charge:)
charge.filters.each do |filter|
new(subscription:, charge:, charge_filter: filter).expire_cache
expire_cache(subscription:, charge:, charge_filter: filter)
end

new(subscription:, charge:).expire_cache
expire_cache(subscription:, charge:)
end

def initialize(subscription:, charge:, charge_filter: nil)
def initialize(subscription:, charge:, charge_filter: nil, expires_in: nil)
@subscription = subscription
@charge = charge
@charge_filter = charge_filter

super(expires_in:)
end

# IMPORTANT
Expand Down

0 comments on commit 8fffa66

Please sign in to comment.