简体   繁体   English

除了 ROOM,在 Android 中缓存数据的最佳方法是什么?

[英]What is the best way to cache data in Android other than ROOM?

This question can be the solution to a few but trying to discuss the drawback here.这个问题可以解决一些问题,但试图在这里讨论缺点。

I have a project in which I use ROOM for caching the server response.我有一个项目,我在其中使用 ROOM 来缓存服务器响应。 Although it's one of several techniques to cache the data, instead of worrying about the best way of doing something I felt let's start( as I need to query the specific stored data to display ) and built the whole project on ROOM as caching technique.虽然它是缓存数据的几种技术之一,但与其担心做某事的最佳方式,我觉得让我们开始(因为我需要查询要显示的特定存储数据)并在 ROOM 上构建整个项目作为缓存技术。 What I'm doing currently,我目前在做什么,

First Call,第一次通话,

  1. Fetch data from an API.从 API 获取数据。
  2. Manipulate data according to the need.根据需要处理数据。
  3. Insert it into the ROOM database.将其插入 ROOM 数据库。
  4. Fetch it from ROOM to display.从 ROOM 中获取它以显示。

Subsequent call where cache data is available,缓存数据可用的后续调用,

  1. Fetch data from an API meanwhile display Cached data from ROOM.从 API 获取数据,同时显示 ROOM 中的缓存数据。
  2. Update the ROOM DB with a fresh response.使用新的响应更新 ROOM DB。
  3. Update UI.更新用户界面。

I definitely agree the above logic works perfectly to most of the scenarios for many.我绝对同意上述逻辑在大多数情况下都非常适用。

But there is still a drawback here.但是这里仍然有一个缺点。 At this point, my project has become huge, and every data shown is dependent on ROOM DB.至此,我的项目已经变得庞大,显示的每一个数据都依赖于 ROOM DB。 Every release I do has some or the other change in the table for which I need to write a migration.我所做的每个版本都会在我需要为其编写迁移的表中进行一些或其他更改。 Now my app users are spread across 5 to 6 different versions of the app.现在我的应用用户分布在 5 到 6 个不同版本的应用中。 The maintainability of the app is getting costlier.应用程序的可维护性变得越来越昂贵。

My question is,我的问题是,

  1. Is there a better way to handle migration complexity?有没有更好的方法来处理迁移复杂性?
  2. What should I consider other than ROOM for caching if I dedicate my time to REVAMP my caching logic?如果我花时间来改造我的缓存逻辑,除了 ROOM 缓存之外,我还应该考虑什么?

I hope I was able to explain my problem, thanks for your time on my issue.我希望我能够解释我的问题,感谢您在我的问题上花费的时间。

I am not sure if this would be really helpful.. But if you say that your table structure keeps on changing frequently then maybe instead of using ROOM or SQLite for caching you can consider writing the data(assuming its JSON) in a file in cache folder of your app and reading from it.. Android does provide us with a cache folder where we can create temp files etc so this way, if there is any change in the structure, you will not have to worry about migrations as the data will be written in the file and read from it directly.我不确定这是否真的有帮助.. 但是如果你说你的表结构不断变化,那么也许不是使用 ROOM 或 SQLite 进行缓存,你可以考虑将数据(假设它的 JSON)写入缓存中的文件中您的应用程序的文件夹并从中读取.. Android 确实为我们提供了一个缓存文件夹,我们可以在其中创建临时文件等,因此如果结构发生任何变化,您将不必担心迁移,因为数据会写入文件并直接从中读取。 So, changes in your model would suffice for this requirement.因此,您的 model 的更改足以满足此要求。 This would become similar to NoSQL but I am not sure android supports NoSQL natively.这将变得类似于 NoSQL 但我不确定 android 是否支持 NoSQL 本机。 There is also not very significant lag or processing time involved in this file read/write method.. But if you want to use the ROOM persistence library then yes you will have to handle migrations for every change in the structure..这种文件读/写方法也没有很明显的延迟或处理时间。但是如果你想使用 ROOM 持久性库,那么是的,你必须为结构中的每一次更改处理迁移。

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

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