简体   繁体   English

如何让 Glide 保持连接?

[英]How to make Glide keep connection alive?

I am using the Android Glide library to load images remotely.我正在使用 Android Glide 库远程加载图像。

The image URL protocol is https so there is a performance implication due to TSL every time a new connection is established.图像 URL 协议是https ,因此每次建立新连接时都会因 TSL 而产生性能影响。

I'm using the okhttp integration:我正在使用 okhttp 集成:

def glide_version = "4.10.0"
implementation "com.github.bumptech.glide:glide:$glide_version"
implementation "com.github.bumptech.glide:okhttp3-integration:$glide_version"

Is it possible to make Glide keep the connection alive for better performance?是否可以让 Glide 保持连接状态以获得更好的性能?

Try setting up an HTTP client with a custom ConnectionPool.尝试使用自定义 ConnectionPool 设置 HTTP 客户端。 Take a look at the connection pool's constructor:看一下连接池的构造函数:

public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit)

So you can try something like:因此,您可以尝试以下操作:

@GlideModule
class YourGlideModule : AppGlideModule() {
    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry
                .replace(
                        GlideUrl::class.java,
                        InputStream::class.java,
                        OkHttpUrlLoader.Factory(
                                OkHttpClient.Builder()
                                        .connectionPool(ConnectionPool(5, 5, TimeUnit.MINUTES))
                                        .build()))
    }
}

Those are default values, but you can find the ones that suit you.这些是默认值,但您可以找到适合您的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM