跳到内容 跳到搜索

Active Storage 镜像服务

封装一组镜像服务,并提供单个 ActiveStorage::Service 对象,所有文件都将上传到该对象。一个 primary 服务被指定用来回答对

  • 下载

  • 存在?

  • 网址

  • 直接上传网址

  • 直接上传的头部

方法
D
M
N
U

属性

[R] 镜像
[R] 主要的

类公共方法

new(primary:, mirrors:)

# File activestorage/lib/active_storage/service/mirror_service.rb, line 31
def initialize(primary:, mirrors:)
  @primary, @mirrors = primary, mirrors
  @executor = Concurrent::ThreadPoolExecutor.new(
    min_threads: 1,
    max_threads: mirrors.size,
    max_queue: 0,
    fallback_policy: :caller_runs,
    idle_time: 60
  )
end

实例公共方法

delete(key)

删除所有服务上的 key 处的文件。

# File activestorage/lib/active_storage/service/mirror_service.rb, line 52
def delete(key)
  perform_across_services :delete, key
end

delete_prefixed(prefix)

删除所有服务上以 prefix 开头的键处的文件。

# File activestorage/lib/active_storage/service/mirror_service.rb, line 57
def delete_prefixed(prefix)
  perform_across_services :delete_prefixed, prefix
end

mirror(key, checksum:)

key 处的文件从主服务复制到每个镜像,其中该文件尚不存在。

# File activestorage/lib/active_storage/service/mirror_service.rb, line 66
def mirror(key, checksum:)
  instrument :mirror, key: key, checksum: checksum do
    if (mirrors_in_need_of_mirroring = mirrors.select { |service| !service.exist?(key) }).any?
      primary.open(key, checksum: checksum) do |io|
        mirrors_in_need_of_mirroring.each do |service|
          io.rewind
          service.upload key, io, checksum: checksum
        end
      end
    end
  end
end

upload(key, io, checksum: nil, **options)

io 上传到所有服务上指定的 key。对主服务的上传是同步进行的,而对镜像的上传是异步进行的。如果提供了 checksum,则所有服务都会在上传完成后确保匹配,否则会引发 ActiveStorage::IntegrityError

# File activestorage/lib/active_storage/service/mirror_service.rb, line 45
def upload(key, io, checksum: nil, **options)
  io.rewind
  primary.upload key, io, checksum: checksum, **options
  mirror_later key, checksum: checksum
end