乐于分享
好东西不私藏

Android人狂喜缓存秒提APP性能

Android人狂喜缓存秒提APP性能

📦 Android 缓存实战:结合真实场景与代码,全面提升应用性能

缓存是 Android 开发中至关重要的技术,能够有效提升应用性能、减少网络流量消耗、优化用户体验。本文将深入讲解以下内容:

  • 缓存的基本概念
  • Android 中的常见缓存类型
  • 真实业务场景实战
  • 基于现代开发库(Jetpack、Retrofit、Room、Coil 等)的代码示例
  • 缓存最佳实践

🧠 什么是缓存?

缓存是指将数据临时存储起来,以便后续请求同一份数据时可以更快响应的技术。
举个例子:开发新闻类应用时,不需要每次用户打开 App 都发起网络请求拉取文章,可以把文章缓存在本地,仅在必要时刷新内容即可。

🛠 Android 中的缓存类型

缓存类型 适用场景 存储位置
内存缓存(In-Memory Cache) 访问速度最快,适合存储图片、UI 状态等临时数据 RAM
磁盘缓存(Disk Cache) 支持离线访问,常见于 Retrofit/OkHttp 场景 文件系统
数据库缓存(Database Cache) 结构化的离线数据存储,典型实现为 Room SQLite
SharedPreferences 存储简单的键值对数据,比如应用设置项 XML 文件

🛠 真实业务场景:带 API 缓存的新闻应用

需求痛点
假设你正在开发一款从 REST API 拉取文章的新闻应用,需要满足以下要求:

  • 在线时优先加载最新内容
  • 避免不必要的 API 请求,节省流量
  • 离线时可以正常展示已缓存的内容

✅ 解决方案设计

  1. 使用 Retrofit + OkHttp 处理网络请求
  2. 基于 OkHttp 开启磁盘缓存
  3. 将拉取到的结构化数据存入 Room 数据库
  4. 结合 ViewModel 采用仓库(Repository)模式架构

🔁 分步实现

1. 配置带缓存功能的 Retrofit + OkHttp

fun provideOkHttpClient(context: Context): OkHttpClient {  
    val cacheSize =10*1024*1024//10 MB 缓存大小
    val cache = Cache(context.cacheDir, cacheSize.toLong())  
  
    return OkHttpClient.Builder()  
        .cache(cache)  
        .addInterceptor { chain ->  
            var request = chain.request()  
            request =if (hasNetwork(context))  
                // 有网时缓存1分钟1分钟内重复请求直接取缓存
                request.newBuilder().header("Cache-Control", "public, max-age="+60).build()  
            else// 无网时最多使用24小时内的缓存
                request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale="+60*60*24).build()
            chain.proceed(request)  
        }  
        .build()  
}

2. Retrofit 基础配置

val retrofit = Retrofit.Builder()  
    .baseUrl("https://api.example.com/")  
    .client(provideOkHttpClient(context))  
    .addConverterFactory(GsonConverterFactory.create())  
    .build()  
  
interface NewsService {  
    @GET("top-headlines")  
    suspend fun getTopHeadlines(): Response<List<Article>>  
}

3. 配置 Room 数据库作为本地缓存

@Entity  
data classArticleEntity(  
    @PrimaryKey val id: String,  
    val title: String,  
    val content: String  
)  
  
@Dao  
interface ArticleDao {  
    @Query("SELECT * FROM ArticleEntity")  
    suspend fun getAll(): List<ArticleEntity>@Insert(onConflict = OnConflictStrategy.REPLACE)  
    suspend fun insertAll(articles: List<ArticleEntity>)  
}  
  
@Database(entities = [ArticleEntity::class], version =1)  
abstract classAppDatabase : RoomDatabase() {  
    abstract fun articleDao(): ArticleDao  
}

4. 实现仓库层(Repository)

classNewsRepository(  
    private val api: NewsService,  
    private val dao: ArticleDao  
) {  
    suspend fun getArticles(): List<ArticleEntity> {  
        returntry {  
            val response = api.getTopHeadlines()  
            if (response.isSuccessful && response.body() != null) {  
                // 请求成功先把最新数据写入数据库缓存
                val articles = response.body()!!  
                dao.insertAll(articles.map {  
                    ArticleEntity(it.id, it.title, it.content)  
                })  
                // 返回数据库中的最新数据
                dao.getAll()  
            } else {  
                // 请求失败直接返回缓存数据
                dao.getAll()  
            }  
        } catch (e: Exception) {  
            // 网络异常等情况返回缓存数据
            dao.getAll()  
        }  
    }  
}

5. ViewModel 与 UI 层调用

classNewsViewModel(private val repo: NewsRepository) : ViewModel() {  
    val articles = liveData {  
        emit(repo.getArticles())  
    }  
}

🌟 实现收益

  • ✅ 磁盘+数据库双缓存机制,大幅提升加载速度
  • ✅ 基于 Room 实现完善的离线访问能力
  • ✅ OkHttp 缓存减少重复请求,大幅降低流量消耗

💡 最佳实践

  • 合理设置缓存过期时间(max-agemax-stale 参数)
  • 绝对不要缓存用户敏感数据
  • 始终提供手动刷新数据的入口,避免用户一直看到过期内容
  • 监控缓存占用大小,必要时执行清理逻辑

🔍 拓展:用 Coil 实现图片缓存

Image(  
    painter = rememberImagePainter(  
        data ="https://example.com/image.jpg",  
        builder = {  
            crossfade(true)  
            placeholder(R.drawable.placeholder)  
        }  
    ),  
    contentDescription = null  
)

🔹 Coil、Glide、Picasso 等主流图片加载库都会默认开启内存+磁盘双缓存

📌 总结

缓存是 Android 应用开发中必不可少的优化手段,能够保障应用性能、节省用户流量、提供友好的离线访问体验。结合 OkHttpRoom 与上述最佳实践,你完全可以为用户打造出极速、稳定的使用体验。

原文链接https://medium.com/@prahaladsharma4u/caching-in-android-boosting-app-performance-with-real-world-scenarios-code-84c3b169f5f4