Sha256: 51fc19374610268b0561f3b0fdaa7adb698fc0ac614f278e9ba79048ed311cc9

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

module NCore
  module Client::Cache
    extend ActiveSupport::Concern

    module ClassMethods

      private

      # only caches GET requests with 200..299, 409, 422 responses
      # cache_opts: true
      #   use *::Api.cache_store
      # cache_opts: {...}
      #   use: *::Api.cache_store, with options: {...}
      #   hint: add  force: true  to execute the query and rewrite the cache
      # cache_opts: Store.new
      #   use Store.new as-is
      def execute_request(req, cache_opts=nil)
        case cache_opts
        when true
          store, cache_opts = cache_store, {}
        when Hash
          store, cache_opts = cache_store, cache_opts.symbolize_keys
        when nil, false
          store = false
        else
          store, cache_opts = cache_opts, {}
        end

        if store && req[:method] == :get
          store.fetch request_cache_key(**req.slice(:url, :headers)), cache_opts do
            super
          end
        else
          super
        end
      end


      def request_cache_key(url:, headers:)
        hdrs = headers.reject{|k,v| k=='x-request-id'}.sort
        [ 'ncore',
          url.gsub(/[^a-zA-Z0-9]+/,'-'),
          Digest::MD5.hexdigest(hdrs.to_s)
        ].join(':')
      end

    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ncore-3.12.0 lib/ncore/client_cache.rb
ncore-3.11.0 lib/ncore/client_cache.rb
ncore-3.10.0 lib/ncore/client_cache.rb
ncore-3.9.1 lib/ncore/client_cache.rb
ncore-3.9.0 lib/ncore/client_cache.rb
ncore-3.8.1 lib/ncore/client_cache.rb
ncore-3.8.0 lib/ncore/client_cache.rb
ncore-3.7.1 lib/ncore/client_cache.rb
ncore-3.7.0 lib/ncore/client_cache.rb