Sha256: 17bbfe3a5f4590b0d52fff592bf40f2b81ce6d71312abe3cb87b12c2b9c52476

Contents?: true

Size: 1.66 KB

Versions: 50

Compression:

Stored size: 1.66 KB

Contents

module ActiveFedora
  class CachingConnection < Ldp::Client
    def initialize(host, options = {})
      super
      @cache = {}
      @cache_enabled = false
    end

    def get(url, options = {})
      if @cache_enabled
        cache_resource(url) { super }
      else
        super
      end
    end

    def post(*)
      clear_cache if @cache_enabled
      super
    end

    def put(*)
      clear_cache if @cache_enabled
      super
    end

    def patch(*)
      clear_cache if @cache_enabled
      super
    end

    # Enable the cache within the block.
    def cache
      old = @cache_enabled
      @cache_enabled = true
      yield
    ensure
      @cache_enabled = old
      clear_cache unless @cache_enabled
    end

    def enable_cache!
      @cache_enabled = true
    end

    def disable_cache!
      @cache_enabled = false
    end

    # Disable the query cache within the block.
    def uncached
      old = @cache_enabled
      @cache_enabled = false
      yield
    ensure
      @cache_enabled = old
    end

    def clear_cache
      @cache.clear
    end

    private

      def log(url)
        ActiveSupport::Notifications.instrument("ldp.active_fedora",
                                                id: url, name: "Load LDP", ldp_service: object_id) { yield }
      end

      def cache_resource(url, &_block)
        result =
          if @cache.key?(url)
            ActiveSupport::Notifications.instrument("ldp.active_fedora",
                                                    id: url, name: "CACHE", ldp_service: object_id)
            @cache[url]
          else
            @cache[url] = log(url) { yield }
          end
        result.dup
      end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
active-fedora-11.5.6 lib/active_fedora/caching_connection.rb
active-fedora-11.2.1 lib/active_fedora/caching_connection.rb
active-fedora-11.5.5 lib/active_fedora/caching_connection.rb
active-fedora-11.5.4 lib/active_fedora/caching_connection.rb
active-fedora-11.5.3 lib/active_fedora/caching_connection.rb
active-fedora-11.5.2 lib/active_fedora/caching_connection.rb
active-fedora-11.5.0 lib/active_fedora/caching_connection.rb
active-fedora-11.4.1 lib/active_fedora/caching_connection.rb
active-fedora-11.4.0 lib/active_fedora/caching_connection.rb
active-fedora-11.3.1 lib/active_fedora/caching_connection.rb
active-fedora-11.3.0 lib/active_fedora/caching_connection.rb
active-fedora-11.2.0 lib/active_fedora/caching_connection.rb
active-fedora-11.1.6 lib/active_fedora/caching_connection.rb
active-fedora-11.1.5 lib/active_fedora/caching_connection.rb
active-fedora-11.1.4 lib/active_fedora/caching_connection.rb
active-fedora-11.1.3 lib/active_fedora/caching_connection.rb
active-fedora-11.1.2 lib/active_fedora/caching_connection.rb
active-fedora-11.1.1 lib/active_fedora/caching_connection.rb
active-fedora-11.1.0 lib/active_fedora/caching_connection.rb
active-fedora-10.3.0 lib/active_fedora/caching_connection.rb