Sha256: aac584f1a2208596993dad2d96bde2723154ebb4d58a1d70a4072f77ddb7a786

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

##
# Aspect that provide cache for the endpoints.
module CacheAspect
  def call_endpoint(cache, *args)
    return super(*args) unless !cache[:cache].nil? && cache[:endpoints_to_cache]&.include?(args[0])

    cache_filtered_name = cache_name_filter(args[1], cache[:cached_methods][args[0]])

    cache[:cache].mutex.synchronize do
      return cache[:cache].cache[cache_filtered_name][0] unless cache[:cache].cache[cache_filtered_name].nil?

      response = super(*args)
      cache[:cache].cache[cache_filtered_name] = [response, Time.now] if should_cache_response?(response[1])
      response
    end
  end

  private

  def cache_name_filter(client_data, cached_methods_params)
    filtered_headers = client_data[:headers]&.filter { |key, _value| cached_methods_params&.include?(key) }
    filtered_params = client_data[:params]&.filter { |key, _value| cached_methods_params&.include?(key) }
    [{ params: filtered_params, headers: filtered_headers }].to_s.to_sym
  end

  def should_cache_response?(status)
    (200..299).include?(status.to_i)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
macaw_framework-1.3.21 lib/macaw_framework/aspects/cache_aspect.rb
macaw_framework-1.3.1 lib/macaw_framework/aspects/cache_aspect.rb
macaw_framework-1.3.0 lib/macaw_framework/aspects/cache_aspect.rb
macaw_framework-1.2.6 lib/macaw_framework/aspects/cache_aspect.rb
macaw_framework-1.2.5 lib/macaw_framework/aspects/cache_aspect.rb