class Bundler::CompactIndexClient::Cache - bundler: Ruby Standard Library Documentation

# class Bundler::CompactIndexClient::Cache

### Attributes

directory[R]

### Public Class Methods

new(directory, fetcher = nil) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 10
    def initialize(directory, fetcher = nil)
      @directory = Pathname.new(directory).expand_path
      @updater = Updater.new(fetcher) if fetcher
      @mutex = Thread::Mutex.new
      @endpoints = Set.new

      @info_root = mkdir("info")
      @special_characters_info_root = mkdir("info-special-characters")
      @info_etag_root = mkdir("info-etags")
    end

### Public Instance Methods

info(name, remote_checksum = nil) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 29
    def info(name, remote_checksum = nil)
      path = info_path(name)

      if remote_checksum && remote_checksum != SharedHelpers.checksum_for_file(path, :MD5)
        fetch("info/#{name}", path, info_etag_path(name))
      else
        Bundler::CompactIndexClient.debug { "update skipped info/#{name} (#{remote_checksum ? "versions index checksum is nil" : "versions index checksum matches local"})" }
        read(path)
      end
    end

names() click to toggle source

    # File bundler/compact_index_client/cache.rb, line 21
    def names
      fetch("names", names_path, names_etag_path)
    end

reset!() click to toggle source

    # File bundler/compact_index_client/cache.rb, line 40
    def reset!
      @mutex.synchronize { @endpoints.clear }
    end

versions() click to toggle source

    # File bundler/compact_index_client/cache.rb, line 25
    def versions
      fetch("versions", versions_path, versions_etag_path)
    end

### Private Instance Methods

already_fetched?(remote_path) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 86
    def already_fetched?(remote_path)
      @mutex.synchronize { !@endpoints.add?(remote_path) }
    end

fetch(remote_path, path, etag_path) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 75
    def fetch(remote_path, path, etag_path)
      if already_fetched?(remote_path)
        Bundler::CompactIndexClient.debug { "already fetched #{remote_path}" }
      else
        Bundler::CompactIndexClient.debug { "fetching #{remote_path}" }
        @updater&.update(remote_path, path, etag_path)
      end

      read(path)
    end

info_etag_path(name) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 62
    def info_etag_path(name)
      name = name.to_s
      @info_etag_root.join("#{name}-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}")
    end

info_path(name) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 51
    def info_path(name)
      name = name.to_s
      # TODO: converge this into the info_root by hashing all filenames like info_etag_path
      if /[^a-z0-9_-]/.match?(name)
        name += "-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}"
        @special_characters_info_root.join(name)
      else
        @info_root.join(name)
      end
    end

mkdir(name) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 67
    def mkdir(name)
      directory.join(name).tap do |dir|
        SharedHelpers.filesystem_access(dir) do
          FileUtils.mkdir_p(dir)
        end
      end
    end

names_etag_path(= directory.join("names.etag")) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 47
          def names_etag_path = directory.join("names.etag")
          def versions_path = directory.join("versions")
          def versions_etag_path = directory.join("versions.etag")

          def info_path(name)
            name = name.to_s
            # TODO: converge this into the info_root by hashing all filenames like info_etag_path
            if /[^a-z0-9_-]/.match?(name)
              name += "-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}"
              @special_characters_info_root.join(name)
            else
              @info_root.join(name)
            end
          end

          def info_etag_path(name)
            name = name.to_s
            @info_etag_root.join("#{name}-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}")
          end

          def mkdir(name)
            directory.join(name).tap do |dir|
              SharedHelpers.filesystem_access(dir) do
                FileUtils.mkdir_p(dir)
              end
            end
          end

          def fetch(remote_path, path, etag_path)
            if already_fetched?(remote_path)
              Bundler::CompactIndexClient.debug { "already fetched #{remote_path}" }
            else
              Bundler::CompactIndexClient.debug { "fetching #{remote_path}" }
              @updater&.update(remote_path, path, etag_path)
            end

            read(path)
          end

          def already_fetched?(remote_path)
            @mutex.synchronize { !@endpoints.add?(remote_path) }
          end

          def read(path)
            return unless path.file?
            SharedHelpers.filesystem_access(path, :read, &:read)
          end
        end
      end
    end

names_path(= directory.join("names")) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 46
        def names_path = directory.join("names")
        def names_etag_path = directory.join("names.etag")
        def versions_path = directory.join("versions")
        def versions_etag_path = directory.join("versions.etag")

        def info_path(name)
          name = name.to_s
          # TODO: converge this into the info_root by hashing all filenames like info_etag_path
          if /[^a-z0-9_-]/.match?(name)
            name += "-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}"
            @special_characters_info_root.join(name)
          else
            @info_root.join(name)
          end
        end

        def info_etag_path(name)
          name = name.to_s
          @info_etag_root.join("#{name}-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}")
        end

        def mkdir(name)
          directory.join(name).tap do |dir|
            SharedHelpers.filesystem_access(dir) do
              FileUtils.mkdir_p(dir)
            end
          end
        end

        def fetch(remote_path, path, etag_path)
          if already_fetched?(remote_path)
            Bundler::CompactIndexClient.debug { "already fetched #{remote_path}" }
          else
            Bundler::CompactIndexClient.debug { "fetching #{remote_path}" }
            @updater&.update(remote_path, path, etag_path)
          end

          read(path)
        end

        def already_fetched?(remote_path)
          @mutex.synchronize { !@endpoints.add?(remote_path) }
        end

        def read(path)
          return unless path.file?
          SharedHelpers.filesystem_access(path, :read, &:read)
        end
      end
    end

read(path) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 90
    def read(path)
      return unless path.file?
      SharedHelpers.filesystem_access(path, :read, &:read)
    end

versions_etag_path(= directory.join("versions.etag")) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 49
      def versions_etag_path = directory.join("versions.etag")

      def info_path(name)
        name = name.to_s
        # TODO: converge this into the info_root by hashing all filenames like info_etag_path
        if /[^a-z0-9_-]/.match?(name)
          name += "-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}"
          @special_characters_info_root.join(name)
        else
          @info_root.join(name)
        end
      end

      def info_etag_path(name)
        name = name.to_s
        @info_etag_root.join("#{name}-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}")
      end

      def mkdir(name)
        directory.join(name).tap do |dir|
          SharedHelpers.filesystem_access(dir) do
            FileUtils.mkdir_p(dir)
          end
        end
      end

      def fetch(remote_path, path, etag_path)
        if already_fetched?(remote_path)
          Bundler::CompactIndexClient.debug { "already fetched #{remote_path}" }
        else
          Bundler::CompactIndexClient.debug { "fetching #{remote_path}" }
          @updater&.update(remote_path, path, etag_path)
        end

        read(path)
      end

      def already_fetched?(remote_path)
        @mutex.synchronize { !@endpoints.add?(remote_path) }
      end

      def read(path)
        return unless path.file?
        SharedHelpers.filesystem_access(path, :read, &:read)
      end
    end

versions_path(= directory.join("versions")) click to toggle source

    # File bundler/compact_index_client/cache.rb, line 48
        def versions_path = directory.join("versions")
        def versions_etag_path = directory.join("versions.etag")

        def info_path(name)
          name = name.to_s
          # TODO: converge this into the info_root by hashing all filenames like info_etag_path
          if /[^a-z0-9_-]/.match?(name)
            name += "-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}"
            @special_characters_info_root.join(name)
          else
            @info_root.join(name)
          end
        end

        def info_etag_path(name)
          name = name.to_s
          @info_etag_root.join("#{name}-#{SharedHelpers.digest(:MD5).hexdigest(name).downcase}")
        end

        def mkdir(name)
          directory.join(name).tap do |dir|
            SharedHelpers.filesystem_access(dir) do
              FileUtils.mkdir_p(dir)
            end
          end
        end

        def fetch(remote_path, path, etag_path)
          if already_fetched?(remote_path)
            Bundler::CompactIndexClient.debug { "already fetched #{remote_path}" }
          else
            Bundler::CompactIndexClient.debug { "fetching #{remote_path}" }
            @updater&.update(remote_path, path, etag_path)
          end

          read(path)
        end

        def already_fetched?(remote_path)
          @mutex.synchronize { !@endpoints.add?(remote_path) }
        end

        def read(path)
          return unless path.file?
          SharedHelpers.filesystem_access(path, :read, &:read)
        end
      end
    end