Giter VIP home page Giter VIP logo

webcache's Introduction

WebCache

WebView 缓存管理,提升网页打开速度。

在 WebView 请求网络资源前,它会先依次查询以下资源,没有命中时才从网络加载。

  • 资源包,将资源包解压到缓存目录,再从中加载资源
    • 离线资源包,直接打包到APK里
    • 动态资源包,需要动态预加载
  • 离线资源,直接从assets目录加载资源
  • HTTP缓存,使用Okhttp的代替WebView的缓存管理

使用

// 初始化
WebCacheManager.init(this)


// 加载离线包,离线包直接打包到APK里,放在`assets`目录,在客户端做映射配置 
// 资源包加载时被解压到缓存目录 
// 请求关联路径下的资源时,会先在缓存目录里寻找对应资源,未命中时再交由WebView请求网络资源
val localBundles = mapOf(
    // 在WebView中请求 http://a.com/local/ 路径下的资源时会先偿试在`assets`目录的 local.zip 中查找
    "http://a.com/local/" to "local.zip"
)
WebCacheManager.loadBundles(localBundles)


// 加载动态资源包,可覆盖离线包,服务端动态下发,在使用前预加载 
// 从给定链接下载资源资源包并解压到缓存目录
// 请求关联路径下的资源时,会先在缓存目录里寻找对应资源,未命中时再交由WebView请求网络资源
val remoteBundles = listOf(
    BundleItem("http://a.com/remote/", "http://a.com/remote.zip", "XXXXXX")
)
WebCacheManager.loadBundles(remoteBundles)


// 离线资源,将给定路径映射到`assets`下的子目录 
// 请求关联路径下的资源时,会先在`assets`下对应的子目录里寻找资源,未命中时再交由WebView请求网络资源
val assetPaths = mapOf(
    // 请求 http://a.com/waoh/ 路径下的资源时会先偿试在`assets/waoh`目录下查找
    "http://a.com/waoh/" to "waoh"
)
WebCacheManager.addAssetPaths(assetPaths)


// 使用Okhttp的代替WebView的请求给定路径下的资源,使用Okhttp的缓存管理
WebCacheManager.addCacheUrls("http://example.com/a/", "http://example.com/b/")


// 拦截资源的请求并返回缓存资源
web.webViewClient = object : WebViewClient() {
    override fun shouldInterceptRequest(view: WebView, request: WebResourceRequest): WebResourceResponse? {
        return WebCacheManager.get(request)
    }
}

Gradle

repositories { 
    maven { url "https://gitee.com/ezy/repo/raw/cosmo/"}
} 
dependencies {
    implementation "me.reezy.cosmo:webcache:0.7.0"
}

LICENSE

The Component is open-sourced software licensed under the Apache license.

webcache's People

Contributors

czy1121 avatar

Stargazers

gitzjy avatar mahongyin avatar xwcc avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.