Sha256: d999f0f359328daad03b6941b9f09198eaf2beb700241ca367412764a736fa34

Contents?: true

Size: 1.33 KB

Versions: 24

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module IronBank
  # Use the provided cache if present.
  #
  module Cacheable
    def reload
      remove_instance_vars
      @remote = self.class.find(id, force: true).remote
      self
    end

    private

    def remove_instance_vars
      # Substract predefined variables from the instance variables
      (instance_variables - [:@remote]).each do |var|
        remove_instance_variable(:"#{var}")
      end
    end

    def cache
      self.class.cache
    end

    # Override queryable class methods to use cache if present.
    #
    module ClassMethods
      def find(id, force: false)
        return super(id) unless cache

        cache.fetch(id, force: force) { super(id) }
      end

      def where(conditions,
                limit: IronBank::Actions::Query::DEFAULT_ZUORA_LIMIT)
        # Conditions can be empty when called from #all, it does not make sense
        # to try to cache all records returned then.
        return super if conditions.empty?

        return super unless cache

        # Ensure we are not colliding when the conditions are similar for two
        # different resources, like Account and Subscription.
        cache_key = conditions.merge(object_name: name)
        cache.fetch(cache_key) { super }
      end

      def cache
        IronBank.configuration.cache
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
iron_bank-5.4.1 lib/iron_bank/cacheable.rb
iron_bank-5.4.0 lib/iron_bank/cacheable.rb
iron_bank-5.3.2 lib/iron_bank/cacheable.rb
iron_bank-5.3.0 lib/iron_bank/cacheable.rb
iron_bank-5.2.6 lib/iron_bank/cacheable.rb
iron_bank-5.2.4 lib/iron_bank/cacheable.rb
iron_bank-5.2.3 lib/iron_bank/cacheable.rb
iron_bank-5.2.0 lib/iron_bank/cacheable.rb
iron_bank-5.1.1 lib/iron_bank/cacheable.rb
iron_bank-5.1.0 lib/iron_bank/cacheable.rb
iron_bank-4.4.3 lib/iron_bank/cacheable.rb
iron_bank-5.0.1 lib/iron_bank/cacheable.rb
iron_bank-5.0.0 lib/iron_bank/cacheable.rb
iron_bank-4.4.1 lib/iron_bank/cacheable.rb
iron_bank-4.4.0 lib/iron_bank/cacheable.rb
iron_bank-4.3.1 lib/iron_bank/cacheable.rb
iron_bank-4.3.0 lib/iron_bank/cacheable.rb
iron_bank-4.2.0 lib/iron_bank/cacheable.rb
iron_bank-4.1.1 lib/iron_bank/cacheable.rb
iron_bank-4.1.0 lib/iron_bank/cacheable.rb